Commit 7674a8f4 authored by Kamil Trzciński's avatar Kamil Trzciński

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
parents f139ccf7 1048ed4d
......@@ -160,18 +160,18 @@ class Deployment < ActiveRecord::Base
end
def has_metrics?
prometheus_adapter&.can_query?
prometheus_adapter&.can_query? && success?
end
def metrics
return {} unless has_metrics? && success?
return {} unless has_metrics?
metrics = prometheus_adapter.query(:deployment, self)
metrics&.merge(deployment_time: finished_at.to_i) || {}
end
def additional_metrics
return {} unless has_metrics? && success?
return {} unless has_metrics?
metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
metrics&.merge(deployment_time: finished_at.to_i) || {}
......
......@@ -11,7 +11,7 @@ class EnvironmentStatusEntity < Grape::Entity
project_environment_path(es.project, es.environment)
end
expose :metrics_url, if: ->(*) { can_read_environment? && environment.has_metrics? } do |es|
expose :metrics_url, if: ->(*) { can_read_environment? && deployment.has_metrics? } do |es|
metrics_project_environment_deployment_path(es.project, es.environment, es.deployment)
end
......@@ -45,6 +45,10 @@ class EnvironmentStatusEntity < Grape::Entity
object.environment
end
def deployment
object.deployment
end
def project
object.environment.project
end
......
---
title: Avoid returning deployment metrics url to MR widget when the deployment is
not successful
merge_request: 23010
author:
type: fixed
......@@ -40,4 +40,40 @@ describe EnvironmentStatusEntity do
it { is_expected.to include(:stop_url) }
end
context 'when deployment has metrics' do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
let(:simple_metrics) do
{
success: true,
metrics: {},
last_update: 42
}
end
before do
project.add_maintainer(user)
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
allow(entity).to receive(:deployment).and_return(deployment)
end
context 'when deployment succeeded' do
let(:deployment) { create(:deployment, :succeed, :review_app) }
it 'returns metrics url' do
expect(subject[:metrics_url])
.to eq("/#{project.namespace.name}/#{project.name}/environments/#{environment.id}/deployments/#{deployment.iid}/metrics")
end
end
context 'when deployment is running' do
let(:deployment) { create(:deployment, :running, :review_app) }
it 'does not return metrics url' do
expect(subject[:metrics_url]).to be_nil
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment