diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /lib/pager_duty | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'lib/pager_duty')
-rw-r--r-- | lib/pager_duty/webhook_payload_parser.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/pager_duty/webhook_payload_parser.rb b/lib/pager_duty/webhook_payload_parser.rb new file mode 100644 index 00000000000..573fb36f0ca --- /dev/null +++ b/lib/pager_duty/webhook_payload_parser.rb @@ -0,0 +1,66 @@ +# 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 |