Commit 721175ec authored by Vitali Tatarintev's avatar Vitali Tatarintev Committed by James Lopez

Automatically resolve alert when associated issue closes

parent 04d6297d
...@@ -139,6 +139,10 @@ class Issue < ApplicationRecord ...@@ -139,6 +139,10 @@ class Issue < ApplicationRecord
issue.closed_at = nil issue.closed_at = nil
issue.closed_by = nil issue.closed_by = nil
end end
after_transition any => :closed do |issue|
issue.resolve_associated_alert_management_alert
end
end end
# Alias to state machine .with_state_id method # Alias to state machine .with_state_id method
...@@ -352,6 +356,18 @@ class Issue < ApplicationRecord ...@@ -352,6 +356,18 @@ class Issue < ApplicationRecord
@design_collection ||= ::DesignManagement::DesignCollection.new(self) @design_collection ||= ::DesignManagement::DesignCollection.new(self)
end 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 private
def ensure_metrics 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 ...@@ -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, description automatically populated from an alert. To create the issue,
click the **Create Issue** button. You can then view the issue from the click the **Create Issue** button. You can then view the issue from the
alert by clicking the **View Issue** button. 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 ...@@ -186,6 +186,35 @@ describe Issue do
expect { issue.close }.to change { issue.state_id }.from(open_state).to(closed_state) expect { issue.close }.to change { issue.state_id }.from(open_state).to(closed_state)
end 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 end
describe '#reopen' do 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