Skip to content

Commit 7674a8f

Browse files
committed
Merge branch 'fix-deployment-metrics-in-mr-widget' into 'master'
Avoid returning deployment metrics url to MR widget when the deployment is not successful Closes #53870 See merge request gitlab-org/gitlab-ce!23010
2 parents f139ccf + 1048ed4 commit 7674a8f

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

app/models/deployment.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,18 @@ def formatted_deployment_time
160160
end
161161

162162
def has_metrics?
163-
prometheus_adapter&.can_query?
163+
prometheus_adapter&.can_query? && success?
164164
end
165165

166166
def metrics
167-
return {} unless has_metrics? && success?
167+
return {} unless has_metrics?
168168

169169
metrics = prometheus_adapter.query(:deployment, self)
170170
metrics&.merge(deployment_time: finished_at.to_i) || {}
171171
end
172172

173173
def additional_metrics
174-
return {} unless has_metrics? && success?
174+
return {} unless has_metrics?
175175

176176
metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
177177
metrics&.merge(deployment_time: finished_at.to_i) || {}

app/serializers/environment_status_entity.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class EnvironmentStatusEntity < Grape::Entity
1111
project_environment_path(es.project, es.environment)
1212
end
1313

14-
expose :metrics_url, if: ->(*) { can_read_environment? && environment.has_metrics? } do |es|
14+
expose :metrics_url, if: ->(*) { can_read_environment? && deployment.has_metrics? } do |es|
1515
metrics_project_environment_deployment_path(es.project, es.environment, es.deployment)
1616
end
1717

@@ -45,6 +45,10 @@ def environment
4545
object.environment
4646
end
4747

48+
def deployment
49+
object.deployment
50+
end
51+
4852
def project
4953
object.environment.project
5054
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Avoid returning deployment metrics url to MR widget when the deployment is
3+
not successful
4+
merge_request: 23010
5+
author:
6+
type: fixed

spec/serializers/environment_status_entity_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,40 @@
4040

4141
it { is_expected.to include(:stop_url) }
4242
end
43+
44+
context 'when deployment has metrics' do
45+
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
46+
47+
let(:simple_metrics) do
48+
{
49+
success: true,
50+
metrics: {},
51+
last_update: 42
52+
}
53+
end
54+
55+
before do
56+
project.add_maintainer(user)
57+
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
58+
allow(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
59+
allow(entity).to receive(:deployment).and_return(deployment)
60+
end
61+
62+
context 'when deployment succeeded' do
63+
let(:deployment) { create(:deployment, :succeed, :review_app) }
64+
65+
it 'returns metrics url' do
66+
expect(subject[:metrics_url])
67+
.to eq("/#{project.namespace.name}/#{project.name}/environments/#{environment.id}/deployments/#{deployment.iid}/metrics")
68+
end
69+
end
70+
71+
context 'when deployment is running' do
72+
let(:deployment) { create(:deployment, :running, :review_app) }
73+
74+
it 'does not return metrics url' do
75+
expect(subject[:metrics_url]).to be_nil
76+
end
77+
end
78+
end
4379
end

0 commit comments

Comments
 (0)