Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for a going on/off call webhook #120

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/models/pager_tree/integrations/outgoing_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OutgoingEvent
attr_accessor :team
attr_accessor :account_user
attr_accessor :comment
attr_accessor :event_reminder

define_model_callbacks :initialize

Expand All @@ -33,6 +34,7 @@ def initialize(params = {})
self.team ||= nil
self.account_user ||= nil
self.comment ||= nil
self.event_reminder ||= nil
end
end
end
67 changes: 56 additions & 11 deletions app/models/pager_tree/integrations/outgoing_webhook/v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class OutgoingWebhook::V3 < Integration
{key: :alert_resolved, type: :boolean, default: false},
{key: :alert_dropped, type: :boolean, default: false},
{key: :alert_handoff, type: :boolean, default: false},
{key: :event_reminder_going_on_call, type: :boolean, default: false},
{key: :event_reminder_going_off_call, type: :boolean, default: false},
{key: :template, type: :string, default: nil},
{key: :send_linked, type: :boolean, default: false},
{key: :outgoing_rules, type: :string, default: nil}
Expand All @@ -27,6 +29,8 @@ class OutgoingWebhook::V3 < Integration
validates :option_alert_resolved, inclusion: {in: [true, false]}
validates :option_alert_dropped, inclusion: {in: [true, false]}
validates :option_alert_handoff, inclusion: {in: [true, false]}
validates :option_event_reminder_going_on_call, inclusion: {in: [true, false]}
validates :option_event_reminder_going_off_call, inclusion: {in: [true, false]}
validates :option_send_linked, inclusion: {in: [true, false]}

after_initialize do
Expand All @@ -38,6 +42,8 @@ class OutgoingWebhook::V3 < Integration
self.option_alert_resolved ||= false
self.option_alert_dropped ||= false
self.option_alert_handoff ||= false
self.option_event_reminder_going_on_call ||= false
self.option_event_reminder_going_off_call ||= false
self.option_send_linked ||= false
self.option_template ||= ""
self.option_outgoing_rules ||= ""
Expand Down Expand Up @@ -73,28 +79,58 @@ def adapter_outgoing_interest?(event_name)

def adapter_process_outgoing
body = nil
event_type = adapter_outgoing_event.event_name.to_s.tr("_", ".")
event_type = _map_event_name(adapter_outgoing_event.event_name)
handlebars_data = {
alert: adapter_outgoing_event.alert&.try(:v3_format) || adapter_outgoing_event&.alert,
event: {
type: event_type
}
}.compact

# Do the custom templating portion for outgoing hooks
if self.option_template.present?
begin
body = JSON.parse(handlebars(self.option_template, {
alert: adapter_outgoing_event.alert.try(:v3_format) || adapter_outgoing_event.alert,
event: {
type: event_type
}
}))
body = JSON.parse(handlebars(self.option_template, handlebars_data))
rescue JSON::ParserError => e
logs.create(message: "Error parsing JSON, abort custom format for option template. Error: #{e.message}")
rescue => e
Rails.logger.error "Error while processing option_template for #{id}: #{e.message}"
end
end

body ||= {
data: adapter_outgoing_event.alert.try(:v3_format) || adapter_outgoing_event.alert,
type: event_type
}
if !body.present?
body = {}
body[:type] = event_type
body[:data] = case event_type
when "event_reminder.going_on_call", "event_reminder.going_off_call"
{
id: adapter_outgoing_event.event_reminder.id,
prefix_id: adapter_outgoing_event.event_reminder.prefix_id,
event: {
id: adapter_outgoing_event.event_reminder.event.id,
prefix_id: adapter_outgoing_event.event_reminder.event.prefix_id,
start_time: adapter_outgoing_event.event_reminder.next_start.in_time_zone(adapter_outgoing_event.event_reminder.event.time_zone),
end_time: adapter_outgoing_event.event_reminder.next_end.in_time_zone(adapter_outgoing_event.event_reminder.event.time_zone),
time_zone: adapter_outgoing_event.event_reminder.event.time_zone,
iana_time_zone: adapter_outgoing_event.event_reminder.event.iana_time_zone,
layer: adapter_outgoing_event.event_reminder.event.layer,
teams: adapter_outgoing_event.event_reminder.event.teams.map { |x| {id: x.id, tiny_id: x.tiny_id, prefix_id: x.prefix_id, name: x.name} },
account_user: {
id: adapter_outgoing_event.event_reminder.account_user.id,
prefix_id: adapter_outgoing_event.event_reminder.account_user.prefix_id,
user: {
id: adapter_outgoing_event.event_reminder.account_user.user.id,
prefix_id: adapter_outgoing_event.event_reminder.account_user.user.prefix_id,
name: adapter_outgoing_event.event_reminder.account_user.user.name,
email: adapter_outgoing_event.event_reminder.account_user.user.email
}
}
}
}
else
adapter_outgoing_event.alert&.try(:v3_format) || adapter_outgoing_event&.alert
end
end

url = adapter_outgoing_event.outgoing_rules_data.dig("webhook_url") || option_webhook_url
body.merge!(adapter_outgoing_event.outgoing_rules_data.except("webhook_url"))
Expand All @@ -110,5 +146,14 @@ def adapter_process_outgoing

outgoing_webhook_delivery
end

def _map_event_name(event_name)
case event_name
when "event_reminder_going_on_call" then "event_reminder.going_on_call"
when "event_reminder_going_off_call" then "event_reminder.going_off_call"
else
event_name.to_s.tr("_", ".")
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
:alert_resolved,
:alert_dropped,
:alert_handoff,
:event_reminder_going_on_call,
:event_reminder_going_off_call,
:send_linked
]
%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
:alert_resolved,
:alert_dropped,
:alert_handoff,
:event_reminder_going_on_call,
:event_reminder_going_off_call,
]
%>
<% opts.each do |opt| %>
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ en:
option_alert_resolved_hint_html: "Send when the alert is marked resolved"
option_alert_dropped_hint_html: "Send when the alert is dropped"
option_alert_handoff_hint_html: "Send when the alert has been handed off"
option_event_reminder_going_on_call_hint_html: "Send when a user is going on call"
option_event_reminder_going_off_call_hint_html: "Send when a user is going off call"
option_template_hint_html: "A handlebars template describing the body that should be posted. See <a href='https://pagertree.com/docs/integration-guides/outgoing-webhook#custom-format' target='_blank'>docs</a> for details."
option_send_linked_hint_html: "Send linked data (source, source_log, user, team)"
channel:
Expand Down Expand Up @@ -143,6 +145,8 @@ en:
option_alert_resolved: "alert.resolved"
option_alert_dropped: "alert.dropped"
option_alert_handoff: "alert.handoff"
option_event_reminder_going_on_call: "event_reminder.going_on_call"
option_event_reminder_going_off_call: "event_reminder.going_off_call"
option_outgoing_rules: "Outgoing Rules"
"pager_tree/integrations/apex_ping/v3":
option_api_key: "API Key"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OutgoingWebhook::V3Test < ActiveSupport::TestCase

assert_equal @integration.option_webhook_url, outgoing_webhook_delivery.url
assert_equal :queued.to_s, outgoing_webhook_delivery.status
assert_equal expected_payload.to_json, outgoing_webhook_delivery.body.to_json
assert_equal expected_payload.as_json, outgoing_webhook_delivery.body.as_json
end

test "basic authorization works" do
Expand Down