Commit f5a67298 authored by Patrick Bajao's avatar Patrick Bajao

Merge branch '323140-create-system-note-for-escalations' into 'master'

Add escalation note when escalating to users

See merge request gitlab-org/gitlab!64899
parents 45e72f92 bb6d5af6
......@@ -28,6 +28,7 @@ module IncidentManagement
validates :rule_id, presence: true, uniqueness: { scope: [:alert_id] }
delegate :project, to: :alert
delegate :policy, to: :rule, allow_nil: true
end
end
end
......@@ -112,6 +112,10 @@ module EE
issuables_service(noteable, project, author).publish_issue_to_status_page
end
def notify_via_escalation(noteable, project, recipients, escalation_policy, oncall_schedule)
escalations_service(noteable, project).notify_via_escalation(recipients, escalation_policy: escalation_policy, oncall_schedule: oncall_schedule)
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.notify_via_escalation(target, project, oncall_notification_recipients, escalation.policy, oncall_schedule)
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 notify_via_escalation(recipients, escalation_policy: nil, oncall_schedule: nil)
body = if escalation_policy
"notified #{recipients.map(&:to_reference).to_sentence} of this alert via escalation policy **#{escalation_policy.name}**"
else
"notified #{recipients.map(&:to_reference).to_sentence} of this alert via schedule **#{oncall_schedule.name}**, per an escalation rule which no longer exists"
end
create_note(NoteSummary.new(noteable, project, author, body, action: 'new_alert_added'))
end
end
end
......@@ -11,6 +11,7 @@ RSpec.describe IncidentManagement::PendingEscalations::Alert do
it { is_expected.to validate_presence_of(:process_at) }
it { is_expected.to validate_presence_of(:status) }
it { is_expected.to delegate_method(:project).to(:alert) }
it { is_expected.to delegate_method(:policy).to(:rule).allow_nil }
it { is_expected.to validate_uniqueness_of(:rule_id).scoped_to([:alert_id]) }
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(:notify_via_escalation).with(alert, project, [a_kind_of(User)], escalation_policy, schedule_1)
.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 '#notify_via_escalation' do
subject { described_class.new(noteable: noteable, project: project).notify_via_escalation([user, user_2], escalation_policy: escalation_policy, oncall_schedule: oncall_schedule) }
let_it_be(:escalation_policy) { create(:incident_management_escalation_policy, project: project) }
let_it_be(:oncall_schedule) { create(:incident_management_oncall_schedule, 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
context 'when policy is missing' do
let_it_be(:escalation_policy) { nil }
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 schedule **#{oncall_schedule.name}**, per an escalation rule which no longer exists")
end
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