2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ErrorTracking
|
2019-05-30 16:15:17 +05:30
|
|
|
class ProjectErrorTrackingSetting < ActiveRecord::Base
|
2019-02-15 15:39:39 +05:30
|
|
|
include ReactiveCaching
|
|
|
|
|
|
|
|
self.reactive_cache_key = ->(setting) { [setting.class.model_name.singular, setting.project_id] }
|
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
validates :api_url, length: { maximum: 255 }, public_url: true, url: { enforce_sanitization: true, ascii_only: true }, allow_nil: true
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
validates :api_url, presence: true, if: :enabled
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
validate :validate_api_url_path, if: :enabled
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
validates :token, presence: true, if: :enabled
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
attr_encrypted :token,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
|
|
|
algorithm: 'aes-256-gcm'
|
|
|
|
|
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def project_name
|
|
|
|
super || project_name_from_slug
|
|
|
|
end
|
|
|
|
|
|
|
|
def organization_name
|
|
|
|
super || organization_name_from_slug
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_slug
|
|
|
|
project_slug_from_api_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def organization_slug
|
|
|
|
organization_slug_from_api_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_api_url_from(api_host:, project_slug:, organization_slug:)
|
|
|
|
uri = Addressable::URI.parse("#{api_host}/api/0/projects/#{organization_slug}/#{project_slug}/")
|
|
|
|
uri.path = uri.path.squeeze('/')
|
|
|
|
|
|
|
|
uri.to_s
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
api_host
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def sentry_client
|
|
|
|
Sentry::Client.new(api_url, token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sentry_external_url
|
|
|
|
self.class.extract_sentry_external_url(api_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_sentry_issues(opts = {})
|
|
|
|
with_reactive_cache('list_issues', opts.stringify_keys) do |result|
|
2019-05-30 16:15:17 +05:30
|
|
|
{ issues: result }
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def list_sentry_projects
|
|
|
|
{ projects: sentry_client.list_projects }
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def calculate_reactive_cache(request, opts)
|
|
|
|
case request
|
|
|
|
when 'list_issues'
|
2019-05-30 16:15:17 +05:30
|
|
|
sentry_client.list_issues(**opts.symbolize_keys)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# http://HOST/api/0/projects/ORG/PROJECT
|
|
|
|
# ->
|
|
|
|
# http://HOST/ORG/PROJECT
|
|
|
|
def self.extract_sentry_external_url(url)
|
|
|
|
url.sub('api/0/projects/', '')
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def api_host
|
|
|
|
return if api_url.blank?
|
|
|
|
|
|
|
|
# This returns http://example.com/
|
|
|
|
Addressable::URI.join(api_url, '/').to_s
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def project_name_from_slug
|
|
|
|
@project_name_from_slug ||= project_slug_from_api_url&.titleize
|
|
|
|
end
|
|
|
|
|
|
|
|
def organization_name_from_slug
|
|
|
|
@organization_name_from_slug ||= organization_slug_from_api_url&.titleize
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_slug_from_api_url
|
2019-05-30 16:15:17 +05:30
|
|
|
extract_slug(:project)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def organization_slug_from_api_url
|
2019-05-30 16:15:17 +05:30
|
|
|
extract_slug(:organization)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
def extract_slug(capture)
|
2019-03-02 22:35:43 +05:30
|
|
|
return if api_url.blank?
|
|
|
|
|
|
|
|
begin
|
|
|
|
url = Addressable::URI.parse(api_url)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
2019-05-30 16:15:17 +05:30
|
|
|
return nil
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
@slug_match ||= url.path.match(%r{^/api/0/projects/+(?<organization>[^/]+)/+(?<project>[^/|$]+)}) || {}
|
|
|
|
@slug_match[capture]
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def validate_api_url_path
|
2019-03-02 22:35:43 +05:30
|
|
|
return if api_url.blank?
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
begin
|
|
|
|
unless Addressable::URI.parse(api_url).path.starts_with?('/api/0/projects')
|
|
|
|
errors.add(:api_url, 'path needs to start with /api/0/projects')
|
|
|
|
end
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|