2020-07-17 02:09:11 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# PHP composer support (https://getcomposer.org/)
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class ComposerPackages < ::API::Base
|
2020-07-17 02:09:11 -04:00
|
|
|
helpers ::API::Helpers::PackagesManagerClientsHelpers
|
|
|
|
helpers ::API::Helpers::RelatedResourcesHelpers
|
|
|
|
helpers ::API::Helpers::Packages::BasicAuthHelpers
|
|
|
|
include ::API::Helpers::Packages::BasicAuthHelpers::Constants
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-10-30 11:08:59 -04:00
|
|
|
feature_category :package_registry
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
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
|
|
|
|
|
2020-10-13 14:08:58 -04:00
|
|
|
rescue_from Packages::Composer::ComposerJsonService::InvalidJson do |e|
|
|
|
|
render_api_error!(e.message, 422)
|
|
|
|
end
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
helpers do
|
|
|
|
def packages
|
|
|
|
strong_memoize(:packages) do
|
|
|
|
packages = ::Packages::Composer::PackagesFinder.new(current_user, user_group).execute
|
|
|
|
|
|
|
|
if params[:package_name].present?
|
2020-12-15 07:10:17 -05:00
|
|
|
params[:package_name], params[:sha] = params[:package_name].split('$')
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
packages = packages.with_name(params[:package_name])
|
|
|
|
end
|
|
|
|
|
|
|
|
packages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-18 20:09:00 -04:00
|
|
|
def composer_v2?
|
|
|
|
headers['User-Agent'].to_s.include?('Composer/2')
|
|
|
|
end
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
def presenter
|
2021-03-18 20:09:00 -04:00
|
|
|
@presenter ||= ::Packages::Composer::PackagesPresenter.new(user_group, packages, composer_v2?)
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
require_packages_enabled!
|
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a group'
|
|
|
|
end
|
|
|
|
|
|
|
|
resource :group, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
before do
|
|
|
|
user_group
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Composer packages endpoint at group level'
|
2021-12-14 10:17:06 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2022-05-06 17:08:35 -04:00
|
|
|
get ':id/-/packages/composer/packages', urgency: :low do
|
2020-07-17 02:09:11 -04:00
|
|
|
presenter.root
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Composer packages endpoint at group level for packages list'
|
|
|
|
params do
|
|
|
|
requires :sha, type: String, desc: 'Shasum of current json'
|
|
|
|
end
|
2021-12-14 10:17:06 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2022-05-06 17:08:35 -04:00
|
|
|
get ':id/-/packages/composer/p/:sha', urgency: :low do
|
2020-07-17 02:09:11 -04:00
|
|
|
presenter.provider
|
|
|
|
end
|
|
|
|
|
2021-03-18 20:09:00 -04:00
|
|
|
desc 'Composer v2 packages p2 endpoint at group level for package versions metadata'
|
2020-07-17 02:09:11 -04:00
|
|
|
params do
|
|
|
|
requires :package_name, type: String, file_path: true, desc: 'The Composer package name'
|
|
|
|
end
|
2021-12-14 10:17:06 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2022-05-06 17:08:35 -04:00
|
|
|
get ':id/-/packages/composer/p2/*package_name', requirements: COMPOSER_ENDPOINT_REQUIREMENTS, file_path: true, urgency: :low do
|
2021-03-10 07:09:14 -05:00
|
|
|
not_found! if packages.empty?
|
|
|
|
|
|
|
|
presenter.package_versions
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Composer packages endpoint at group level for package versions metadata'
|
|
|
|
params do
|
|
|
|
requires :package_name, type: String, file_path: true, desc: 'The Composer package name'
|
|
|
|
end
|
2021-12-14 10:17:06 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2022-05-06 17:08:35 -04:00
|
|
|
get ':id/-/packages/composer/*package_name', requirements: COMPOSER_ENDPOINT_REQUIREMENTS, file_path: true, urgency: :low do
|
2020-07-17 02:09:11 -04:00
|
|
|
not_found! if packages.empty?
|
2020-12-15 07:10:17 -05:00
|
|
|
not_found! if params[:sha].blank?
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
presenter.package_versions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
desc 'Composer packages endpoint for registering packages'
|
|
|
|
namespace ':id/packages/composer' do
|
2021-12-14 10:17:06 -05:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
params do
|
|
|
|
optional :branch, type: String, desc: 'The name of the branch'
|
|
|
|
optional :tag, type: String, desc: 'The name of the tag'
|
|
|
|
exactly_one_of :tag, :branch
|
|
|
|
end
|
2022-05-06 17:08:35 -04:00
|
|
|
post urgency: :low do
|
2020-07-17 02:09:11 -04:00
|
|
|
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
|
2020-12-03 19:09:55 -05:00
|
|
|
.new(authorized_user_project, current_user, declared_params.merge(build: current_authenticated_job))
|
2020-07-17 02:09:11 -04:00
|
|
|
.execute
|
|
|
|
|
2021-10-18 11:12:11 -04:00
|
|
|
track_package_event('push_package', :composer, project: authorized_user_project, user: current_user, namespace: authorized_user_project.namespace)
|
|
|
|
|
2020-07-17 02:09:11 -04:00
|
|
|
created!
|
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :sha, type: String, desc: 'Shasum of current json'
|
|
|
|
requires :package_name, type: String, file_path: true, desc: 'The Composer package name'
|
|
|
|
end
|
2022-04-18 14:10:36 -04:00
|
|
|
route_setting :authentication, job_token_allowed: true, basic_auth_personal_access_token: true, deploy_token_allowed: true
|
2022-05-06 17:08:35 -04:00
|
|
|
get 'archives/*package_name', urgency: :default do
|
2022-04-18 14:10:36 -04:00
|
|
|
authorize_read_package!(authorized_user_project)
|
|
|
|
|
2022-09-14 14:12:48 -04:00
|
|
|
package = authorized_user_project
|
2020-07-17 02:09:11 -04:00
|
|
|
.packages
|
|
|
|
.composer
|
|
|
|
.with_name(params[:package_name])
|
|
|
|
.with_composer_target(params[:sha])
|
|
|
|
.first
|
2022-09-14 14:12:48 -04:00
|
|
|
metadata = package&.composer_metadatum
|
2020-07-17 02:09:11 -04:00
|
|
|
|
|
|
|
not_found! unless metadata
|
|
|
|
|
2022-04-18 14:10:36 -04:00
|
|
|
track_package_event('pull_package', :composer, project: authorized_user_project, namespace: authorized_user_project.namespace)
|
2022-09-14 14:12:48 -04:00
|
|
|
package.touch_last_downloaded_at
|
2021-04-06 23:09:01 -04:00
|
|
|
|
2022-04-18 14:10:36 -04:00
|
|
|
send_git_archive authorized_user_project.repository, ref: metadata.target_sha, format: 'zip', append_sha: true
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|