Commit 621788fe authored by Sean Arnold's avatar Sean Arnold Committed by Michael Kozono

Auto close related alert issue on recovery

- Limited to prometheus alerts right now
parent 227d1fee
...@@ -66,7 +66,11 @@ module AlertManagement ...@@ -66,7 +66,11 @@ module AlertManagement
def process_resolved_alert_management_alert def process_resolved_alert_management_alert
return if am_alert.blank? 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( logger.warn(
message: 'Unable to update AlertManagement::Alert status to resolved', message: 'Unable to update AlertManagement::Alert status to resolved',
...@@ -75,6 +79,16 @@ module AlertManagement ...@@ -75,6 +79,16 @@ module AlertManagement
) )
end 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 def logger
@logger ||= Gitlab::AppLogger @logger ||= Gitlab::AppLogger
end 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 ...@@ -16,7 +16,9 @@ FactoryBot.define do
end end
trait :with_issue do trait :with_issue do
issue after(:create) do |alert|
create(:issue, alert_management_alert: alert, project: alert.project)
end
end end
trait :with_assignee do |alert| trait :with_assignee do |alert|
......
...@@ -78,7 +78,7 @@ RSpec.describe 'getting Alert Management Alerts' do ...@@ -78,7 +78,7 @@ RSpec.describe 'getting Alert Management Alerts' do
expect(second_alert).to include( expect(second_alert).to include(
'iid' => resolved_alert.iid.to_s, 'iid' => resolved_alert.iid.to_s,
'issueIid' => nil, 'issueIid' => resolved_alert.issue_iid.to_s,
'status' => 'RESOLVED', 'status' => 'RESOLVED',
'endedAt' => resolved_alert.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ') 'endedAt' => resolved_alert.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ')
) )
......
...@@ -123,6 +123,21 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do ...@@ -123,6 +123,21 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
it 'resolves an existing alert' do it 'resolves an existing alert' do
expect { execute }.to change { alert.reload.resolved? }.to(true) expect { execute }.to change { alert.reload.resolved? }.to(true)
end 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 end
context 'when status change did not succeed' do 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