Commit 4a8e1e7a authored by Michael Kozono's avatar Michael Kozono

Merge branch '222253-close-issue-on-resolved-alert' into 'master'

Auto close related alert issue on recovery

See merge request gitlab-org/gitlab!35208
parents d213e1fb 621788fe
......@@ -66,7 +66,11 @@ module AlertManagement
def process_resolved_alert_management_alert
return if am_alert.blank?
return if am_alert.resolve(ends_at)
if am_alert.resolve(ends_at)
close_issue(am_alert.issue)
return
end
logger.warn(
message: 'Unable to update AlertManagement::Alert status to resolved',
......@@ -75,6 +79,16 @@ module AlertManagement
)
end
def close_issue(issue)
return if issue.blank? || issue.closed?
Issues::CloseService
.new(project, User.alert_bot)
.execute(issue, system_note: false)
SystemNoteService.auto_resolve_prometheus_alert(issue, project, User.alert_bot) if issue.reset.closed?
end
def logger
@logger ||= Gitlab::AppLogger
end
......
---
title: Automatically close related issue when resolving Alert Management Prometheus
Alert
merge_request: 35208
author:
type: added
......@@ -16,7 +16,9 @@ FactoryBot.define do
end
trait :with_issue do
issue
after(:create) do |alert|
create(:issue, alert_management_alert: alert, project: alert.project)
end
end
trait :with_assignee do |alert|
......
......@@ -78,7 +78,7 @@ RSpec.describe 'getting Alert Management Alerts' do
expect(second_alert).to include(
'iid' => resolved_alert.iid.to_s,
'issueIid' => nil,
'issueIid' => resolved_alert.issue_iid.to_s,
'status' => 'RESOLVED',
'endedAt' => resolved_alert.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ')
)
......
......@@ -123,6 +123,21 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
it 'resolves an existing alert' do
expect { execute }.to change { alert.reload.resolved? }.to(true)
end
context 'existing issue' do
let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: parsed_alert.gitlab_fingerprint) }
it 'closes the issue' do
issue = alert.issue
expect { execute }
.to change { issue.reload.state }
.from('opened')
.to('closed')
end
specify { expect { execute }.to change(Note, :count).by(1) }
end
end
context 'when status change did not succeed' 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