2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
module API
2021-01-03 14:25:43 +05:30
class GenericPackages < :: API :: Base
GENERIC_PACKAGES_REQUIREMENTS = {
package_name : API :: NO_SLASH_URL_PART_REGEX ,
file_name : API :: NO_SLASH_URL_PART_REGEX
} . freeze
2021-03-11 19:13:27 +05:30
ALLOWED_STATUSES = %w[ default hidden ] . freeze
2021-01-29 00:20:46 +05:30
feature_category :package_registry
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2020-11-24 15:15:51 +05:30
before do
require_packages_enabled!
2021-04-17 20:07:23 +05:30
authenticate_non_get!
2020-11-24 15:15:51 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project'
2020-11-24 15:15:51 +05:30
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2021-03-08 18:12:59 +05:30
route_setting :authentication , job_token_allowed : true , basic_auth_personal_access_token : true , deploy_token_allowed : true
2020-11-24 15:15:51 +05:30
namespace ':id/packages/generic' do
2021-01-03 14:25:43 +05:30
namespace ':package_name/*package_version/:file_name' , requirements : GENERIC_PACKAGES_REQUIREMENTS do
desc 'Workhorse authorize generic package file' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success code : 200
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ generic_packages ]
2021-01-03 14:25:43 +05:30
end
2021-03-08 18:12:59 +05:30
route_setting :authentication , job_token_allowed : true , basic_auth_personal_access_token : true , deploy_token_allowed : true
2021-01-03 14:25:43 +05:30
params do
requires :package_name , type : String , desc : 'Package name' , regexp : Gitlab :: Regex . generic_package_name_regex , file_path : true
requires :package_version , type : String , desc : 'Package version' , regexp : Gitlab :: Regex . generic_package_version_regex
requires :file_name , type : String , desc : 'Package file name' , regexp : Gitlab :: Regex . generic_package_file_name_regex , file_path : true
2021-03-11 19:13:27 +05:30
optional :status , type : String , values : ALLOWED_STATUSES , desc : 'Package status'
2021-01-03 14:25:43 +05:30
end
put 'authorize' do
2022-11-25 23:54:43 +05:30
project = authorized_user_project
2021-01-03 14:25:43 +05:30
authorize_workhorse! ( subject : project , maximum_size : project . actual_limits . generic_packages_max_file_size )
end
desc 'Upload package file' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success [
{ code : 200 } ,
{ code : 201 }
]
failure [
{ code : 400 , message : 'Bad Request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ generic_packages ]
2021-01-03 14:25:43 +05:30
end
params do
requires :package_name , type : String , desc : 'Package name' , regexp : Gitlab :: Regex . generic_package_name_regex , file_path : true
requires :package_version , type : String , desc : 'Package version' , regexp : Gitlab :: Regex . generic_package_version_regex
requires :file_name , type : String , desc : 'Package file name' , regexp : Gitlab :: Regex . generic_package_file_name_regex , file_path : true
2021-03-11 19:13:27 +05:30
optional :status , type : String , values : ALLOWED_STATUSES , desc : 'Package status'
2023-01-13 00:05:48 +05:30
requires :file , type : :: API :: Validations :: Types :: WorkhorseFile , desc : 'The package file to be published (generated by Multipart middleware)' , documentation : { type : 'file' }
2021-12-11 22:18:48 +05:30
optional :select , type : String , values : %w[ package_file ]
2021-01-03 14:25:43 +05:30
end
2021-03-08 18:12:59 +05:30
route_setting :authentication , job_token_allowed : true , basic_auth_personal_access_token : true , deploy_token_allowed : true
2021-01-03 14:25:43 +05:30
put do
2022-11-25 23:54:43 +05:30
project = authorized_user_project
2021-01-03 14:25:43 +05:30
authorize_upload! ( project )
bad_request! ( 'File is too large' ) if max_file_size_exceeded?
2023-05-27 22:25:52 +05:30
track_package_event ( 'push_package' , :generic , project : project , namespace : project . namespace )
2021-01-03 14:25:43 +05:30
create_package_file_params = declared_params . merge ( build : current_authenticated_job )
2021-12-11 22:18:48 +05:30
package_file = :: Packages :: Generic :: CreatePackageFileService
2021-01-03 14:25:43 +05:30
. new ( project , current_user , create_package_file_params )
. execute
2021-12-11 22:18:48 +05:30
if params [ :select ] == 'package_file'
present package_file
else
created!
end
2021-01-03 14:25:43 +05:30
rescue ObjectStorage :: RemoteStoreError = > e
Gitlab :: ErrorTracking . track_exception ( e , extra : { file_name : params [ :file_name ] , project_id : project . id } )
forbidden!
2021-06-08 01:23:25 +05:30
rescue :: Packages :: DuplicatePackageError
bad_request! ( 'Duplicate package is not allowed' )
2021-01-03 14:25:43 +05:30
end
desc 'Download package file' do
detail 'This feature was introduced in GitLab 13.5'
2023-03-04 22:38:38 +05:30
success code : 200
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not Found' }
]
tags %w[ generic_packages ]
2021-01-03 14:25:43 +05:30
end
params do
requires :package_name , type : String , desc : 'Package name' , regexp : Gitlab :: Regex . generic_package_name_regex , file_path : true
requires :package_version , type : String , desc : 'Package version' , regexp : Gitlab :: Regex . generic_package_version_regex
requires :file_name , type : String , desc : 'Package file name' , regexp : Gitlab :: Regex . generic_package_file_name_regex , file_path : true
end
2021-03-08 18:12:59 +05:30
route_setting :authentication , job_token_allowed : true , basic_auth_personal_access_token : true , deploy_token_allowed : true
2021-01-03 14:25:43 +05:30
get do
2022-11-25 23:54:43 +05:30
project = authorized_user_project ( action : :read_package )
2021-01-03 14:25:43 +05:30
authorize_read_package! ( project )
package = :: Packages :: Generic :: PackageFinder . new ( project ) . execute! ( params [ :package_name ] , params [ :package_version ] )
package_file = :: Packages :: PackageFileFinder . new ( package , params [ :file_name ] ) . execute!
2023-05-27 22:25:52 +05:30
track_package_event ( 'pull_package' , :generic , project : project , namespace : project . namespace )
2021-01-03 14:25:43 +05:30
2022-10-11 01:57:18 +05:30
present_package_file! ( package_file )
2021-01-03 14:25:43 +05:30
end
2020-11-24 15:15:51 +05:30
end
end
end
helpers do
include :: API :: Helpers :: PackagesHelpers
2021-01-03 14:25:43 +05:30
include :: API :: Helpers :: Packages :: BasicAuthHelpers
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
def max_file_size_exceeded?
2022-11-25 23:54:43 +05:30
authorized_user_project . actual_limits . exceeded? ( :generic_packages_max_file_size , params [ :file ] . size )
2020-11-24 15:15:51 +05:30
end
end
end
end