Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dependency_proxy_helpers.rb « packages « helpers « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8ae1dddd7e6a8307ea3e63cc62168e0cef9c1fe (plain)
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
51
52
# frozen_string_literal: true

module API
  module Helpers
    module Packages
      module DependencyProxyHelpers
        REGISTRY_BASE_URLS = {
          npm: 'https://registry.npmjs.org/',
          pypi: 'https://pypi.org/simple/'
        }.freeze

        APPLICATION_SETTING_NAMES = {
          npm: 'npm_package_requests_forwarding',
          pypi: 'pypi_package_requests_forwarding'
        }.freeze

        def redirect_registry_request(forward_to_registry, package_type, options)
          if forward_to_registry && redirect_registry_request_available?(package_type)
            ::Gitlab::Tracking.event(self.options[:for].name, "#{package_type}_request_forward")
            redirect(registry_url(package_type, options))
          else
            yield
          end
        end

        def registry_url(package_type, options)
          base_url = REGISTRY_BASE_URLS[package_type]

          raise ArgumentError, "Can't build registry_url for package_type #{package_type}" unless base_url

          case package_type
          when :npm
            "#{base_url}#{options[:package_name]}"
          when :pypi
            "#{base_url}#{options[:package_name]}/"
          end
        end

        def redirect_registry_request_available?(package_type)
          application_setting_name = APPLICATION_SETTING_NAMES[package_type]

          raise ArgumentError, "Can't find application setting for package_type #{package_type}" unless application_setting_name

          ::Gitlab::CurrentSettings
            .current_application_settings
            .attributes
            .fetch(application_setting_name, false)
        end
      end
    end
  end
end