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 ...@@ -41,7 +41,8 @@ module AlertManagement
project, project,
user, user,
title: alert_presenter.title, title: alert_presenter.title,
description: alert_presenter.issue_description description: alert_presenter.issue_description,
severity: alert.severity
).execute ).execute
end end
......
...@@ -5,11 +5,12 @@ module IncidentManagement ...@@ -5,11 +5,12 @@ module IncidentManagement
class CreateService < BaseService class CreateService < BaseService
ISSUE_TYPE = 'incident' ISSUE_TYPE = 'incident'
def initialize(project, current_user, title:, description:) def initialize(project, current_user, title:, description:, severity: IssuableSeverity::DEFAULT)
super(project, current_user) super(project, current_user)
@title = title @title = title
@description = description @description = description
@severity = severity
end end
def execute def execute
...@@ -23,12 +24,14 @@ module IncidentManagement ...@@ -23,12 +24,14 @@ module IncidentManagement
return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid? return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid?
issue.update_severity(severity)
success(issue) success(issue)
end end
private private
attr_reader :title, :description attr_reader :title, :description, :severity
def success(issue) def success(issue)
ServiceResponse.success(payload: { issue: 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 ...@@ -82,6 +82,30 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
expect(user).to have_received(:can?).with(:create_issue, project) expect(user).to have_received(:can?).with(:create_issue, project)
end 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 context 'when the alert is prometheus alert' do
let(:alert) { prometheus_alert } let(:alert) { prometheus_alert }
let(:issue) { subject.payload[:issue] } let(:issue) { subject.payload[:issue] }
......
...@@ -39,6 +39,34 @@ RSpec.describe IncidentManagement::Incidents::CreateService do ...@@ -39,6 +39,34 @@ RSpec.describe IncidentManagement::Incidents::CreateService do
let(:issue) { new_issue } let(:issue) { new_issue }
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 context 'when incident label does not exists' do
it 'creates incident label' do it 'creates incident label' do
expect { create_incident }.to change { project.labels.where(title: label_title).count }.by(1) 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