Commit a7413a61 authored by David Fernandez's avatar David Fernandez

Merge branch 'track-validation-error-on-environment-update' into 'master'

Track the validation error on Environment Update

See merge request gitlab-org/gitlab!63398
parents 6db20760 15998eb4
......@@ -9,6 +9,8 @@ module Deployments
delegate :variables, to: :deployable
delegate :options, to: :deployable, allow_nil: true
EnvironmentUpdateFailure = Class.new(StandardError)
def initialize(deployment)
@deployment = deployment
@deployable = deployment.deployable
......@@ -31,8 +33,18 @@ module Deployments
renew_deployment_tier
environment.fire_state_event(action)
if environment.save && !environment.stopped?
deployment.update_merge_request_metrics!
if environment.save
deployment.update_merge_request_metrics! unless environment.stopped?
else
# If there is a validation error on environment update, such as
# the external URL is malformed, the error message is recorded for debugging purpose.
# We should surface the error message to users for letting them to take an action.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/21182.
Gitlab::ErrorTracking.track_exception(
EnvironmentUpdateFailure.new,
project_id: deployment.project_id,
environment_id: environment.id,
reason: environment.errors.full_messages.to_sentence)
end
end
end
......
......@@ -95,6 +95,42 @@ RSpec.describe Deployments::UpdateEnvironmentService do
end
end
context 'when external URL is specified and the tier is unset' do
let(:options) { { name: 'production', url: external_url } }
before do
environment.update_columns(external_url: external_url, tier: nil)
job.update!(environment: 'production')
end
context 'when external URL is valid' do
let(:external_url) { 'https://google.com' }
it 'succeeds to update the tier automatically' do
expect { subject.execute }.to change { environment.tier }.from(nil).to('production')
end
end
context 'when external URL is invalid' do
let(:external_url) { 'google.com' }
it 'fails to update the tier due to validation error' do
expect { subject.execute }.not_to change { environment.tier }
end
it 'tracks an exception' do
expect(Gitlab::ErrorTracking).to receive(:track_exception)
.with(an_instance_of(described_class::EnvironmentUpdateFailure),
project_id: project.id,
environment_id: environment.id,
reason: %q{External url is blocked: Only allowed schemes are http, https})
.once
subject.execute
end
end
end
context 'when variables are used' do
let(:options) do
{ name: 'review-apps/$CI_COMMIT_REF_NAME',
......
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