Read about our upcoming Code of Conduct on this issue

project_error_tracking_setting.rb 5.68 KB
Newer Older
1
2
3
# frozen_string_literal: true

module ErrorTracking
4
  class ProjectErrorTrackingSetting < ApplicationRecord
Reuben Pereira's avatar
Reuben Pereira committed
5
    include Gitlab::Utils::StrongMemoize
6
    include ReactiveCaching
7
    include Gitlab::Routing
8

9
    SENTRY_API_ERROR_TYPE_BAD_REQUEST = 'bad_request_for_sentry_api'
10
11
    SENTRY_API_ERROR_TYPE_MISSING_KEYS = 'missing_keys_in_sentry_response'
    SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE = 'non_20x_response_from_sentry'
12
    SENTRY_API_ERROR_INVALID_SIZE = 'invalid_size_of_sentry_response'
13

Reuben Pereira's avatar
Reuben Pereira committed
14
15
16
17
18
19
20
21
    API_URL_PATH_REGEXP = %r{
      \A
        (?<prefix>/api/0/projects/+)
        (?:
          (?<organization>[^/]+)/+
          (?<project>[^/]+)/*
        )?
      \z
Stan Hu's avatar
Stan Hu committed
22
    }x.freeze
Reuben Pereira's avatar
Reuben Pereira committed
23

24
25
    self.reactive_cache_key = ->(setting) { [setting.class.model_name.singular, setting.project_id] }

26
27
    belongs_to :project

28
    validates :api_url, length: { maximum: 255 }, public_url: { enforce_sanitization: true, ascii_only: true }, allow_nil: true
29

Reuben Pereira's avatar
Reuben Pereira committed
30
    validates :api_url, presence: { message: 'is a required field' }, if: :enabled
31
32
33

    validate :validate_api_url_path, if: :enabled

Reuben Pereira's avatar
Reuben Pereira committed
34
    validates :token, presence: { message: 'is a required field' }, if: :enabled
35

36
37
38
39
    attr_encrypted :token,
      mode: :per_attribute_iv,
      key: Settings.attr_encrypted_db_key_base_truncated,
      algorithm: 'aes-256-gcm'
40
41
42

    after_save :clear_reactive_cache!

Reuben Pereira's avatar
Reuben Pereira committed
43
44
45
46
47
    def api_url=(value)
      super
      clear_memoization(:api_url_slugs)
    end

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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:)
Reuben Pereira's avatar
Reuben Pereira committed
65
66
      return if api_host.blank?

67
68
69
70
71
72
73
74
      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

75
76
77
78
79
80
81
82
83
84
    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|
85
        result
86
87
88
      end
    end

89
    def list_sentry_projects
90
      { projects: sentry_client.projects }
91
92
    end

93
94
95
96
97
98
99
100
101
102
103
104
    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

105
106
107
108
109
110
    def update_issue(opts = {} )
      handle_exceptions do
        { updated: sentry_client.update_issue(opts) }
      end
    end

111
    def calculate_reactive_cache(request, opts)
112
113
114
115
116
      handle_exceptions do
        case request
        when 'list_issues'
          sentry_client.list_issues(**opts.symbolize_keys)
        when 'issue_details'
117
118
          issue = sentry_client.issue_details(**opts.symbolize_keys)
          { issue: add_gitlab_issue_details(issue) }
119
120
121
122
123
        when 'issue_latest_event'
          {
            latest_event: sentry_client.issue_latest_event(**opts.symbolize_keys)
          }
        end
124
125
126
127
128
129
130
      end
    end

    # http://HOST/api/0/projects/ORG/PROJECT
    # ->
    # http://HOST/ORG/PROJECT
    def self.extract_sentry_external_url(url)
131
      url&.sub('api/0/projects/', '')
132
133
    end

134
135
136
137
138
139
140
    def api_host
      return if api_url.blank?

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

141
142
    private

143
144
    def add_gitlab_issue_details(issue)
      issue.gitlab_commit = match_gitlab_commit(issue.first_release_version)
145
      issue.gitlab_commit_path = project_commit_path(project, issue.gitlab_commit) if issue.gitlab_commit
146
147
148
149
150
151
152
153
154
155
156
157

      issue
    end

    def match_gitlab_commit(release_version)
      return unless release_version

      commit = project.repository.commit(release_version)

      commit&.id
    end

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    def handle_exceptions
      yield
    rescue Sentry::Client::Error => e
      { error: e.message, error_type: SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE }
    rescue Sentry::Client::MissingKeysError => e
      { error: e.message, error_type: SENTRY_API_ERROR_TYPE_MISSING_KEYS }
    rescue Sentry::Client::ResponseInvalidSizeError => e
      { error: e.message, error_type: SENTRY_API_ERROR_INVALID_SIZE }
    rescue Sentry::Client::BadRequestError => e
      { error: e.message, error_type: SENTRY_API_ERROR_TYPE_BAD_REQUEST }
    rescue StandardError => e
      Gitlab::ErrorTracking.track_exception(e)
      { error: 'Unexpected Error' }
    end

173
174
175
176
177
178
179
180
181
    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
Reuben Pereira's avatar
Reuben Pereira committed
182
      api_url_slug(:project)
183
184
185
    end

    def organization_slug_from_api_url
Reuben Pereira's avatar
Reuben Pereira committed
186
187
188
189
190
191
      api_url_slug(:organization)
    end

    def api_url_slug(capture)
      slugs = strong_memoize(:api_url_slugs) { extract_api_url_slugs || {} }
      slugs[capture]
192
193
    end

Reuben Pereira's avatar
Reuben Pereira committed
194
    def extract_api_url_slugs
195
196
197
198
199
      return if api_url.blank?

      begin
        url = Addressable::URI.parse(api_url)
      rescue Addressable::URI::InvalidURIError
Reuben Pereira's avatar
Reuben Pereira committed
200
        return
201
202
      end

Reuben Pereira's avatar
Reuben Pereira committed
203
      url.path.match(API_URL_PATH_REGEXP)
204
205
    end

206
    def validate_api_url_path
207
208
      return if api_url.blank?

Reuben Pereira's avatar
Reuben Pereira committed
209
210
211
212
213
214
      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')
215
216
      end
    end
217
218
  end
end