Commit 893584d8 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add a generic alert notification parser

As a prototype, the parser will turn initial payload into a Prometheus
specific format.
That helps us reuse the existing implementation of creating issues based
on Prometheus alerts.
Follow up changes will extend the parser to more fields.
parent b42b1b2b
...@@ -27,7 +27,11 @@ module Projects ...@@ -27,7 +27,11 @@ module Projects
def process_incident_issues def process_incident_issues
IncidentManagement::ProcessAlertWorker IncidentManagement::ProcessAlertWorker
.perform_async(project.id, params.to_h) .perform_async(project.id, parsed_payload)
end
def parsed_payload
Gitlab::Alerting::NotificationPayloadParser.call(params.to_h)
end end
end end
end end
......
# frozen_string_literal: true
module Gitlab
module Alerting
class NotificationPayloadParser
def initialize(payload)
@payload = payload.to_h.with_indifferent_access
end
def self.call(payload)
new(payload).call
end
def call
{
'annotations' => {
'title' => payload[:title]
},
'startsAt' => payload[:starts_at]
}
end
private
attr_reader :payload
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
describe Gitlab::Alerting::NotificationPayloadParser do
describe '.call' do
subject { described_class.call(payload) }
let(:starts_at) { Time.now.change(usec: 0) }
let(:payload) do
{
'title' => 'alert title',
'starts_at' => starts_at.rfc3339
}
end
it 'returns Prometheus-like payload' do
is_expected.to eq(
{
'annotations' => {
'title' => 'alert title'
},
'startsAt' => starts_at.rfc3339
}
)
end
end
end
...@@ -17,10 +17,8 @@ describe Projects::Alerting::NotifyService do ...@@ -17,10 +17,8 @@ describe Projects::Alerting::NotifyService do
let(:service) { described_class.new(project, nil, payload) } let(:service) { described_class.new(project, nil, payload) }
let(:payload_raw) do let(:payload_raw) do
{ {
'annotations' => { 'title' => 'alert title',
'title' => 'annotation title' 'starts_at' => starts_at.rfc3339
},
'startsAt' => starts_at.rfc3339
} }
end end
let(:payload) { ActionController::Parameters.new(payload_raw).permit! } 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