Skip to content

Commit

Permalink
Pulsetic Integration (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
armiiller authored Feb 6, 2024
1 parent 71f81e3 commit 486f7e8
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['2.7', '3.0']
ruby: ['2.7', '3.0', '3.1']
gemfile:
- rails_7
- rails_master
exclude:
- gemfile: rails_master
ruby: 2.7
- gemfile: rails_master
ruby: 3.0
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
BUNDLE_PATH_RELATIVE_TO_CWD: true
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.0
70 changes: 70 additions & 0 deletions app/models/pager_tree/integrations/pulsetic/v3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module PagerTree::Integrations
class Pulsetic::V3 < Integration
OPTIONS = []
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"

after_initialize do
end

def adapter_supports_incoming?
true
end

def adapter_supports_outgoing?
false
end

def adapter_incoming_can_defer?
true
end

def adapter_thirdparty_id
id = adapter_incoming_request_params.dig("monitor", "id")
typ = _alert_type == "certificate_expires_soon" ? "certificate" : "monitor"
"#{id}_#{typ}"
end

def adapter_action
case _alert_type
when "monitor_offline", "certificate_expires_soon"
:create
when "monitor_online"
:resolve
else
:other
end
end

def adapter_process_create
Alert.new(
title: _title,
thirdparty_id: adapter_thirdparty_id,
additional_data: _additional_datums
)
end

private

def _title
case _alert_type
when "monitor_offline"
"#{adapter_incoming_request_params.dig("monitor", "url")} OFFLINE"
when "certificate_expires_soon"
"#{adapter_incoming_request_params.dig("monitor", "url")} CERTIFICATE EXPIRES SOON"
else
"[PULSETIC] UNKNOWN ALERT TYPE"
end
end

def _alert_type
adapter_incoming_request_params.dig("alert_type")
end

def _additional_datums
[
AdditionalDatum.new(format: "link", label: "Monitor", value: "https://app.pulsetic.com/monitors/#{adapter_incoming_request_params.dig("monitor", "id")}/overview"),
AdditionalDatum.new(format: "link", label: "URL", value: adapter_incoming_request_params.dig("monitor", "url"))
]
end
end
end
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions test/fixtures/pager_tree/integrations/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ prtg_v3:
type: "PagerTree::Integrations::Prtg::V3"
# options: no_options

pulsetic_v3:
type: "PagerTree::Integrations::Pulsetic::V3"
# options: no_options

sentry_v3:
type: "PagerTree::Integrations::Sentry::V3"
# options: no_options
Expand Down
94 changes: 94 additions & 0 deletions test/models/pager_tree/integrations/pulsetic/v3_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require "test_helper"

module PagerTree::Integrations
class Pulsetic::V3Test < ActiveSupport::TestCase
include Integrateable

setup do
@integration = pager_tree_integrations_integrations(:pulsetic_v3)

@create_request = {
alert_type: "monitor_offline",
monitor: {
id: 123456,
url: "https://statuscode.app/200"
}
}.with_indifferent_access

@resolve_request = @create_request.deep_dup
@resolve_request[:alert_type] = "monitor_online"

@certificate_request = @create_request.deep_dup
@certificate_request[:alert_type] = "certificate_expires_soon"
@certificate_request[:days_left] = 5

@other_request = @create_request.deep_dup
@other_request[:alert_type] = "baaad"
end

test "sanity" do
assert @integration.adapter_supports_incoming?
assert @integration.adapter_incoming_can_defer?
assert_not @integration.adapter_supports_outgoing?
assert @integration.adapter_show_alerts?
assert @integration.adapter_show_logs?
assert_not @integration.adapter_show_outgoing_webhook_delivery?
end

test "adapter_actions" do
@integration.adapter_incoming_request_params = @create_request
assert_equal :create, @integration.adapter_action

@integration.adapter_incoming_request_params = @resolve_request
assert_equal :resolve, @integration.adapter_action

@integration.adapter_incoming_request_params = @certificate_request
assert_equal :create, @integration.adapter_action

@integration.adapter_incoming_request_params = @other_request
assert_equal :other, @integration.adapter_action
end

test "adapter_thirdparty_id" do
@integration.adapter_incoming_request_params = @create_request
assert_equal "123456_monitor", @integration.adapter_thirdparty_id

@integration.adapter_incoming_request_params = @certificate_request
assert_equal "123456_certificate", @integration.adapter_thirdparty_id
end

test "adapter_process_create_monitor" do
@integration.adapter_incoming_request_params = @create_request

true_alert = Alert.new(
title: "https://statuscode.app/200 OFFLINE",
urgency: nil,
thirdparty_id: "123456_monitor",
dedup_keys: [],
additional_data: [
AdditionalDatum.new(format: "link", label: "Monitor", value: "https://app.pulsetic.com/monitors/123456/overview"),
AdditionalDatum.new(format: "link", label: "URL", value: "https://statuscode.app/200")
]
)

assert_equal true_alert.to_json, @integration.adapter_process_create.to_json
end

test "adapter_process_create_certificate" do
@integration.adapter_incoming_request_params = @certificate_request

true_alert = Alert.new(
title: "https://statuscode.app/200 CERTIFICATE EXPIRES SOON",
urgency: nil,
thirdparty_id: "123456_certificate",
dedup_keys: [],
additional_data: [
AdditionalDatum.new(format: "link", label: "Monitor", value: "https://app.pulsetic.com/monitors/123456/overview"),
AdditionalDatum.new(format: "link", label: "URL", value: "https://statuscode.app/200")
]
)

assert_equal true_alert.to_json, @integration.adapter_process_create.to_json
end
end
end

0 comments on commit 486f7e8

Please sign in to comment.