Commit 62c5f15a authored by James Lopez's avatar James Lopez

Merge branch '216142-resolve-alert-when-associated-issue-closes' into 'master'

Automatically resolve alert when associated issue closes

Closes #216142

See merge request gitlab-org/gitlab!33278
parents 8d7d7acf 721175ec
......@@ -139,6 +139,10 @@ class Issue < ApplicationRecord
issue.closed_at = nil
issue.closed_by = nil
end
after_transition any => :closed do |issue|
issue.resolve_associated_alert_management_alert
end
end
# Alias to state machine .with_state_id method
......@@ -352,6 +356,18 @@ class Issue < ApplicationRecord
@design_collection ||= ::DesignManagement::DesignCollection.new(self)
end
def resolve_associated_alert_management_alert
return unless alert_management_alert
return if alert_management_alert.resolve
Gitlab::AppLogger.warn(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: id,
alert_id: alert_management_alert.id,
alert_errors: alert_management_alert.errors.messages
)
end
private
def ensure_metrics
......
---
title: Automatically resolve alert when associated issue closes
merge_request: 33278
author:
type: added
......@@ -89,3 +89,6 @@ The Alert Management detail view enables you to create an issue with a
description automatically populated from an alert. To create the issue,
click the **Create Issue** button. You can then view the issue from the
alert by clicking the **View Issue** button.
Closing a GitLab issue associated with an alert changes the alert's status to Resolved.
See [Alert Management statuses](#alert-management-statuses) for more details about statuses.
......@@ -186,6 +186,35 @@ describe Issue do
expect { issue.close }.to change { issue.state_id }.from(open_state).to(closed_state)
end
context 'when there is an associated Alert Management Alert' do
context 'when alert can be resolved' do
let!(:alert) { create(:alert_management_alert, project: issue.project, issue: issue) }
it 'resolves an alert' do
expect { issue.close }.to change { alert.reload.resolved? }.to(true)
end
end
context 'when alert cannot be resolved' do
let!(:alert) { create(:alert_management_alert, :with_validation_errors, project: issue.project, issue: issue) }
before do
allow(Gitlab::AppLogger).to receive(:warn).and_call_original
end
it 'writes a warning into the log' do
issue.close
expect(Gitlab::AppLogger).to have_received(:warn).with(
message: 'Cannot resolve an associated Alert Management alert',
issue_id: issue.id,
alert_id: alert.id,
alert_errors: { hosts: ['hosts array is over 255 chars'] }
)
end
end
end
end
describe '#reopen' do
......
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