Commit b110760c authored by David O'Regan's avatar David O'Regan Committed by Peter Leitzen

Add a system note on Alert creation

We add a base set of creating system
notes when creating generic and
prometheus alerts.
parent 91e71c4f
......@@ -33,7 +33,8 @@ module SystemNoteHelper
'designs_removed' => 'doc-image',
'designs_discussion_added' => 'doc-image',
'status' => 'status',
'alert_issue_added' => 'issues'
'alert_issue_added' => 'issues',
'new_alert_added' => 'warning'
}.freeze
def system_note_icon_name(note)
......
......@@ -20,7 +20,7 @@ class SystemNoteMetadata < ApplicationRecord
title time_tracking branch milestone discussion task moved
opened closed merged duplicate locked unlocked outdated
tag due_date pinned_embed cherry_pick health_status approved unapproved
status alert_issue_added relate unrelate
status alert_issue_added relate unrelate new_alert_added
].freeze
validates :note, presence: true
......
......@@ -54,6 +54,7 @@ module AlertManagement
if new_alert.save
new_alert.execute_services
@am_alert = new_alert
SystemNoteService.create_new_alert(new_alert, Gitlab::AlertManagement::AlertParams::MONITORING_TOOLS[:prometheus])
return
end
......
......@@ -48,6 +48,7 @@ module Projects
def create_alert
alert = AlertManagement::Alert.create(am_alert_params)
alert.execute_services if alert.persisted?
SystemNoteService.create_new_alert(alert, 'Generic Alert Endpoint')
alert
end
......
......@@ -308,6 +308,10 @@ module SystemNoteService
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project, author: author).new_alert_issue(issue)
end
def create_new_alert(alert, monitoring_tool)
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project).create_new_alert(monitoring_tool)
end
private
def merge_requests_service(noteable, project, author)
......
......@@ -2,6 +2,21 @@
module SystemNotes
class AlertManagementService < ::SystemNotes::BaseService
# Called when the a new AlertManagement::Alert has been created
#
# alert - AlertManagement::Alert object.
#
# Example Note text:
#
# "GitLab Alert Bot logged an alert from Prometheus"
#
# Returns the created Note object
def create_new_alert(monitoring_tool)
body = "logged an alert from **#{monitoring_tool}**"
create_note(NoteSummary.new(noteable, project, User.alert_bot, body, action: 'new_alert_added'))
end
# Called when the status of an AlertManagement::Alert has changed
#
# alert - AlertManagement::Alert object.
......
---
title: Add a system note on Alert creation
merge_request: 40128
author:
type: added
......@@ -84,6 +84,10 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService do
context 'when alert can be created' do
it_behaves_like 'creates an alert management alert'
it 'creates a system note corresponding to alert creation' do
expect { subject }.to change(Note, :count).by(1)
end
it 'processes the incident alert' do
expect(IncidentManagement::ProcessAlertWorker)
.to receive(:perform_async)
......
......@@ -123,6 +123,10 @@ RSpec.describe Projects::Alerting::NotifyService do
it_behaves_like 'creates an alert management alert'
it_behaves_like 'assigns the alert properties'
it 'creates a system note corresponding to alert creation' do
expect { subject }.to change(Note, :count).by(1)
end
context 'existing alert with same fingerprint' do
let(:fingerprint_sha) { Digest::SHA1.hexdigest(fingerprint) }
let!(:alert) { create(:alert_management_alert, project: project, fingerprint: fingerprint_sha) }
......
......@@ -739,4 +739,17 @@ RSpec.describe SystemNoteService do
described_class.new_alert_issue(alert, alert.issue, author)
end
end
describe '.create_new_alert' do
let(:alert) { build(:alert_management_alert) }
let(:monitoring_tool) { 'Prometheus' }
it 'calls AlertManagementService' do
expect_next_instance_of(SystemNotes::AlertManagementService) do |service|
expect(service).to receive(:create_new_alert).with(monitoring_tool)
end
described_class.create_new_alert(alert, monitoring_tool)
end
end
end
......@@ -7,6 +7,19 @@ RSpec.describe ::SystemNotes::AlertManagementService do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:noteable) { create(:alert_management_alert, :with_issue, :acknowledged, project: project) }
describe '#create_new_alert' do
subject { described_class.new(noteable: noteable, project: project).create_new_alert('Some Service') }
it_behaves_like 'a system note' do
let(:author) { User.alert_bot }
let(:action) { 'new_alert_added' }
end
it 'has the appropriate message' do
expect(subject.note).to eq('logged an alert from **Some Service**')
end
end
describe '#change_alert_status' do
subject { described_class.new(noteable: noteable, project: project, author: author).change_alert_status(noteable) }
......
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