Class: ErrorTracking::ProjectErrorTrackingSetting

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Gitlab::Routing, Gitlab::Utils::StrongMemoize, ReactiveCaching
Defined in:
app/models/error_tracking/project_error_tracking_setting.rb

Constant Summary collapse

SENTRY_API_ERROR_TYPE_BAD_REQUEST =
'bad_request_for_sentry_api'
SENTRY_API_ERROR_TYPE_MISSING_KEYS =
'missing_keys_in_sentry_response'
SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE =
'non_20x_response_from_sentry'
SENTRY_API_ERROR_INVALID_SIZE =
'invalid_size_of_sentry_response'
API_URL_PATH_REGEXP =
%r{
  \A
    (?<prefix>/api/0/projects/+)
    (?:
      (?<organization>[^/]+)/+
      (?<project>[^/]+)/*
    )?
  \z
}x.freeze

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Routing

add_helpers, includes_helpers, redirect_legacy_paths, url_helpers

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.build_api_url_from(api_host:, project_slug:, organization_slug:) ⇒ Object


67
68
69
70
71
72
73
74
75
76
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 67

def self.build_api_url_from(api_host:, project_slug:, organization_slug:)
  return if api_host.blank?

  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

.extract_sentry_external_url(url) ⇒ Object


141
142
143
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 141

def self.extract_sentry_external_url(url)
  url&.sub('api/0/projects/', '')
end

Instance Method Details

#api_hostObject


145
146
147
148
149
150
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 145

def api_host
  return if api_url.blank?

  # This returns http://example.com/
  Addressable::URI.join(api_url, '/').to_s
end

#api_url=(value) ⇒ Object


46
47
48
49
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 46

def api_url=(value)
  super
  clear_memoization(:api_url_slugs)
end

#calculate_reactive_cache(request, opts) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 118

def calculate_reactive_cache(request, opts)
  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
  end
end

#expire_issues_cacheObject


134
135
136
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 134

def expire_issues_cache
  clear_reactive_cache_set!('list_issues')
end

#issue_details(opts = {}) ⇒ Object


100
101
102
103
104
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 100

def issue_details(opts = {})
  with_reactive_cache('issue_details', opts.stringify_keys) do |result|
    result
  end
end

#issue_latest_event(opts = {}) ⇒ Object


106
107
108
109
110
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 106

def issue_latest_event(opts = {})
  with_reactive_cache('issue_latest_event', opts.stringify_keys) do |result|
    result
  end
end

#list_sentry_issues(opts = {}) ⇒ Object


88
89
90
91
92
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 88

def list_sentry_issues(opts = {})
  with_reactive_cache_set('list_issues', opts.stringify_keys) do |result|
    result
  end
end

#list_sentry_projectsObject


94
95
96
97
98
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 94

def list_sentry_projects
  handle_exceptions do
    { projects: sentry_client.projects }
  end
end

#organization_nameObject


55
56
57
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 55

def organization_name
  super || organization_name_from_slug
end

#organization_slugObject


63
64
65
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 63

def organization_slug
  organization_slug_from_api_url
end

#project_nameObject


51
52
53
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 51

def project_name
  super || project_name_from_slug
end

#project_slugObject


59
60
61
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 59

def project_slug
  project_slug_from_api_url
end

#sentry_clientObject


78
79
80
81
82
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 78

def sentry_client
  strong_memoize(:sentry_client) do
    Sentry::Client.new(api_url, token)
  end
end

#sentry_external_urlObject


84
85
86
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 84

def sentry_external_url
  self.class.extract_sentry_external_url(api_url)
end

#update_issue(opts = {}) ⇒ Object


112
113
114
115
116
# File 'app/models/error_tracking/project_error_tracking_setting.rb', line 112

def update_issue(opts = {} )
  handle_exceptions do
    { updated: sentry_client.update_issue(opts) }
  end
end