Commit 26b75d37 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '225254-put-auto-close-incidents-behind-user-setting' into 'master'

Hook up the auto_close_incident checkbox with the corresponding DB column

See merge request gitlab-org/gitlab!40379
parents f1e74d6b 7c498f2e
......@@ -43,7 +43,7 @@ module OperationsHelper
create_issue: setting.create_issue.to_s,
issue_template_key: setting.issue_template_key.to_s,
send_email: setting.send_email.to_s,
auto_close_incident: 'true',
auto_close_incident: setting.auto_close_incident.to_s,
pagerduty_active: setting.pagerduty_active.to_s,
pagerduty_token: setting.pagerduty_token.to_s,
pagerduty_webhook_url: project_incidents_integrations_pagerduty_url(@project, token: setting.pagerduty_token),
......
......@@ -3,6 +3,7 @@
module AlertManagement
class ProcessPrometheusAlertService < BaseService
include Gitlab::Utils::StrongMemoize
include ::IncidentManagement::Settings
def execute
return bad_request unless parsed_alert.valid?
......@@ -69,6 +70,7 @@ module AlertManagement
def process_resolved_alert_management_alert
return if am_alert.blank?
return unless auto_close_incident?
if am_alert.resolve(ends_at)
close_issue(am_alert.issue)
......
......@@ -16,5 +16,9 @@ module IncidentManagement
def process_issues?
incident_management_setting.create_issue?
end
def auto_close_incident?
incident_management_setting.auto_close_incident?
end
end
end
......@@ -35,6 +35,9 @@ module Gitlab
},
pagerduty_active: {
name: 'pagerduty_webhook'
},
auto_close_incident: {
name: 'auto_close_incident'
}
}.with_indifferent_access.freeze
end
......
......@@ -152,7 +152,8 @@ RSpec.describe Projects::Settings::OperationsController do
create_issue: 'false',
send_email: 'false',
issue_template_key: 'some-other-template',
pagerduty_active: 'true'
pagerduty_active: 'true',
auto_close_incident: 'true'
}
}
end
......@@ -187,6 +188,8 @@ RSpec.describe Projects::Settings::OperationsController do
it_behaves_like 'a gitlab tracking event', { send_email: '0' }, 'disabled_sending_emails'
it_behaves_like 'a gitlab tracking event', { pagerduty_active: '1' }, 'enabled_pagerduty_webhook'
it_behaves_like 'a gitlab tracking event', { pagerduty_active: '0' }, 'disabled_pagerduty_webhook'
it_behaves_like 'a gitlab tracking event', { auto_close_incident: '1' }, 'enabled_auto_close_incident'
it_behaves_like 'a gitlab tracking event', { auto_close_incident: '0' }, 'disabled_auto_close_incident'
end
end
......
......@@ -137,7 +137,8 @@ RSpec.describe OperationsHelper do
:project_incident_management_setting,
project: project,
issue_template_key: 'template-key',
pagerduty_active: true
pagerduty_active: true,
auto_close_incident: false
)
end
......@@ -150,7 +151,7 @@ RSpec.describe OperationsHelper do
create_issue: 'false',
issue_template_key: 'template-key',
send_email: 'false',
auto_close_incident: 'true',
auto_close_incident: 'false',
pagerduty_active: 'true',
pagerduty_token: operations_settings.pagerduty_token,
pagerduty_webhook_url: project_incidents_integrations_pagerduty_url(project, token: operations_settings.pagerduty_token),
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe AlertManagement::ProcessPrometheusAlertService do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:project, reload: true) { create(:project, :repository) }
before do
allow(ProjectServiceWorker).to receive(:perform_async)
......@@ -128,55 +128,67 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
let(:status) { 'resolved' }
let!(:alert) { create(:alert_management_alert, project: project, fingerprint: parsed_alert.gitlab_fingerprint) }
context 'when status can be changed' do
it 'resolves an existing alert' do
expect { execute }.to change { alert.reload.resolved? }.to(true)
end
context 'when auto_resolve_incident set to true' do
let_it_be(:operations_settings) { create(:project_incident_management_setting, project: project, auto_close_incident: true) }
[true, false].each do |state_tracking_enabled|
context 'existing issue' do
before do
stub_feature_flags(track_resource_state_change_events: state_tracking_enabled)
end
context 'when status can be changed' do
it 'resolves an existing alert' do
expect { execute }.to change { alert.reload.resolved? }.to(true)
end
let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: parsed_alert.gitlab_fingerprint) }
[true, false].each do |state_tracking_enabled|
context 'existing issue' do
before do
stub_feature_flags(track_resource_state_change_events: state_tracking_enabled)
end
it 'closes the issue' do
issue = alert.issue
let!(:alert) { create(:alert_management_alert, :with_issue, project: project, fingerprint: parsed_alert.gitlab_fingerprint) }
expect { execute }
.to change { issue.reload.state }
.from('opened')
.to('closed')
end
it 'closes the issue' do
issue = alert.issue
expect { execute }
.to change { issue.reload.state }
.from('opened')
.to('closed')
end
if state_tracking_enabled
specify { expect { execute }.to change(ResourceStateEvent, :count).by(1) }
else
specify { expect { execute }.to change(Note, :count).by(1) }
if state_tracking_enabled
specify { expect { execute }.to change(ResourceStateEvent, :count).by(1) }
else
specify { expect { execute }.to change(Note, :count).by(1) }
end
end
end
end
end
context 'when status change did not succeed' do
before do
allow(AlertManagement::Alert).to receive(:for_fingerprint).and_return([alert])
allow(alert).to receive(:resolve).and_return(false)
end
context 'when status change did not succeed' do
before do
allow(AlertManagement::Alert).to receive(:for_fingerprint).and_return([alert])
allow(alert).to receive(:resolve).and_return(false)
end
it 'writes a warning to the log' do
expect(Gitlab::AppLogger).to receive(:warn).with(
message: 'Unable to update AlertManagement::Alert status to resolved',
project_id: project.id,
alert_id: alert.id
)
it 'writes a warning to the log' do
expect(Gitlab::AppLogger).to receive(:warn).with(
message: 'Unable to update AlertManagement::Alert status to resolved',
project_id: project.id,
alert_id: alert.id
)
execute
execute
end
end
it { is_expected.to be_success }
end
it { is_expected.to be_success }
context 'when auto_resolve_incident set to false' do
let_it_be(:operations_settings) { create(:project_incident_management_setting, project: project, auto_close_incident: false) }
it 'does not resolve an existing alert' do
expect { execute }.not_to change { alert.reload.resolved? }
end
end
end
context 'environment given' 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