Class: Sentry::V3

Inherits:
Integration
  • Object
show all
Defined in:
app/models/pager_tree/integrations/sentry/v3.rb

Constant Summary collapse

OPTIONS =
[
  {key: :client_secret, type: :string, default: nil},
  {key: :authorization_token, type: :string, default: nil},
  {key: :authorization_token_expires_at, type: :string, default: nil},
  {key: :authorization_refresh_token, type: :string, default: nil},
  {key: :code, type: :string, default: nil}
]

Instance Method Summary collapse

Instance Method Details

#adapter_actionObject



49
50
51
52
53
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 49

def adapter_action
  return :other unless should_process?

  send("_#{hook_resource}_adapter_action")
end

#adapter_incoming_can_defer?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 41

def adapter_incoming_can_defer?
  true
end

#adapter_process_createObject



55
56
57
58
59
60
61
62
63
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 55

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

#adapter_process_otherObject



65
66
67
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 65

def adapter_process_other
  _installation_process_other if installation?
end

#adapter_should_block_incoming?(request) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 19

def adapter_should_block_incoming?(request)
  should_block = false
  # https://docs.sentry.io/product/integrations/integration-platform/webhooks/#sentry-hook-signature
  client_secret = option_client_secret.presence || PagerTree::Integrations.integration_sentry_v3_client_secret
  if client_secret.present? && request.headers["sentry-hook-signature"].present?
    sentry_signature = request.headers["sentry-hook-signature"]
    data = request.body.read
    digest = OpenSSL::HMAC.hexdigest("SHA256", client_secret, data)
    should_block = sentry_signature != digest
  end

  should_block
end

#adapter_supports_incoming?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 33

def adapter_supports_incoming?
  true
end

#adapter_supports_outgoing?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 37

def adapter_supports_outgoing?
  false
end

#adapter_thirdparty_idObject



45
46
47
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 45

def adapter_thirdparty_id
  send("_#{hook_resource}_adapter_thirdparty_id")
end

#initialize_authorization_token!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 88

def initialize_authorization_token!
  if thirdparty_id.present? && option_code.present?
    response = HTTParty.post("https://sentry.io/api/0/sentry-app-installations/#{thirdparty_id}/authorizations/",
      headers: {
        "Content-Type" => "application/json"
      }, body: {
        grant_type: "authorization_code",
        code: option_code,
        client_id: PagerTree::Integrations.integration_sentry_v3_client_id,
        client_secret: PagerTree::Integrations.integration_sentry_v3_client_secret
      }.to_json)

    if response.code == 201
      json = JSON.parse(response.body)
      self.option_authorization_token = json.dig("token")
      self.option_authorization_refresh_token = json.dig("refreshToken")
      self.option_authorization_token_expires_at = json.dig("expiresAt")
      save!

      return true
    end
  end

  false
rescue => e
  Rails.logger.error("Error initializing Sentry App Authorization Token: #{e.message}")
  false
end

#refresh_authorization_token!Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 117

def refresh_authorization_token!
  if thirdparty_id.present? && option_authorization_refresh_token.present?
    response = HTTParty.post("https://sentry.io/api/0/sentry-app-installations/#{thirdparty_id}/authorizations/",
      headers: {
        "Content-Type" => "application/json"
      }, body: {
        grant_type: "refresh_token",
        refresh_token: option_authorization_refresh_token,
        client_id: PagerTree::Integrations.integration_sentry_v3_client_id,
        client_secret: PagerTree::Integrations.integration_sentry_v3_client_secret
      }.to_json)

    if response.code == 201
      json = JSON.parse(response.body)
      self.option_authorization_token = json.dig("token")
      self.option_authorization_refresh_token = json.dig("refreshToken")
      self.option_authorization_token_expires_at = json.dig("expiresAt")
      save!

      return true
    end
  end

  false
rescue => e
  Rails.logger.error("Error refreshing Sentry App Authorization Token: #{e.message}")
  false
end

#verify_installationObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/pager_tree/integrations/sentry/v3.rb', line 69

def verify_installation
  if thirdparty_id.present?
    HTTParty.put("https://sentry.io/api/0/sentry-app-installations/#{thirdparty_id}/",
      headers: {
        "Content-Type" => "application/json",
        "Authorization" => "Bearer #{option_authorization_token}"
      }, body: {
        status: "installed"
      }.to_json)

    return true
  end

  false
rescue => e
  Rails.logger.error("Error sending Sentry App Installation Confirmation: #{e.message}")
  false
end