File: list_projects_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (50 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

module ErrorTracking
  class ListProjectsService < ErrorTracking::BaseService
    private

    def perform
      return error('Access denied', :unauthorized) unless can?(current_user, :admin_sentry, project)

      unless project_error_tracking_setting.valid?
        return error(project_error_tracking_setting.errors.full_messages.join(', '), :bad_request)
      end

      response = project_error_tracking_setting.list_sentry_projects

      compose_response(response)
    end

    def parse_response(response)
      { projects: response[:projects] }
    end

    def project_error_tracking_setting
      (super || project.build_error_tracking_setting).tap do |setting|
        setting.api_url = ErrorTracking::ProjectErrorTrackingSetting.build_api_url_from(
          api_host: params[:api_host],
          organization_slug: 'org',
          project_slug: 'proj'
        )

        setting.token = token(setting)
        setting.enabled = true
      end
    end
    strong_memoize_attr :project_error_tracking_setting

    def token(setting)
      return if setting.api_url_changed? && masked_token?

      # Use param token if not masked, otherwise use database token
      return params[:token] unless masked_token?

      setting.token
    end

    def masked_token?
      ErrorTracking::SentryClient::Token.masked_token?(params[:token])
    end
  end
end