Commit a58295c3 authored by syasonik's avatar syasonik

Support an alert template field to allow for customization

parent c833bf01
......@@ -3,6 +3,8 @@
module Projects
module Prometheus
class AlertPresenter < Gitlab::View::Presenter::Delegated
RESERVED_ANNOTATIONS = %w(gitlab_incident_markdown)
def full_title
[environment_name, alert_title].compact.join(': ')
end
......@@ -40,6 +42,10 @@ module Projects
MARKDOWN
end
def alert_markdown
annotations.find { |annotation| annotation.label == 'gitlab_incident_markdown' }&.value
end
private
def alert_title
......@@ -64,6 +70,7 @@ module Projects
def annotation_list
strong_memoize(:annotation_list) do
annotations
.reject { |annotation| RESERVED_ANNOTATIONS.include?(annotation.label) }
.map { |annotation| bullet(annotation.label, annotation.value) }
.join("\n")
end
......
......@@ -33,17 +33,23 @@ module IncidentManagement
end
def issue_description
return alert_summary unless issue_template_content
horizontal_line = "\n---\n\n"
alert_summary + horizontal_line + issue_template_content
[
alert_summary,
alert_markdown,
issue_template_content
].compact.join(horizontal_line)
end
def alert_summary
alert.issue_summary_markdown
end
def alert_markdown
alert.alert_markdown
end
def alert
strong_memoize(:alert) do
Gitlab::Alerting::Alert.new(project: project, payload: params).present
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
describe IncidentManagement::CreateIssueService do
let(:project) { create(:project, :repository) }
set(:project) { create(:project, :repository) }
let(:service) { described_class.new(project, nil, alert_payload) }
let(:alert_starts_at) { Time.now }
let(:alert_title) { 'TITLE' }
......@@ -20,7 +20,7 @@ describe IncidentManagement::CreateIssueService do
Gitlab::Alerting::Alert.new(project: project, payload: alert_payload).present
end
let!(:setting) do
set(:setting) do
create(:project_incident_management_setting, project: project)
end
......@@ -45,21 +45,16 @@ describe IncidentManagement::CreateIssueService do
end
end
context 'with issue_template_content' do
before do
create_issue_template('bug', issue_template_content)
setting.update!(issue_template_key: 'bug')
end
shared_examples 'GFM template' do
context 'plain content' do
let(:issue_template_content) { 'some content' }
let(:template_content) { 'some content' }
it 'creates an issue appending issue template' do
expect(subject).to include(status: :success)
expect(issue.description).to include(alert_presenter.issue_summary_markdown)
expect(issue.description).to include(summary_separator)
expect(issue.description).to include(issue_template_content)
expect(issue.description).to include(template_content)
end
end
......@@ -67,7 +62,7 @@ describe IncidentManagement::CreateIssueService do
let(:user) { create(:user) }
let(:plain_text) { 'some content' }
let(:issue_template_content) do
let(:template_content) do
<<~CONTENT
#{plain_text}
/due tomorrow
......@@ -87,6 +82,40 @@ describe IncidentManagement::CreateIssueService do
expect(issue.assignees).to eq([user])
end
end
end
context 'with gitlab_incident_markdown' do
let(:alert_annotations) do
{ title: alert_title, gitlab_incident_markdown: template_content }
end
it_behaves_like 'GFM template'
end
context 'with issue_template_content' do
before do
create_issue_template('bug', template_content)
setting.update!(issue_template_key: 'bug')
end
it_behaves_like 'GFM template'
context 'and gitlab_incident_markdown' do
let(:template_content) { 'plain text'}
let(:alt_template) { 'alernate text' }
let(:alert_annotations) do
{ title: alert_title, gitlab_incident_markdown: alt_template }
end
it 'includes both templates' do
expect(subject).to include(status: :success)
expect(issue.description).to include(alert_presenter.issue_summary_markdown)
expect(issue.description).to include(template_content)
expect(issue.description).to include(alt_template)
expect(issue.description.count(summary_separator)).to eq(2)
end
end
private
......
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