debian-mirror-gitlab/lib/api/helpers/packages/dependency_proxy_helpers.rb

53 lines
1.6 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module API
module Helpers
module Packages
module DependencyProxyHelpers
REGISTRY_BASE_URLS = {
2021-10-27 15:23:28 +05:30
npm: 'https://registry.npmjs.org/',
pypi: 'https://pypi.org/simple/'
}.freeze
APPLICATION_SETTING_NAMES = {
npm: 'npm_package_requests_forwarding',
pypi: 'pypi_package_requests_forwarding'
2020-07-28 23:09:34 +05:30
}.freeze
def redirect_registry_request(forward_to_registry, package_type, options)
2021-10-27 15:23:28 +05:30
if forward_to_registry && redirect_registry_request_available?(package_type)
2021-04-29 21:17:54 +05:30
::Gitlab::Tracking.event(self.options[:for].name, "#{package_type}_request_forward")
2020-07-28 23:09:34 +05:30
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]}"
2021-10-27 15:23:28 +05:30
when :pypi
"#{base_url}#{options[:package_name]}/"
2020-07-28 23:09:34 +05:30
end
end
2021-10-27 15:23:28 +05:30
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)
2020-07-28 23:09:34 +05:30
end
end
end
end
end