debian-mirror-gitlab/lib/api/composer_packages.rb

224 lines
8.1 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
# PHP composer support (https://getcomposer.org/)
module API
2021-01-03 14:25:43 +05:30
class ComposerPackages < ::API::Base
2020-07-28 23:09:34 +05:30
helpers ::API::Helpers::PackagesManagerClientsHelpers
helpers ::API::Helpers::RelatedResourcesHelpers
helpers ::API::Helpers::Packages::BasicAuthHelpers
include ::API::Helpers::Packages::BasicAuthHelpers::Constants
include ::Gitlab::Utils::StrongMemoize
2021-01-29 00:20:46 +05:30
feature_category :package_registry
2020-07-28 23:09:34 +05:30
content_type :json, 'application/json'
default_format :json
COMPOSER_ENDPOINT_REQUIREMENTS = {
package_name: API::NO_SLASH_URL_PART_REGEX
}.freeze
default_format :json
rescue_from ArgumentError do |e|
render_api_error!(e.message, 400)
end
rescue_from ActiveRecord::RecordInvalid do |e|
render_api_error!(e.message, 400)
end
2021-01-03 14:25:43 +05:30
rescue_from Packages::Composer::ComposerJsonService::InvalidJson do |e|
render_api_error!(e.message, 422)
end
2020-07-28 23:09:34 +05:30
helpers do
def packages
strong_memoize(:packages) do
packages = ::Packages::Composer::PackagesFinder.new(current_user, user_group).execute
if params[:package_name].present?
2021-02-22 17:27:13 +05:30
params[:package_name], params[:sha] = params[:package_name].split('$')
2020-07-28 23:09:34 +05:30
packages = packages.with_name(params[:package_name])
end
packages
end
end
2021-04-17 20:07:23 +05:30
def composer_v2?
headers['User-Agent'].to_s.include?('Composer/2')
end
2020-07-28 23:09:34 +05:30
def presenter
2021-04-17 20:07:23 +05:30
@presenter ||= ::Packages::Composer::PackagesPresenter.new(user_group, packages, composer_v2?)
2020-07-28 23:09:34 +05:30
end
end
before do
require_packages_enabled!
end
params do
2023-03-04 22:38:38 +05:30
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of a group'
2020-07-28 23:09:34 +05:30
end
resource :group, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2023-03-04 22:38:38 +05:30
after_validation do
2020-07-28 23:09:34 +05:30
user_group
end
2023-03-04 22:38:38 +05:30
desc 'Composer packages endpoint at group level' do
detail 'This feature was introduced in GitLab 13.1'
success code: 200
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2022-01-26 12:08:38 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2022-07-16 23:28:13 +05:30
get ':id/-/packages/composer/packages', urgency: :low do
2020-07-28 23:09:34 +05:30
presenter.root
end
2023-03-04 22:38:38 +05:30
desc 'Composer packages endpoint at group level for packages list' do
detail 'This feature was introduced in GitLab 13.1'
success code: 200
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2020-07-28 23:09:34 +05:30
params do
2023-03-04 22:38:38 +05:30
requires :sha, type: String, desc: 'Shasum of current json', documentation: { example: '673594f85a55fe3c0eb45df7bd2fa9d95a1601ab' }
2020-07-28 23:09:34 +05:30
end
2022-01-26 12:08:38 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2022-07-16 23:28:13 +05:30
get ':id/-/packages/composer/p/:sha', urgency: :low do
2020-07-28 23:09:34 +05:30
presenter.provider
end
2023-03-04 22:38:38 +05:30
desc 'Composer v2 packages p2 endpoint at group level for package versions metadata' do
detail 'This feature was introduced in GitLab 13.1'
success code: 200
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2020-07-28 23:09:34 +05:30
params do
2023-03-04 22:38:38 +05:30
requires :package_name, type: String, file_path: true, desc: 'The Composer package name', documentation: { example: 'my-composer-package' }
2020-07-28 23:09:34 +05:30
end
2022-01-26 12:08:38 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2022-07-16 23:28:13 +05:30
get ':id/-/packages/composer/p2/*package_name', requirements: COMPOSER_ENDPOINT_REQUIREMENTS, file_path: true, urgency: :low do
2021-04-17 20:07:23 +05:30
not_found! if packages.empty?
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
presenter.package_versions
end
2023-03-04 22:38:38 +05:30
desc 'Composer packages endpoint at group level for package versions metadata' do
detail 'This feature was introduced in GitLab 12.1'
success code: 200
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2021-04-17 20:07:23 +05:30
params do
2023-03-04 22:38:38 +05:30
requires :package_name, type: String, file_path: true, desc: 'The Composer package name', documentation: { example: 'my-composer-package' }
2021-04-17 20:07:23 +05:30
end
2022-01-26 12:08:38 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2022-07-16 23:28:13 +05:30
get ':id/-/packages/composer/*package_name', requirements: COMPOSER_ENDPOINT_REQUIREMENTS, file_path: true, urgency: :low do
2020-07-28 23:09:34 +05:30
not_found! if packages.empty?
2021-02-22 17:27:13 +05:30
not_found! if params[:sha].blank?
2020-07-28 23:09:34 +05:30
presenter.package_versions
end
end
params do
2023-03-04 22:38:38 +05:30
requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of a project'
2020-07-28 23:09:34 +05:30
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
namespace ':id/packages/composer' do
2022-01-26 12:08:38 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2020-07-28 23:09:34 +05:30
2023-03-04 22:38:38 +05:30
desc 'Composer packages endpoint for registering packages' do
detail 'This feature was introduced in GitLab 13.1'
success code: 201
failure [
{ code: 400, message: 'Bad Request' },
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2020-07-28 23:09:34 +05:30
params do
2023-03-04 22:38:38 +05:30
optional :branch, type: String, desc: 'The name of the branch', documentation: { example: 'release' }
optional :tag, type: String, desc: 'The name of the tag', documentation: { example: 'v1.0.0' }
2020-07-28 23:09:34 +05:30
exactly_one_of :tag, :branch
end
2022-07-16 23:28:13 +05:30
post urgency: :low do
2020-07-28 23:09:34 +05:30
authorize_create_package!(authorized_user_project)
if params[:branch].present?
params[:branch] = find_branch!(params[:branch])
elsif params[:tag].present?
params[:tag] = find_tag!(params[:tag])
else
bad_request!
end
::Packages::Composer::CreatePackageService
2021-02-22 17:27:13 +05:30
.new(authorized_user_project, current_user, declared_params.merge(build: current_authenticated_job))
2020-07-28 23:09:34 +05:30
.execute
2021-11-18 22:05:49 +05:30
track_package_event('push_package', :composer, project: authorized_user_project, user: current_user, namespace: authorized_user_project.namespace)
2020-07-28 23:09:34 +05:30
created!
end
2023-03-04 22:38:38 +05:30
desc 'Composer package endpoint to download a package archive' do
detail 'This feature was introduced in GitLab 13.1'
success code: 200
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 404, message: 'Not Found' }
]
tags %w[composer_packages]
end
2020-07-28 23:09:34 +05:30
params do
2023-03-04 22:38:38 +05:30
requires :sha, type: String, desc: 'Shasum of current json', documentation: { example: '673594f85a55fe3c0eb45df7bd2fa9d95a1601ab' }
requires :package_name, type: String, file_path: true, desc: 'The Composer package name', documentation: { example: 'my-composer-package' }
2020-07-28 23:09:34 +05:30
end
2022-06-21 17:19:12 +05:30
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
2022-07-16 23:28:13 +05:30
get 'archives/*package_name', urgency: :default do
2023-03-04 22:38:38 +05:30
project = authorized_user_project(action: :read_package)
2022-06-21 17:19:12 +05:30
2023-03-04 22:38:38 +05:30
package = project
2020-07-28 23:09:34 +05:30
.packages
.composer
.with_name(params[:package_name])
.with_composer_target(params[:sha])
.first
2022-10-11 01:57:18 +05:30
metadata = package&.composer_metadatum
2020-07-28 23:09:34 +05:30
not_found! unless metadata
2023-03-04 22:38:38 +05:30
track_package_event('pull_package', :composer, project: project, namespace: project.namespace)
2022-10-11 01:57:18 +05:30
package.touch_last_downloaded_at
2021-04-29 21:17:54 +05:30
2023-03-04 22:38:38 +05:30
send_git_archive project.repository, ref: metadata.target_sha, format: 'zip', append_sha: true
2020-07-28 23:09:34 +05:30
end
end
end
end
end