Commit f484f3f8 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add SystemNodes::IncidentService class

Allows to add system notes on change severity status for incidents
parent 18f3c62f
...@@ -34,7 +34,8 @@ module SystemNoteHelper ...@@ -34,7 +34,8 @@ module SystemNoteHelper
'designs_discussion_added' => 'doc-image', 'designs_discussion_added' => 'doc-image',
'status' => 'status', 'status' => 'status',
'alert_issue_added' => 'issues', 'alert_issue_added' => 'issues',
'new_alert_added' => 'warning' 'new_alert_added' => 'warning',
'severity' => 'information-o'
}.freeze }.freeze
def system_note_icon_name(note) def system_note_icon_name(note)
......
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
class IssuableSeverity < ApplicationRecord class IssuableSeverity < ApplicationRecord
DEFAULT = 'unknown' DEFAULT = 'unknown'
SEVERITY_LABELS = {
unknown: 'Unknown',
low: 'Low - S4',
medium: 'Medium - S3',
high: 'High - S2',
critical: 'Critical - S1'
}.freeze
belongs_to :issue belongs_to :issue
......
...@@ -20,7 +20,7 @@ class SystemNoteMetadata < ApplicationRecord ...@@ -20,7 +20,7 @@ class SystemNoteMetadata < ApplicationRecord
title time_tracking branch milestone discussion task moved title time_tracking branch milestone discussion task moved
opened closed merged duplicate locked unlocked outdated opened closed merged duplicate locked unlocked outdated
tag due_date pinned_embed cherry_pick health_status approved unapproved tag due_date pinned_embed cherry_pick health_status approved unapproved
status alert_issue_added relate unrelate new_alert_added status alert_issue_added relate unrelate new_alert_added severity
].freeze ].freeze
validates :note, presence: true validates :note, presence: true
......
...@@ -308,6 +308,10 @@ module SystemNoteService ...@@ -308,6 +308,10 @@ module SystemNoteService
::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project).create_new_alert(monitoring_tool) ::SystemNotes::AlertManagementService.new(noteable: alert, project: alert.project).create_new_alert(monitoring_tool)
end end
def change_incident_severity(incident, author)
::SystemNotes::IncidentService.new(noteable: incident, project: incident.project, author: author).change_incident_severity
end
private private
def merge_requests_service(noteable, project, author) def merge_requests_service(noteable, project, author)
......
# frozen_string_literal: true
module SystemNotes
class IncidentService < ::SystemNotes::BaseService
# Called when the severity of an Incident has changed
#
# Example Note text:
#
# "changed the severity to Medium - S3"
#
# Returns the created Note object
def change_incident_severity
severity = noteable.severity
severity_label = IssuableSeverity::SEVERITY_LABELS.fetch(severity.to_sym)
body = "changed the severity to **#{severity_label}**"
create_note(NoteSummary.new(noteable, project, author, body, action: 'severity'))
end
end
end
...@@ -741,4 +741,16 @@ RSpec.describe SystemNoteService do ...@@ -741,4 +741,16 @@ RSpec.describe SystemNoteService do
described_class.create_new_alert(alert, monitoring_tool) described_class.create_new_alert(alert, monitoring_tool)
end end
end end
describe '.change_incident_severity' do
let(:incident) { build(:incident) }
it 'calls IncidentService' do
expect_next_instance_of(SystemNotes::IncidentService) do |service|
expect(service).to receive(:change_incident_severity)
end
described_class.change_incident_severity(incident, author)
end
end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::SystemNotes::IncidentService do
let_it_be(:author) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:noteable) { create(:incident, project: project) }
let_it_be(:issuable_severity) { create(:issuable_severity, issue: noteable, severity: :medium) }
describe '#change_incident_severity' do
subject(:change_severity) { described_class.new(noteable: noteable, project: project, author: author).change_incident_severity }
it_behaves_like 'a system note' do
let(:action) { 'severity' }
end
IssuableSeverity.severities.keys.each do |severity|
context "with #{severity} severity" do
before do
issuable_severity.update!(severity: severity)
end
it 'has the appropriate message' do
severity_label = IssuableSeverity::SEVERITY_LABELS.fetch(severity.to_sym)
expect(change_severity.note).to eq("changed the severity to **#{severity_label}**")
end
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