Commit 007a6572 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Implement PagerDuty webhook payload parser

parent 4d8b81b3
# frozen_string_literal: true
module PagerDuty
class WebhookPayloadParser
def initialize(payload)
@payload = payload
end
def self.call(payload)
new(payload).call
end
def call
Array(payload['messages']).map { |msg| parse_message(msg) }
end
private
attr_reader :payload
def parse_message(message)
{
'event' => message['event'],
'incident' => parse_incident(message['incident'])
}
end
def parse_incident(incident)
return {} if incident.blank?
{
'url' => incident['html_url'],
'incident_number' => incident['incident_number'],
'title' => incident['title'],
'status' => incident['status'],
'created_at' => incident['created_at'],
'urgency' => incident['urgency'],
'incident_key' => incident['incident_key'],
'assignees' => reject_empty(parse_assignees(incident)),
'impacted_services' => reject_empty(parse_impacted_services(incident))
}
end
def parse_assignees(incident)
Array(incident['assignments']).map do |a|
{
'summary' => a.dig('assignee', 'summary'),
'url' => a.dig('assignee', 'html_url')
}
end
end
def parse_impacted_services(incident)
Array(incident['impacted_services']).map do |is|
{
'summary' => is['summary'],
'url' => is['html_url']
}
end
end
def reject_empty(entities)
Array(entities).reject { |e| e['summary'].blank? && e['url'].blank? }
end
end
end
{
"messages": [
{
"event": "incident.trigger",
"log_entries": [
{
"id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"type": "trigger_log_entry",
"summary": "Triggered through the website",
"self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"created_at": "2017-09-26T15:14:36Z",
"agent": {
"id": "P553OPV",
"type": "user_reference",
"summary": "Laura Haley",
"self": "https://api.pagerduty.com/users/P553OPV",
"html_url": "https://webdemo.pagerduty.com/users/P553OPV"
},
"channel": {
"type": "web_trigger",
"summary": "My new incident",
"subject": "My new incident",
"details": "Oh my gosh",
"details_omitted": false
},
"service": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"incident": {
"id": "PRORDTY",
"type": "incident_reference",
"summary": "[#33] My new incident",
"self": "https://api.pagerduty.com/incidents/PRORDTY",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY"
},
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"contexts": [],
"event_details": {
"description": "My new incident"
}
}
],
"webhook": {
"endpoint_url": "https://requestb.in/18ao6fs1",
"name": "V2 wabhook",
"description": null,
"webhook_object": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"config": {},
"outbound_integration": {
"id": "PJFWPEP",
"type": "outbound_integration_reference",
"summary": "Generic V2 Webhook",
"self": "https://api.pagerduty.com/outbound_integrations/PJFWPEP",
"html_url": null
},
"accounts_addon": null,
"id": "PKT9NNX",
"type": "webhook",
"summary": "V2 wabhook",
"self": "https://api.pagerduty.com/webhooks/PKT9NNX",
"html_url": null
},
"incident": {
"incident_number": 33,
"title": "My new incident",
"description": "My new incident",
"created_at": "2017-09-26T15:14:36Z",
"status": "triggered",
"pending_actions": [
{
"type": "escalate",
"at": "2017-09-26T15:44:36Z"
},
{
"type": "resolve",
"at": "2017-09-26T19:14:36Z"
}
],
"incident_key": null,
"service": {
"id": "PN49J75",
"name": "Production XDB Cluster",
"description": "This service was created during onboarding on July 5, 2017.",
"auto_resolve_timeout": 14400,
"acknowledgement_timeout": 1800,
"created_at": "2017-07-05T17:33:09Z",
"status": "critical",
"last_incident_timestamp": "2017-09-26T15:14:36Z",
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"incident_urgency_rule": {
"type": "constant",
"urgency": "high"
},
"scheduled_actions": [],
"support_hours": null,
"escalation_policy": {
"id": "PINYWEF",
"type": "escalation_policy_reference",
"summary": "Default",
"self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
"html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
},
"addons": [],
"privilege": null,
"alert_creation": "create_alerts_and_incidents",
"integrations": [
{
"id": "PUAYF96",
"type": "generic_events_api_inbound_integration_reference",
"summary": "API",
"self": "https://api.pagerduty.com/services/PN49J75/integrations/PUAYF96",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/PUAYF96"
},
{
"id": "P90GZUH",
"type": "generic_email_inbound_integration_reference",
"summary": "Email",
"self": "https://api.pagerduty.com/services/PN49J75/integrations/P90GZUH",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/P90GZUH"
}
],
"metadata": {},
"type": "service",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"assignments": [
{
"at": "2017-09-26T15:14:36Z",
"assignee": {
"id": "P553OPV",
"type": "user_reference",
"summary": "Laura Haley",
"self": "https://api.pagerduty.com/users/P553OPV",
"html_url": "https://webdemo.pagerduty.com/users/P553OPV"
}
}
],
"acknowledgements": [],
"last_status_change_at": "2017-09-26T15:14:36Z",
"last_status_change_by": {
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
},
"first_trigger_log_entry": {
"id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"type": "trigger_log_entry_reference",
"summary": "Triggered through the website",
"self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E"
},
"escalation_policy": {
"id": "PINYWEF",
"type": "escalation_policy_reference",
"summary": "Default",
"self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
"html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
},
"privilege": null,
"teams": [
{
"id": "P4SI59S",
"type": "team_reference",
"summary": "Engineering",
"self": "https://api.pagerduty.com/teams/P4SI59S",
"html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
}
],
"alert_counts": {
"all": 0,
"triggered": 0,
"resolved": 0
},
"impacted_services": [
{
"id": "PN49J75",
"type": "service_reference",
"summary": "Production XDB Cluster",
"self": "https://api.pagerduty.com/services/PN49J75",
"html_url": "https://webdemo.pagerduty.com/services/PN49J75"
}
],
"is_mergeable": true,
"basic_alert_grouping": null,
"alert_grouping": null,
"metadata": {},
"external_references": [],
"importance": null,
"incidents_responders": [],
"responder_requests": [],
"subscriber_requests": [],
"urgency": "high",
"id": "PRORDTY",
"type": "incident",
"summary": "[#33] My new incident",
"self": "https://api.pagerduty.com/incidents/PRORDTY",
"html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY",
"alerts": [
{
"alert_key": "c24117fc42e44b44b4d6876190583378"
}
]
},
"id": "69a7ced0-a2cd-11e7-a799-22000a15839c",
"created_on": "2017-09-26T15:14:36Z"
}
]
}
# frozen_string_literal: true
require 'fast_spec_helper'
RSpec.describe PagerDuty::WebhookPayloadParser do
describe '.call' do
let(:fixture_file) do
File.read(File.join(File.dirname(__FILE__), '../../fixtures/pager_duty/webhook_incident_trigger.json'))
end
subject(:parse) { described_class.call(payload) }
context 'when payload is a correct PagerDuty payload' do
let(:payload) { Gitlab::Json.parse(fixture_file) }
it 'returns parsed payload' do
is_expected.to eq(
[
{
'event' => 'incident.trigger',
'incident' => {
'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY',
'incident_number' => 33,
'title' => 'My new incident',
'status' => 'triggered',
'created_at' => '2017-09-26T15:14:36Z',
'urgency' => 'high',
'incident_key' => nil,
'assignees' => [{
'summary' => 'Laura Haley',
'url' => 'https://webdemo.pagerduty.com/users/P553OPV'
}],
'impacted_services' => [{
'summary' => 'Production XDB Cluster',
'url' => 'https://webdemo.pagerduty.com/services/PN49J75'
}]
}
}
]
)
end
context 'when assignments summary and html_url are blank' do
before do
payload['messages'].each do |m|
m['incident']['assignments'] = [{ 'assignee' => { 'summary' => '', 'html_url' => '' } }]
end
end
it 'returns parsed payload with blank assignees' do
assignees = parse.map { |events| events['incident'].slice('assignees') }
expect(assignees).to eq([{ 'assignees' => [] }])
end
end
context 'when impacted_services summary and html_url are blank' do
before do
payload['messages'].each do |m|
m['incident']['impacted_services'] = [{ 'summary' => '', 'html_url' => '' }]
end
end
it 'returns parsed payload with blank assignees' do
assignees = parse.map { |events| events['incident'].slice('impacted_services') }
expect(assignees).to eq([{ 'impacted_services' => [] }])
end
end
end
context 'when payload has no incidents' do
let(:payload) { { 'messages' => [{ 'event' => 'incident.trigger' }] } }
it 'returns payload with blank incidents' do
is_expected.to eq([{ 'event' => 'incident.trigger', 'incident' => {} }])
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