Commit 55f395ea authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add support of default fields of a generic alert

The parser looks for the list of fields to paste them
in an issue's summary:

* start_time
* description
* monitoring_tool
* service
* hosts

If any of the fields are not present, they will be excluded.

If the title field is missing
the parser will set the field to the default value.
parent 893584d8
......@@ -3,6 +3,8 @@
module Gitlab
module Alerting
class NotificationPayloadParser
DEFAULT_TITLE = 'New: Incident'
def initialize(payload)
@payload = payload.to_h.with_indifferent_access
end
......@@ -13,16 +15,32 @@ module Gitlab
def call
{
'annotations' => {
'title' => payload[:title]
},
'startsAt' => payload[:starts_at]
}
'annotations' => annotations,
'startsAt' => payload[:start_time]
}.compact
end
private
attr_reader :payload
def title
payload[:title].presence || DEFAULT_TITLE
end
def annotations
{
'title' => title,
'description' => payload[:description],
'monitoring_tool' => payload[:monitoring_tool],
'service' => payload[:service],
'hosts' => hosts
}.compact
end
def hosts
payload[:hosts] && Array(payload[:hosts])
end
end
end
end
......@@ -10,7 +10,11 @@ describe Gitlab::Alerting::NotificationPayloadParser do
let(:payload) do
{
'title' => 'alert title',
'starts_at' => starts_at.rfc3339
'start_time' => starts_at.rfc3339,
'description' => 'Description',
'monitoring_tool' => 'Monitoring tool name',
'service' => 'Service',
'hosts' => ['gitlab.com']
}
end
......@@ -18,11 +22,45 @@ describe Gitlab::Alerting::NotificationPayloadParser do
is_expected.to eq(
{
'annotations' => {
'title' => 'alert title'
'title' => 'alert title',
'description' => 'Description',
'monitoring_tool' => 'Monitoring tool name',
'service' => 'Service',
'hosts' => ['gitlab.com']
},
'startsAt' => starts_at.rfc3339
}
)
end
context 'when title is blank' do
before do
payload[:title] = ''
end
it 'sets a predefined title' do
expect(subject['annotations']['title']).to eq('New: Incident')
end
end
context 'when hosts attribute is a string' do
before do
payload[:hosts] = 'gitlab.com'
end
it 'returns hosts as an array of one element' do
expect(subject['annotations']['hosts']).to eq(['gitlab.com'])
end
end
context 'when payload is blank' do
let(:payload) { {} }
it 'returns only default title' do
is_expected.to eq(
'annotations' => { 'title' => 'New: Incident' }
)
end
end
end
end
......@@ -18,7 +18,7 @@ describe Projects::Alerting::NotifyService do
let(:payload_raw) do
{
'title' => 'alert title',
'starts_at' => starts_at.rfc3339
'start_time' => starts_at.rfc3339
}
end
let(:payload) { ActionController::Parameters.new(payload_raw).permit! }
......
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