debian-mirror-gitlab/lib/pager_duty/webhook_payload_parser.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
1.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module PagerDuty
class WebhookPayloadParser
2021-04-17 20:07:23 +05:30
SCHEMA_PATH = Rails.root.join('lib', 'pager_duty', 'validator', 'schemas', 'message.json')
2021-01-03 14:25:43 +05:30
2020-07-28 23:09:34 +05:30
def initialize(payload)
@payload = payload
end
def self.call(payload)
new(payload).call
end
def call
2023-03-04 22:38:38 +05:30
parse_message(payload)
2020-07-28 23:09:34 +05:30
end
private
attr_reader :payload
def parse_message(message)
2021-01-03 14:25:43 +05:30
return {} unless valid_message?(message)
2020-07-28 23:09:34 +05:30
{
2023-03-04 22:38:38 +05:30
'event' => message.dig('event', 'event_type'),
'incident' => parse_incident(message.dig('event', 'data'))
2020-07-28 23:09:34 +05:30
}
end
def parse_incident(incident)
2023-03-04 22:38:38 +05:30
return {} unless incident
2020-07-28 23:09:34 +05:30
{
'url' => incident['html_url'],
2023-03-04 22:38:38 +05:30
'incident_number' => incident['number'],
2020-07-28 23:09:34 +05:30
'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)),
2023-03-04 22:38:38 +05:30
'impacted_service' => parse_impacted_service(incident)
2020-07-28 23:09:34 +05:30
}
end
def parse_assignees(incident)
2023-03-04 22:38:38 +05:30
return [] unless incident
Array(incident['assignees']).map do |a|
2020-07-28 23:09:34 +05:30
{
2023-03-04 22:38:38 +05:30
'summary' => a['summary'],
'url' => a['html_url']
2020-07-28 23:09:34 +05:30
}
end
end
2023-03-04 22:38:38 +05:30
def parse_impacted_service(incident)
return {} unless incident
return {} if incident.dig('service', 'summary').blank? && incident.dig('service', 'html_url').blank?
{
'summary' => incident.dig('service', 'summary'),
'url' => incident.dig('service', 'html_url')
}
2020-07-28 23:09:34 +05:30
end
def reject_empty(entities)
Array(entities).reject { |e| e['summary'].blank? && e['url'].blank? }
end
2021-01-03 14:25:43 +05:30
def valid_message?(message)
2021-04-17 20:07:23 +05:30
::JSONSchemer.schema(SCHEMA_PATH).valid?(message)
2021-01-03 14:25:43 +05:30
end
2020-07-28 23:09:34 +05:30
end
end