Commit 127dbc24 authored by Vitali Tatarintev's avatar Vitali Tatarintev Committed by Bob Van Landuyt

Populate an Incident's severity when it's created from an Alert

parent cb3da621
......@@ -41,7 +41,8 @@ module AlertManagement
project,
user,
title: alert_presenter.title,
description: alert_presenter.issue_description
description: alert_presenter.issue_description,
severity: alert.severity
).execute
end
......
......@@ -5,11 +5,12 @@ module IncidentManagement
class CreateService < BaseService
ISSUE_TYPE = 'incident'
def initialize(project, current_user, title:, description:)
def initialize(project, current_user, title:, description:, severity: IssuableSeverity::DEFAULT)
super(project, current_user)
@title = title
@description = description
@severity = severity
end
def execute
......@@ -24,12 +25,14 @@ module IncidentManagement
return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid?
issue.update_severity(severity)
success(issue)
end
private
attr_reader :title, :description
attr_reader :title, :description, :severity
def find_or_create_incident_label
IncidentManagement::CreateIncidentLabelService
......
---
title: Set incident severity when it is created from an alert.
merge_request: 42072
author:
type: added
......@@ -82,6 +82,30 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
expect(user).to have_received(:can?).with(:create_issue, project)
end
context 'with alert severity' do
using RSpec::Parameterized::TableSyntax
where(:alert_severity, :incident_severity) do
'critical' | 'critical'
'high' | 'high'
'medium' | 'medium'
'low' | 'low'
'info' | 'unknown'
'unknown' | 'unknown'
end
with_them do
before do
alert.update!(severity: alert_severity)
execute
end
it 'sets the correct severity level' do
expect(created_issue.severity).to eq(incident_severity)
end
end
end
context 'when the alert is prometheus alert' do
let(:alert) { prometheus_alert }
let(:issue) { subject.payload[:issue] }
......
......@@ -34,6 +34,34 @@ RSpec.describe IncidentManagement::Incidents::CreateService do
end
end
context 'with default severity' do
it 'sets the correct severity level to "unknown"' do
create_incident
expect(new_issue.severity).to eq(IssuableSeverity::DEFAULT)
end
end
context 'with severity' do
using RSpec::Parameterized::TableSyntax
subject(:create_incident) { described_class.new(project, user, title: title, description: description, severity: severity).execute }
where(:severity, :incident_severity) do
'critical' | 'critical'
'high' | 'high'
'medium' | 'medium'
'low' | 'low'
'unknown' | 'unknown'
end
with_them do
it 'sets the correct severity level' do
create_incident
expect(new_issue.severity).to eq(incident_severity)
end
end
end
context 'when incident label does not exists' do
it 'creates incident label' do
expect { create_incident }.to change { project.labels.where(title: label_title).count }.by(1)
......
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