2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ErrorTracking
|
2019-07-07 11:18:12 +05:30
|
|
|
class ProjectErrorTrackingSetting < ApplicationRecord
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-02-15 15:39:39 +05:30
|
|
|
include ReactiveCaching
|
2020-03-13 15:44:24 +05:30
|
|
|
include Gitlab::Routing
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
SENTRY_API_ERROR_TYPE_BAD_REQUEST = 'bad_request_for_sentry_api'
|
2019-07-07 11:18:12 +05:30
|
|
|
SENTRY_API_ERROR_TYPE_MISSING_KEYS = 'missing_keys_in_sentry_response'
|
|
|
|
SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE = 'non_20x_response_from_sentry'
|
2019-12-26 22:10:19 +05:30
|
|
|
SENTRY_API_ERROR_INVALID_SIZE = 'invalid_size_of_sentry_response'
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
API_URL_PATH_REGEXP = %r{
|
|
|
|
\A
|
|
|
|
(?<prefix>/api/0/projects/+)
|
|
|
|
(?:
|
|
|
|
(?<organization>[^/]+)/+
|
|
|
|
(?<project>[^/]+)/*
|
|
|
|
)?
|
|
|
|
\z
|
2019-07-31 22:56:46 +05:30
|
|
|
}x.freeze
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
self.reactive_cache_key = ->(setting) { [setting.class.model_name.singular, setting.project_id] }
|
2020-05-24 23:13:21 +05:30
|
|
|
self.reactive_cache_work_type = :external_dependency
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
self.table_name = 'project_error_tracking_settings'
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
belongs_to :project
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
validates :api_url, length: { maximum: 255 }, public_url: { enforce_sanitization: true, ascii_only: true }, allow_nil: true
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
validates :enabled, inclusion: { in: [true, false] }
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
validates :api_url, presence: { message: 'is a required field' }, if: :enabled
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
validate :validate_api_url_path, if: :enabled
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
validates :token, presence: { message: 'is a required field' }, if: :enabled
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
attr_encrypted :token,
|
|
|
|
mode: :per_attribute_iv,
|
2021-06-08 01:23:25 +05:30
|
|
|
key: Settings.attr_encrypted_db_key_base_32,
|
2019-02-15 15:39:39 +05:30
|
|
|
algorithm: 'aes-256-gcm'
|
|
|
|
|
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def api_url=(value)
|
|
|
|
super
|
|
|
|
clear_memoization(:api_url_slugs)
|
|
|
|
end
|
|
|
|
|
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:)
|
2019-07-07 11:18:12 +05:30
|
|
|
return if api_host.blank?
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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
|
2020-03-13 15:44:24 +05:30
|
|
|
strong_memoize(:sentry_client) do
|
2021-04-17 20:07:23 +05:30
|
|
|
ErrorTracking::SentryClient.new(api_url, token)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def sentry_external_url
|
|
|
|
self.class.extract_sentry_external_url(api_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_sentry_issues(opts = {})
|
2020-04-08 14:13:33 +05:30
|
|
|
with_reactive_cache_set('list_issues', opts.stringify_keys) do |result|
|
2019-07-07 11:18:12 +05:30
|
|
|
result
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def list_sentry_projects
|
2020-03-13 15:44:24 +05:30
|
|
|
handle_exceptions do
|
|
|
|
{ projects: sentry_client.projects }
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def issue_details(opts = {})
|
|
|
|
with_reactive_cache('issue_details', opts.stringify_keys) do |result|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_latest_event(opts = {})
|
|
|
|
with_reactive_cache('issue_latest_event', opts.stringify_keys) do |result|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def update_issue(opts = {} )
|
|
|
|
handle_exceptions do
|
|
|
|
{ updated: sentry_client.update_issue(opts) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def calculate_reactive_cache(request, opts)
|
2020-03-13 15:44:24 +05:30
|
|
|
handle_exceptions do
|
|
|
|
case request
|
|
|
|
when 'list_issues'
|
|
|
|
sentry_client.list_issues(**opts.symbolize_keys)
|
|
|
|
when 'issue_details'
|
|
|
|
issue = sentry_client.issue_details(**opts.symbolize_keys)
|
|
|
|
{ issue: add_gitlab_issue_details(issue) }
|
|
|
|
when 'issue_latest_event'
|
|
|
|
{
|
|
|
|
latest_event: sentry_client.issue_latest_event(**opts.symbolize_keys)
|
|
|
|
}
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def expire_issues_cache
|
|
|
|
clear_reactive_cache_set!('list_issues')
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
# http://HOST/api/0/projects/ORG/PROJECT
|
|
|
|
# ->
|
|
|
|
# http://HOST/ORG/PROJECT
|
|
|
|
def self.extract_sentry_external_url(url)
|
2020-03-13 15:44:24 +05:30
|
|
|
url&.sub('api/0/projects/', '')
|
2019-02-15 15:39:39 +05:30
|
|
|
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
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def add_gitlab_issue_details(issue)
|
|
|
|
issue.gitlab_commit = match_gitlab_commit(issue.first_release_version)
|
|
|
|
issue.gitlab_commit_path = project_commit_path(project, issue.gitlab_commit) if issue.gitlab_commit
|
|
|
|
|
|
|
|
issue
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_gitlab_commit(release_version)
|
|
|
|
return unless release_version
|
|
|
|
|
|
|
|
commit = project.repository.commit(release_version)
|
|
|
|
|
|
|
|
commit&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_exceptions
|
|
|
|
yield
|
2021-04-17 20:07:23 +05:30
|
|
|
rescue ErrorTracking::SentryClient::Error => e
|
2020-03-13 15:44:24 +05:30
|
|
|
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE }
|
2021-04-17 20:07:23 +05:30
|
|
|
rescue ErrorTracking::SentryClient::MissingKeysError => e
|
2020-03-13 15:44:24 +05:30
|
|
|
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_MISSING_KEYS }
|
2021-04-17 20:07:23 +05:30
|
|
|
rescue ErrorTracking::SentryClient::ResponseInvalidSizeError => e
|
2020-03-13 15:44:24 +05:30
|
|
|
{ error: e.message, error_type: SENTRY_API_ERROR_INVALID_SIZE }
|
2021-04-17 20:07:23 +05:30
|
|
|
rescue ErrorTracking::SentryClient::BadRequestError => e
|
2020-03-13 15:44:24 +05:30
|
|
|
{ error: e.message, error_type: SENTRY_API_ERROR_TYPE_BAD_REQUEST }
|
|
|
|
rescue StandardError => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
{ error: 'Unexpected Error' }
|
|
|
|
end
|
|
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
api_url_slug(:project)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def organization_slug_from_api_url
|
2019-07-07 11:18:12 +05:30
|
|
|
api_url_slug(:organization)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def api_url_slug(capture)
|
|
|
|
slugs = strong_memoize(:api_url_slugs) { extract_api_url_slugs || {} }
|
|
|
|
slugs[capture]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_api_url_slugs
|
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-07-07 11:18:12 +05:30
|
|
|
return
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
url.path.match(API_URL_PATH_REGEXP)
|
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-07-07 11:18:12 +05:30
|
|
|
unless api_url_slug(:prefix)
|
|
|
|
return errors.add(:api_url, 'is invalid')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless api_url_slug(:organization)
|
|
|
|
errors.add(:project, 'is a required field')
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|