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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.8 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module API
module Helpers
module Packages
module Npm
include Gitlab::Utils::StrongMemoize
include ::API::Helpers::PackagesHelpers
NPM_ENDPOINT_REQUIREMENTS = {
package_name: API::NO_SLASH_URL_PART_REGEX
}.freeze
def endpoint_scope
params[:id].present? ? :project : :instance
end
def project
strong_memoize(:project) do
case endpoint_scope
when :project
2023-01-13 00:05:48 +05:30
user_project(action: :read_package)
2021-01-29 00:20:46 +05:30
when :instance
# Simulate the same behavior as #user_project by re-using #find_project!
# but take care if the project_id is nil as #find_project! is not designed
# to handle it.
project_id = project_id_or_nil
not_found!('Project') unless project_id
find_project!(project_id)
end
end
end
2023-05-27 22:25:52 +05:30
def finder_for_endpoint_scope(package_name)
case endpoint_scope
when :project
::Packages::Npm::PackageFinder.new(package_name, project: project_or_nil)
when :instance
::Packages::Npm::PackageFinder.new(package_name, namespace: top_namespace_from(package_name))
end
end
2021-01-29 00:20:46 +05:30
def project_or_nil
# mainly used by the metadata endpoint where we need to get a project
# and return nil if not found (no errors should be raised)
strong_memoize(:project_or_nil) do
next unless project_id_or_nil
find_project(project_id_or_nil)
end
end
def project_id_or_nil
strong_memoize(:project_id_or_nil) do
case endpoint_scope
when :project
params[:id]
when :instance
2021-10-27 15:23:28 +05:30
package_name = params[:package_name]
2021-04-17 20:07:23 +05:30
2023-05-27 22:25:52 +05:30
namespace =
if Feature.enabled?(:npm_allow_packages_in_multiple_projects)
top_namespace_from(package_name)
else
namespace_path = ::Packages::Npm.scope_of(package_name)
next unless namespace_path
Namespace.top_most.by_path(namespace_path)
end
2021-04-17 20:07:23 +05:30
next unless namespace
2021-11-11 11:23:49 +05:30
finder = ::Packages::Npm::PackageFinder.new(
package_name,
namespace: namespace,
last_of_each_version: false
)
2021-04-17 20:07:23 +05:30
finder.last&.project_id
2021-01-29 00:20:46 +05:30
end
end
end
2023-05-27 22:25:52 +05:30
private
def top_namespace_from(package_name)
namespace_path = ::Packages::Npm.scope_of(package_name)
return unless namespace_path
Namespace.top_most.by_path(namespace_path)
end
2021-01-29 00:20:46 +05:30
end
end
end
end