2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
#
# NuGet Package Manager Client API
#
# These API endpoints are not consumed directly by users, so there is no documentation for the
# individual endpoints. They are called by the NuGet package manager client when users run commands
# like `nuget install` or `nuget push`. The usage of the GitLab NuGet registry is documented here:
# https://docs.gitlab.com/ee/user/packages/nuget_repository/
#
# Technical debt: https://gitlab.com/gitlab-org/gitlab/issues/35798
module API
module Concerns
module Packages
module NugetEndpoints
extend ActiveSupport :: Concern
POSITIVE_INTEGER_REGEX = %r{ \ A[1-9] \ d* \ z } . freeze
NON_NEGATIVE_INTEGER_REGEX = %r{ \ A(0|[1-9] \ d*) \ z } . freeze
included do
helpers do
2021-03-08 18:12:59 +05:30
def find_packages ( package_name )
packages = package_finder ( package_name ) . execute
2021-02-22 17:27:13 +05:30
not_found! ( 'Packages' ) unless packages . exists?
packages
end
2021-03-08 18:12:59 +05:30
def find_package ( package_name , package_version )
package = package_finder ( package_name , package_version ) . execute
. first
2021-02-22 17:27:13 +05:30
not_found! ( 'Package' ) unless package
package
end
2021-03-08 18:12:59 +05:30
def package_finder ( package_name , package_version = nil )
2021-02-22 17:27:13 +05:30
:: Packages :: Nuget :: PackageFinder . new (
2021-03-08 18:12:59 +05:30
current_user ,
project_or_group ,
package_name : package_name ,
package_version : package_version
2021-02-22 17:27:13 +05:30
)
end
2021-03-08 18:12:59 +05:30
def search_packages ( search_term , search_options )
:: Packages :: Nuget :: SearchService
. new ( current_user , project_or_group , params [ :q ] , search_options )
. execute
end
2021-02-22 17:27:13 +05:30
end
# https://docs.microsoft.com/en-us/nuget/api/service-index
desc 'The NuGet Service Index' do
detail 'This feature was introduced in GitLab 12.6'
2023-03-04 22:38:38 +05:30
success code : 200 , model : :: API :: Entities :: Nuget :: ServiceIndex
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ nuget_packages ]
2021-02-22 17:27:13 +05:30
end
2022-07-16 23:28:13 +05:30
get 'index' , format : :json , urgency : :default do
2023-03-17 16:20:25 +05:30
authorize_packages_access! ( project_or_group , required_permission )
2021-09-04 01:27:46 +05:30
track_package_event ( 'cli_metadata' , :nuget , ** snowplow_gitlab_standard_context . merge ( category : 'API::NugetPackages' ) )
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
present :: Packages :: Nuget :: ServiceIndexPresenter . new ( project_or_group ) ,
with : :: API :: Entities :: Nuget :: ServiceIndex
2021-02-22 17:27:13 +05:30
end
# https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
params do
2023-03-04 22:38:38 +05:30
requires :package_name , type : String , desc : 'The NuGet package name' , regexp : API :: NO_SLASH_URL_PART_REGEX , documentation : { example : 'MyNuGetPkg' }
2021-02-22 17:27:13 +05:30
end
namespace '/metadata/*package_name' do
2021-03-08 18:12:59 +05:30
after_validation do
2023-03-17 16:20:25 +05:30
authorize_packages_access! ( project_or_group , required_permission )
2021-02-22 17:27:13 +05:30
end
desc 'The NuGet Metadata Service - Package name level' do
detail 'This feature was introduced in GitLab 12.8'
2023-03-04 22:38:38 +05:30
success code : 200 , model : :: API :: Entities :: Nuget :: PackagesMetadata
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ nuget_packages ]
2021-02-22 17:27:13 +05:30
end
2022-07-16 23:28:13 +05:30
get 'index' , format : :json , urgency : :low do
2021-03-08 18:12:59 +05:30
present :: Packages :: Nuget :: PackagesMetadataPresenter . new ( find_packages ( params [ :package_name ] ) ) ,
2021-02-22 17:27:13 +05:30
with : :: API :: Entities :: Nuget :: PackagesMetadata
end
desc 'The NuGet Metadata Service - Package name and version level' do
detail 'This feature was introduced in GitLab 12.8'
2023-03-04 22:38:38 +05:30
success code : 200 , model : :: API :: Entities :: Nuget :: PackageMetadata
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ nuget_packages ]
2021-02-22 17:27:13 +05:30
end
params do
2023-03-04 22:38:38 +05:30
requires :package_version , type : String , desc : 'The NuGet package version' , regexp : API :: NO_SLASH_URL_PART_REGEX , documentation : { example : '1.0.0' }
2021-02-22 17:27:13 +05:30
end
2022-07-16 23:28:13 +05:30
get '*package_version' , format : :json , urgency : :low do
2021-03-08 18:12:59 +05:30
present :: Packages :: Nuget :: PackageMetadataPresenter . new ( find_package ( params [ :package_name ] , params [ :package_version ] ) ) ,
2021-02-22 17:27:13 +05:30
with : :: API :: Entities :: Nuget :: PackageMetadata
end
end
# https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource
params do
2023-03-04 22:38:38 +05:30
optional :q , type : String , desc : 'The search term' , documentation : { example : 'MyNuGet' }
optional :skip , type : Integer , desc : 'The number of results to skip' , default : 0 , regexp : NON_NEGATIVE_INTEGER_REGEX , documentation : { example : 1 }
optional :take , type : Integer , desc : 'The number of results to return' , default : Kaminari . config . default_per_page , regexp : POSITIVE_INTEGER_REGEX , documentation : { example : 1 }
2021-02-22 17:27:13 +05:30
optional :prerelease , type : :: Grape :: API :: Boolean , desc : 'Include prerelease versions' , default : true
end
namespace '/query' do
2021-03-08 18:12:59 +05:30
after_validation do
2023-03-17 16:20:25 +05:30
authorize_packages_access! ( project_or_group , required_permission )
2021-02-22 17:27:13 +05:30
end
desc 'The NuGet Search Service' do
detail 'This feature was introduced in GitLab 12.8'
2023-03-04 22:38:38 +05:30
success code : 200 , model : :: API :: Entities :: Nuget :: SearchResults
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ nuget_packages ]
2021-02-22 17:27:13 +05:30
end
2022-07-16 23:28:13 +05:30
get format : :json , urgency : :low do
2021-02-22 17:27:13 +05:30
search_options = {
include_prerelease_versions : params [ :prerelease ] ,
per_page : params [ :take ] ,
padding : params [ :skip ]
}
2021-03-08 18:12:59 +05:30
results = search_packages ( params [ :q ] , search_options )
2021-02-22 17:27:13 +05:30
2021-09-04 01:27:46 +05:30
track_package_event ( 'search_package' , :nuget , ** snowplow_gitlab_standard_context . merge ( category : 'API::NugetPackages' ) )
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
present :: Packages :: Nuget :: SearchResultsPresenter . new ( results ) ,
with : :: API :: Entities :: Nuget :: SearchResults
2021-02-22 17:27:13 +05:30
end
end
end
end
end
end
end