diff options
Diffstat (limited to 'lib/pager_duty')
-rw-r--r-- | lib/pager_duty/validator/schemas/message.json | 47 | ||||
-rw-r--r-- | lib/pager_duty/webhook_payload_parser.rb | 12 |
2 files changed, 56 insertions, 3 deletions
diff --git a/lib/pager_duty/validator/schemas/message.json b/lib/pager_duty/validator/schemas/message.json new file mode 100644 index 00000000000..b1a3185cd1a --- /dev/null +++ b/lib/pager_duty/validator/schemas/message.json @@ -0,0 +1,47 @@ +{ + "type": "object", + "required": ["event", "incident"], + "properties": { + "event": { "type": "string" }, + "incident": { + "type": "object", + "required": [ + "html_url", + "incident_number", + "title", + "status", + "created_at", + "urgency", + "incident_key" + ], + "properties": { + "html_url": { "type": "string" }, + "incindent_number": { "type": "integer" }, + "title": { "type": "string" }, + "status": { "type": "string" }, + "created_at": { "type": "string" }, + "urgency": { "type": "string", "enum": ["high", "low"] }, + "incident_key": { "type": ["string", "null"] }, + "assignments": { + "type": "array", + "items": { + "assignee": { + "type": "array", + "items": { + "summary": { "type": "string" }, + "html_url": { "type": "string" } + } + } + } + }, + "impacted_services": { + "type": "array", + "items": { + "summary": { "type": "string" }, + "html_url": { "type": "string" } + } + } + } + } + } +} diff --git a/lib/pager_duty/webhook_payload_parser.rb b/lib/pager_duty/webhook_payload_parser.rb index 573fb36f0ca..11071926cf2 100644 --- a/lib/pager_duty/webhook_payload_parser.rb +++ b/lib/pager_duty/webhook_payload_parser.rb @@ -2,6 +2,8 @@ module PagerDuty class WebhookPayloadParser + SCHEMA_PATH = File.join('lib', 'pager_duty', 'validator', 'schemas', 'message.json') + def initialize(payload) @payload = payload end @@ -11,7 +13,7 @@ module PagerDuty end def call - Array(payload['messages']).map { |msg| parse_message(msg) } + Array(payload['messages']).map { |msg| parse_message(msg) }.reject(&:empty?) end private @@ -19,6 +21,8 @@ module PagerDuty attr_reader :payload def parse_message(message) + return {} unless valid_message?(message) + { 'event' => message['event'], 'incident' => parse_incident(message['incident']) @@ -26,8 +30,6 @@ module PagerDuty end def parse_incident(incident) - return {} if incident.blank? - { 'url' => incident['html_url'], 'incident_number' => incident['incident_number'], @@ -62,5 +64,9 @@ module PagerDuty def reject_empty(entities) Array(entities).reject { |e| e['summary'].blank? && e['url'].blank? } end + + def valid_message?(message) + ::JSONSchemer.schema(Pathname.new(SCHEMA_PATH)).valid?(message) + end end end |