Avoid returning deployment metrics url to MR widget when the deployment is not successful

This commit is contained in:
Shinya Maeda 2018-11-14 11:11:27 +00:00 committed by Kamil Trzciński
parent f139ccf796
commit 1048ed4d86
4 changed files with 50 additions and 4 deletions

View File

@ -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) || {}

View File

@ -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

View File

@ -0,0 +1,6 @@
---
title: Avoid returning deployment metrics url to MR widget when the deployment is
not successful
merge_request: 23010
author:
type: fixed

View File

@ -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