Commit 2b607f09 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'split-summary-and-alert-details-params' into 'master'

Split summary and alert details params

Closes #31883

See merge request gitlab-org/gitlab!18261
parents 554414f5 bf85c3f7
...@@ -4,6 +4,7 @@ module Projects ...@@ -4,6 +4,7 @@ module Projects
module Prometheus module Prometheus
class AlertPresenter < Gitlab::View::Presenter::Delegated class AlertPresenter < Gitlab::View::Presenter::Delegated
RESERVED_ANNOTATIONS = %w(gitlab_incident_markdown).freeze RESERVED_ANNOTATIONS = %w(gitlab_incident_markdown).freeze
GENERIC_ALERT_SUMMARY_ANNOTATIONS = %w(monitoring_tool service hosts).freeze
MARKDOWN_LINE_BREAK = " \n".freeze MARKDOWN_LINE_BREAK = " \n".freeze
def full_title def full_title
...@@ -58,8 +59,11 @@ module Projects ...@@ -58,8 +59,11 @@ module Projects
def metadata_list def metadata_list
metadata = [] metadata = []
metadata << list_item('starts_at', starts_at) if starts_at metadata << list_item('Start time', starts_at) if starts_at
metadata << list_item('full_query', backtick(full_query)) if full_query metadata << list_item('full_query', backtick(full_query)) if full_query
metadata << list_item(service.label.humanize, service.value) if service
metadata << list_item(monitoring_tool.label.humanize, monitoring_tool.value) if monitoring_tool
metadata << list_item(hosts.label.humanize, host_links) if hosts
metadata.join(MARKDOWN_LINE_BREAK) metadata.join(MARKDOWN_LINE_BREAK)
end end
...@@ -78,7 +82,7 @@ module Projects ...@@ -78,7 +82,7 @@ module Projects
def annotation_list def annotation_list
strong_memoize(:annotation_list) do strong_memoize(:annotation_list) do
annotations annotations
.reject { |annotation| RESERVED_ANNOTATIONS.include?(annotation.label) } .reject { |annotation| annotation.label.in?(RESERVED_ANNOTATIONS | GENERIC_ALERT_SUMMARY_ANNOTATIONS) }
.map { |annotation| list_item(annotation.label, annotation.value) } .map { |annotation| list_item(annotation.label, annotation.value) }
.join(MARKDOWN_LINE_BREAK) .join(MARKDOWN_LINE_BREAK)
end end
...@@ -91,6 +95,16 @@ module Projects ...@@ -91,6 +95,16 @@ module Projects
def backtick(value) def backtick(value)
"`#{value}`" "`#{value}`"
end end
GENERIC_ALERT_SUMMARY_ANNOTATIONS.each do |annotation_name|
define_method(annotation_name) do
annotations.find { |a| a.label == annotation_name }
end
end
def host_links
Array(hosts.value).join(' ')
end
end end
end end
end end
...@@ -37,6 +37,8 @@ describe Projects::Prometheus::AlertPresenter do ...@@ -37,6 +37,8 @@ describe Projects::Prometheus::AlertPresenter do
end end
describe '#issue_summary_markdown' do describe '#issue_summary_markdown' do
let(:markdown_line_break) { ' ' }
subject { presenter.issue_summary_markdown } subject { presenter.issue_summary_markdown }
context 'without default payload' do context 'without default payload' do
...@@ -45,7 +47,7 @@ describe Projects::Prometheus::AlertPresenter do ...@@ -45,7 +47,7 @@ describe Projects::Prometheus::AlertPresenter do
<<~MARKDOWN.chomp <<~MARKDOWN.chomp
#### Summary #### Summary
**starts_at:** #{presenter.starts_at} **Start time:** #{presenter.starts_at}
MARKDOWN MARKDOWN
) )
...@@ -62,11 +64,12 @@ describe Projects::Prometheus::AlertPresenter do ...@@ -62,11 +64,12 @@ describe Projects::Prometheus::AlertPresenter do
<<~MARKDOWN.chomp <<~MARKDOWN.chomp
#### Summary #### Summary
**starts_at:** #{presenter.starts_at} **Start time:** #{presenter.starts_at}
#### Alert Details #### Alert Details
**foo:** value1 \n**bar:** value2 **foo:** value1#{markdown_line_break}
**bar:** value2
MARKDOWN MARKDOWN
) )
end end
...@@ -82,11 +85,64 @@ describe Projects::Prometheus::AlertPresenter do ...@@ -82,11 +85,64 @@ describe Projects::Prometheus::AlertPresenter do
<<~MARKDOWN.chomp <<~MARKDOWN.chomp
#### Summary #### Summary
**starts_at:** #{presenter.starts_at} \n**full_query:** `query` **Start time:** #{presenter.starts_at}#{markdown_line_break}
**full_query:** `query`
MARKDOWN
)
end
end
context 'with the Generic Alert parameters' do
let(:generic_alert_params) do
{
'title' => 'The Generic Alert Title',
'description' => 'The Generic Alert Description',
'monitoring_tool' => 'monitoring_tool_name',
'service' => 'service_name',
'hosts' => ['http://localhost:3000', 'http://localhost:3001']
}
end
before do
payload['annotations'] = generic_alert_params
end
it do
is_expected.to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**Service:** service_name#{markdown_line_break}
**Monitoring tool:** monitoring_tool_name#{markdown_line_break}
**Hosts:** http://localhost:3000 http://localhost:3001
#### Alert Details
**title:** The Generic Alert Title#{markdown_line_break}
**description:** The Generic Alert Description
MARKDOWN MARKDOWN
) )
end end
context 'when hosts is a string' do
before do
payload['annotations'] = { 'hosts' => 'http://localhost:3000' }
end
it do
is_expected.to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**Hosts:** http://localhost:3000
MARKDOWN
)
end
end
end end
end end
......
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