Commit a3c8856e authored by Vitali Tatarintev's avatar Vitali Tatarintev

Check if the issue created by Alert Bot

Update issue title only if it was created by Alert Bot
parent 6d549a2c
......@@ -34,7 +34,7 @@ module EE
validates :weight, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
after_create :update_generic_alert_issue_if_applicable
after_create :update_generic_alert_title_if_applicable
end
class_methods do
......@@ -134,14 +134,14 @@ module EE
private
def update_generic_alert_issue_if_applicable
return unless has_default_generic_alert_title? && project.alerts_service_activated?
update_column(:title, "#{title} #{id}")
def update_generic_alert_title_if_applicable
update_column(:title, "#{title} #{id}") if generic_alert_with_default_title?
end
def has_default_generic_alert_title?
title == ::Gitlab::Alerting::NotificationPayloadParser::DEFAULT_TITLE
def generic_alert_with_default_title?
title == ::Gitlab::Alerting::NotificationPayloadParser::DEFAULT_TITLE &&
project.alerts_service_activated? &&
author == ::User.alert_bot
end
end
end
......@@ -10,15 +10,17 @@ describe Issue do
context 'callbacks' do
describe '.after_create' do
set(:project) { create(:project) }
let(:author) { User.alert_bot }
context 'when issue title is "New: Incident"' do
let(:issue) { build(:issue, project: project, title: 'New: Incident') }
let(:issue) { build(:issue, project: project, author: author, title: 'New: Incident') }
context 'when alerts service is active' do
before do
allow(project).to receive(:alerts_service_activated?).and_return(true)
end
context 'when the author is Alert Bot' do
it 'updates issue title with the IID' do
issue.save
......@@ -26,6 +28,15 @@ describe Issue do
end
end
context 'when the author is not an Alert Bot' do
let(:author) { create(:user) }
it 'does not change issue title' do
expect { issue.save }.not_to change { issue.title }
end
end
end
context 'when alerts service is not active' do
before do
allow(project).to receive(:alerts_service_activated?).and_return(false)
......
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