Commit a45ddf1d authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Dylan Griffith

Update default title of alerts to New: Alert

parent fe4ba437
......@@ -4,6 +4,8 @@ module AlertManagement
class CreateAlertIssueService
include Gitlab::Utils::StrongMemoize
DEFAULT_ALERT_TITLE = ::Gitlab::AlertManagement::Payload::Generic::DEFAULT_TITLE
# @param alert [AlertManagement::Alert]
# @param user [User]
def initialize(alert, user)
......@@ -21,6 +23,8 @@ module AlertManagement
issue = result.payload[:issue]
return error(object_errors(alert), issue) unless associate_alert_with_issue(issue)
update_title_for(issue)
SystemNoteService.new_alert_issue(alert, issue, user)
result
......@@ -50,6 +54,12 @@ module AlertManagement
alert.update(issue_id: issue.id)
end
def update_title_for(issue)
return unless issue.title == DEFAULT_ALERT_TITLE
issue.update!(title: _('New: Incident %{iid}' % { iid: issue.iid }))
end
def error(message, issue = nil)
ServiceResponse.error(payload: { issue: issue }, message: message)
end
......
---
title: 'Update default title of alerts to New: Alert'
merge_request: 54621
author:
type: changed
......@@ -79,7 +79,7 @@ to configure alerts for this integration.
## Customize the alert payload outside of GitLab
For all integration types, you can customize the payload by sending the following
parameters. All fields are optional. If the incoming alert does not contain a value for the `Title` field, a default value of `New: Incident` will be applied.
parameters. All fields are optional. If the incoming alert does not contain a value for the `Title` field, a default value of `New: Alert` will be applied.
| Property | Type | Description |
| ------------------------- | --------------- | ----------- |
......
......@@ -67,8 +67,6 @@ module EE
validates :weight, allow_nil: true, numericality: { greater_than_or_equal_to: 0 }
validate :validate_confidential_epic
after_create :update_generic_alert_title, if: :generic_alert_with_default_title?
state_machine :state_id do
after_transition do |issue|
issue.refresh_blocking_and_blocked_issues_cache!
......@@ -263,15 +261,6 @@ module EE
@blocking_issues_ids ||= ::IssueLink.blocking_issue_ids_for(self)
end
def update_generic_alert_title
update(title: "#{title} #{iid}")
end
def generic_alert_with_default_title?
title == ::Gitlab::AlertManagement::Payload::Generic::DEFAULT_TITLE &&
author == ::User.alert_bot
end
def validate_confidential_epic
return unless epic
......
......@@ -4,8 +4,6 @@ module Gitlab
module AlertManagement
module Payload
class Cilium < Gitlab::AlertManagement::Payload::Generic
DEFAULT_TITLE = 'New: Alert'
attribute :description, paths: %w(flow verdict)
attribute :title, paths: %w(ciliumNetworkPolicy metadata name), fallback: -> { DEFAULT_TITLE }
......
......@@ -4,10 +4,10 @@ require 'spec_helper'
RSpec.describe Gitlab::AlertManagement::Payload::Cilium do
let_it_be(:project) { build_stubbed(:project) }
let(:raw_payload) { build(:network_alert_payload).to_json }
let(:raw_payload) { build(:network_alert_payload) }
let(:parsed_payload) do
described_class.new(project: project, payload: Gitlab::Json.parse(raw_payload))
described_class.new(project: project, payload: Gitlab::Json.parse(raw_payload.to_json))
end
it 'parses cilium specific fields' do
......@@ -15,4 +15,14 @@ RSpec.describe Gitlab::AlertManagement::Payload::Cilium do
expect(parsed_payload.description).to eq('POLICY_DENIED')
expect(parsed_payload.gitlab_fingerprint).to eq('b2ad2a791756abe01692270c6a846129a09891b3')
end
context 'when title is not provided' do
before do
raw_payload[:ciliumNetworkPolicy][:metadata][:name] = nil
end
it 'uses a fallback title' do
expect(parsed_payload.title).to eq('New: Alert')
end
end
end
......@@ -22,39 +22,6 @@ RSpec.describe Issue do
it { is_expected.to include_module(EE::WeightEventable) }
end
context 'callbacks' do
describe '.after_create' do
let_it_be(:project) { create(:project) }
let(:author) { User.alert_bot }
context 'when issue title is "New: Incident"' do
let(:issue) { build(:issue, project: project, author: author, title: 'New: Incident', iid: 503503) }
context 'when the author is Alert Bot' do
it 'updates issue title with the IID' do
expect { issue.save }.to change { issue.title }.to("New: Incident 503503")
end
end
context 'when the author is not an Alert Bot' do
let(:author) { create(:user) }
it 'does not change issue title' do
expect { issue.save }.not_to change { issue.title }
end
end
end
context 'when issue title is not "New: Incident"' do
let(:issue) { build(:issue, project: project, title: 'Not New: Incident') }
it 'does not change issue title' do
expect { issue.save }.not_to change { issue.title }
end
end
end
end
context 'scopes' do
describe '.counts_by_health_status' do
it 'returns counts grouped by health_status' do
......
......@@ -5,7 +5,7 @@ module Gitlab
module AlertManagement
module Payload
class Generic < Base
DEFAULT_TITLE = 'New: Incident'
DEFAULT_TITLE = 'New: Alert'
attribute :description, paths: 'description'
attribute :ends_at, paths: 'end_time', type: :time
......
......@@ -20016,6 +20016,9 @@ msgstr ""
msgid "New..."
msgstr ""
msgid "New: Incident %{iid}"
msgstr ""
msgid "Newest first"
msgstr ""
......
[
{
"iid": "15",
"title": "New: Incident",
"title": "New: Alert",
"createdAt": "2020-06-03T15:46:08Z",
"assignees": {},
"state": "opened",
......
......@@ -13,7 +13,7 @@ RSpec.describe Gitlab::AlertManagement::Payload::Generic do
describe '#title' do
subject { parsed_payload.title }
it_behaves_like 'parsable alert payload field with fallback', 'New: Incident', 'title'
it_behaves_like 'parsable alert payload field with fallback', 'New: Alert', 'title'
end
describe '#severity' do
......
......@@ -118,9 +118,36 @@ RSpec.describe AlertManagement::CreateAlertIssueService do
context 'when the alert is generic' do
let(:alert) { generic_alert }
let(:issue) { subject.payload[:issue] }
let(:default_alert_title) { described_class::DEFAULT_ALERT_TITLE }
it_behaves_like 'creating an alert issue'
it_behaves_like 'setting an issue attributes'
context 'when alert title matches the default title exactly' do
before do
generic_alert.update!(title: default_alert_title)
end
it 'updates issue title with the IID' do
execute
expect(created_issue.title).to eq("New: Incident #{created_issue.iid}")
end
end
context 'when the alert title contains the default title' do
let(:non_default_alert_title) { "Not #{default_alert_title}" }
before do
generic_alert.update!(title: non_default_alert_title)
end
it 'does not change issue title' do
execute
expect(created_issue.title).to eq(non_default_alert_title)
end
end
end
context 'when issue cannot be created' do
......
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