Commit 6bd709f4 authored by Sean Arnold's avatar Sean Arnold

Add system note when escalating to users

Creates a system note after an escalation event to
show which users and on-call polices were escalated to.

Changelog: added
EE: true
parent 78ca2362
......@@ -28,6 +28,7 @@ module IncidentManagement
validates :rule_id, presence: true, uniqueness: { scope: [:alert_id] }
delegate :project, to: :alert
delegate :policy, to: :rule
end
end
end
......@@ -112,6 +112,10 @@ module EE
issuables_service(noteable, project, author).publish_issue_to_status_page
end
def alert_via_escalation(noteable, project, recipients, escalation_policy)
escalations_service(noteable, project).alert_via_escalation(recipients, escalation_policy)
end
private
def issuables_service(noteable, project, author)
......@@ -129,5 +133,9 @@ module EE
def vulnerabilities_service(noteable, project, author)
::SystemNotes::VulnerabilitiesService.new(noteable: noteable, project: project, author: author)
end
def escalations_service(noteable, project)
::SystemNotes::EscalationsService.new(noteable: noteable, project: project)
end
end
end
......@@ -3,6 +3,8 @@
module IncidentManagement
module PendingEscalations
class ProcessService < BaseService
include Gitlab::Utils::StrongMemoize
def initialize(escalation)
@escalation = escalation
@project = escalation.project
......@@ -17,6 +19,7 @@ module IncidentManagement
return if target_status_exceeded_rule?
notify_recipients
create_system_notes
destroy_escalation!
end
......@@ -42,11 +45,17 @@ module IncidentManagement
NotificationService
.new
.async
.notify_oncall_users_of_alert(oncall_notification_recipients.to_a, target)
.notify_oncall_users_of_alert(oncall_notification_recipients, target)
end
def create_system_notes
SystemNoteService.alert_via_escalation(target, project, oncall_notification_recipients, escalation.policy)
end
def oncall_notification_recipients
::IncidentManagement::OncallUsersFinder.new(project, schedule: oncall_schedule).execute
strong_memoize(:oncall_notification_recipients) do
::IncidentManagement::OncallUsersFinder.new(project, schedule: oncall_schedule).execute.to_a
end
end
def destroy_escalation!
......
# frozen_string_literal: true
module SystemNotes
class EscalationsService < ::SystemNotes::BaseService
def initialize(noteable: nil, project: nil)
@noteable = noteable
@project = project
@author = User.alert_bot
end
def alert_via_escalation(recipients, escalation_policy)
body = "notified #{recipients.map(&:to_reference).to_sentence} of this alert via escalation policy **#{escalation_policy.name}**"
create_note(NoteSummary.new(noteable, project, author, body, action: 'new_alert_added'))
end
end
end
......@@ -49,6 +49,14 @@ RSpec.describe IncidentManagement::PendingEscalations::ProcessService do
it_behaves_like 'sends on-call notification'
it_behaves_like 'deletes the escalation'
it 'creates a system note' do
expect(SystemNoteService)
.to receive(:alert_via_escalation).with(alert, project, [a_kind_of(User)], escalation_policy)
.and_call_original
expect { execute }.to change(Note, :count).by(1)
end
context 'feature flag is off' do
before do
stub_feature_flags(escalation_policies_mvc: false)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe SystemNotes::EscalationsService do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
let_it_be(:user_2) { create(:user) }
let_it_be(:author) { User.alert_bot }
describe '#alert_via_escalation' do
subject { described_class.new(noteable: noteable, project: project).alert_via_escalation([user, user_2], escalation_policy) }
let_it_be(:escalation_policy) { create(:incident_management_escalation_policy, project: project) }
let_it_be(:noteable) { create(:alert_management_alert, project: project) }
it_behaves_like 'a system note' do
let(:action) { 'new_alert_added' }
end
it 'posts the correct text to the system note' do
expect(subject.note).to match("notified #{user.to_reference} and #{user_2.to_reference} of this alert via escalation policy **#{escalation_policy.name}**")
end
end
end
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