Commit 2f1453fa authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '238571-populate-severity-for-incident-created-from-alert' into 'master'

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

Closes #238571

See merge request gitlab-org/gitlab!42072
parents cbf5d0f0 127dbc24
......@@ -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
......@@ -23,12 +24,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 success(issue)
ServiceResponse.success(payload: { issue: issue })
......
---
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] }
......
......@@ -39,6 +39,34 @@ RSpec.describe IncidentManagement::Incidents::CreateService do
let(:issue) { new_issue }
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