diff options
Diffstat (limited to 'lib/api/helpers/packages')
-rw-r--r-- | lib/api/helpers/packages/dependency_proxy_helpers.rb | 23 | ||||
-rw-r--r-- | lib/api/helpers/packages/npm.rb | 14 |
2 files changed, 22 insertions, 15 deletions
diff --git a/lib/api/helpers/packages/dependency_proxy_helpers.rb b/lib/api/helpers/packages/dependency_proxy_helpers.rb index 989c4e1761b..b8ae1dddd7e 100644 --- a/lib/api/helpers/packages/dependency_proxy_helpers.rb +++ b/lib/api/helpers/packages/dependency_proxy_helpers.rb @@ -5,11 +5,17 @@ module API module Packages module DependencyProxyHelpers REGISTRY_BASE_URLS = { - npm: 'https://registry.npmjs.org/' + 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? + 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 @@ -25,11 +31,20 @@ module API case package_type when :npm "#{base_url}#{options[:package_name]}" + when :pypi + "#{base_url}#{options[:package_name]}/" end end - def redirect_registry_request_available? - ::Gitlab::CurrentSettings.current_application_settings.npm_package_requests_forwarding + 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 diff --git a/lib/api/helpers/packages/npm.rb b/lib/api/helpers/packages/npm.rb index 2d556f889bf..ce5db52fdbc 100644 --- a/lib/api/helpers/packages/npm.rb +++ b/lib/api/helpers/packages/npm.rb @@ -49,28 +49,20 @@ module API when :project params[:id] when :instance - namespace_path = namespace_path_from_package_name + package_name = params[:package_name] + namespace_path = ::Packages::Npm.scope_of(package_name) next unless namespace_path namespace = Namespace.top_most .by_path(namespace_path) next unless namespace - finder = ::Packages::Npm::PackageFinder.new(params[:package_name], namespace: namespace) + finder = ::Packages::Npm::PackageFinder.new(package_name, namespace: namespace) finder.last&.project_id end end end - - # from "@scope/package-name" return "scope" or nil - def namespace_path_from_package_name - package_name = params[:package_name] - return unless package_name.starts_with?('@') - return unless package_name.include?('/') - - package_name.match(Gitlab::Regex.npm_package_name_regex)&.captures&.first - end end end end |