2019-01-10 14:22:58 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2019-08-05 20:00:50 +00:00
|
|
|
class ProjectContainerRepositories < Grape::API
|
2019-01-10 14:22:58 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2019-08-05 20:00:50 +00:00
|
|
|
REPOSITORY_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(
|
2019-01-10 14:22:58 +00:00
|
|
|
tag_name: API::NO_SLASH_URL_PART_REGEX)
|
|
|
|
|
|
|
|
before { error!('404 Not Found', 404) unless Feature.enabled?(:container_registry_api, user_project, default_enabled: true) }
|
|
|
|
before { authorize_read_container_images! }
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
desc 'Get a project container repositories' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
success Entities::ContainerRegistry::Repository
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
2019-08-05 20:00:50 +00:00
|
|
|
optional :tags, type: Boolean, default: false, desc: 'Determines if tags should be included'
|
2019-01-10 14:22:58 +00:00
|
|
|
end
|
|
|
|
get ':id/registry/repositories' do
|
2019-08-05 20:00:50 +00:00
|
|
|
repositories = ContainerRepositoriesFinder.new(
|
2019-11-04 15:07:36 +00:00
|
|
|
user: current_user, subject: user_project
|
2019-08-05 20:00:50 +00:00
|
|
|
).execute
|
2019-01-10 14:22:58 +00:00
|
|
|
|
2019-11-12 09:06:14 +00:00
|
|
|
track_event( 'list_repositories')
|
|
|
|
|
2019-08-05 20:00:50 +00:00
|
|
|
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags]
|
2019-01-10 14:22:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Delete repository' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :repository_id, type: Integer, desc: 'The ID of the repository'
|
|
|
|
end
|
2019-08-05 20:00:50 +00:00
|
|
|
delete ':id/registry/repositories/:repository_id', requirements: REPOSITORY_ENDPOINT_REQUIREMENTS do
|
2019-01-10 14:22:58 +00:00
|
|
|
authorize_admin_container_image!
|
|
|
|
|
2020-02-14 21:09:08 +00:00
|
|
|
DeleteContainerRepositoryWorker.perform_async(current_user.id, repository.id) # rubocop:disable CodeReuse/Worker
|
2019-11-12 09:06:14 +00:00
|
|
|
track_event('delete_repository')
|
2019-01-10 14:22:58 +00:00
|
|
|
|
|
|
|
status :accepted
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a list of repositories tags' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
success Entities::ContainerRegistry::Tag
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :repository_id, type: Integer, desc: 'The ID of the repository'
|
|
|
|
use :pagination
|
|
|
|
end
|
2019-08-05 20:00:50 +00:00
|
|
|
get ':id/registry/repositories/:repository_id/tags', requirements: REPOSITORY_ENDPOINT_REQUIREMENTS do
|
2019-01-10 14:22:58 +00:00
|
|
|
authorize_read_container_image!
|
|
|
|
|
|
|
|
tags = Kaminari.paginate_array(repository.tags)
|
2019-11-12 09:06:14 +00:00
|
|
|
track_event('list_tags')
|
|
|
|
|
2019-01-10 14:22:58 +00:00
|
|
|
present paginate(tags), with: Entities::ContainerRegistry::Tag
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Delete repository tags (in bulk)' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :repository_id, type: Integer, desc: 'The ID of the repository'
|
2020-03-02 21:08:01 +00:00
|
|
|
optional :name_regex_delete, type: String, desc: 'The tag name regexp to delete, specify .* to delete all'
|
|
|
|
optional :name_regex, type: String, desc: 'The tag name regexp to delete, specify .* to delete all'
|
2020-03-06 09:08:13 +00:00
|
|
|
# require either name_regex (deprecated) or name_regex_delete, it is ok to have both
|
|
|
|
at_least_one_of :name_regex, :name_regex_delete
|
2020-03-02 21:08:01 +00:00
|
|
|
optional :name_regex_keep, type: String, desc: 'The tag name regexp to retain'
|
2019-01-10 14:22:58 +00:00
|
|
|
optional :keep_n, type: Integer, desc: 'Keep n of latest tags with matching name'
|
|
|
|
optional :older_than, type: String, desc: 'Delete older than: 1h, 1d, 1month'
|
|
|
|
end
|
2019-08-05 20:00:50 +00:00
|
|
|
delete ':id/registry/repositories/:repository_id/tags', requirements: REPOSITORY_ENDPOINT_REQUIREMENTS do
|
2019-01-10 14:22:58 +00:00
|
|
|
authorize_admin_container_image!
|
|
|
|
|
2019-06-18 22:08:30 +00:00
|
|
|
message = 'This request has already been made. You can run this at most once an hour for a given container repository'
|
|
|
|
render_api_error!(message, 400) unless obtain_new_cleanup_container_lease
|
|
|
|
|
2020-02-14 21:09:08 +00:00
|
|
|
# rubocop:disable CodeReuse/Worker
|
2019-01-10 14:22:58 +00:00
|
|
|
CleanupContainerRepositoryWorker.perform_async(current_user.id, repository.id,
|
2020-02-13 15:08:52 +00:00
|
|
|
declared_params.except(:repository_id).merge(container_expiration_policy: false))
|
2020-02-14 21:09:08 +00:00
|
|
|
# rubocop:enable CodeReuse/Worker
|
2019-01-10 14:22:58 +00:00
|
|
|
|
2019-11-12 09:06:14 +00:00
|
|
|
track_event('delete_tag_bulk')
|
|
|
|
|
2019-01-10 14:22:58 +00:00
|
|
|
status :accepted
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a details about repository tag' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
success Entities::ContainerRegistry::TagDetails
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :repository_id, type: Integer, desc: 'The ID of the repository'
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
end
|
2019-08-05 20:00:50 +00:00
|
|
|
get ':id/registry/repositories/:repository_id/tags/:tag_name', requirements: REPOSITORY_ENDPOINT_REQUIREMENTS do
|
2019-01-10 14:22:58 +00:00
|
|
|
authorize_read_container_image!
|
|
|
|
validate_tag!
|
|
|
|
|
|
|
|
present tag, with: Entities::ContainerRegistry::TagDetails
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Delete repository tag' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.8.'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :repository_id, type: Integer, desc: 'The ID of the repository'
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
end
|
2019-08-05 20:00:50 +00:00
|
|
|
delete ':id/registry/repositories/:repository_id/tags/:tag_name', requirements: REPOSITORY_ENDPOINT_REQUIREMENTS do
|
2019-01-10 14:22:58 +00:00
|
|
|
authorize_destroy_container_image!
|
|
|
|
validate_tag!
|
|
|
|
|
2019-09-19 11:50:12 +00:00
|
|
|
result = ::Projects::ContainerRepository::DeleteTagsService
|
|
|
|
.new(repository.project, current_user, tags: [declared_params[:tag_name]])
|
|
|
|
.execute(repository)
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2019-11-12 09:06:14 +00:00
|
|
|
track_event('delete_tag')
|
|
|
|
|
2019-09-19 11:50:12 +00:00
|
|
|
status :ok
|
|
|
|
else
|
|
|
|
status :bad_request
|
|
|
|
end
|
2019-01-10 14:22:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def authorize_read_container_images!
|
|
|
|
authorize! :read_container_image, user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_read_container_image!
|
|
|
|
authorize! :read_container_image, repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_destroy_container_image!
|
2019-06-17 11:13:03 +00:00
|
|
|
authorize! :destroy_container_image, repository
|
2019-01-10 14:22:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_container_image!
|
|
|
|
authorize! :admin_container_image, repository
|
|
|
|
end
|
|
|
|
|
2019-06-18 22:08:30 +00:00
|
|
|
def obtain_new_cleanup_container_lease
|
|
|
|
Gitlab::ExclusiveLease
|
|
|
|
.new("container_repository:cleanup_tags:#{repository.id}",
|
|
|
|
timeout: 1.hour)
|
|
|
|
.try_obtain
|
|
|
|
end
|
|
|
|
|
2019-01-10 14:22:58 +00:00
|
|
|
def repository
|
|
|
|
@repository ||= user_project.container_repositories.find(params[:repository_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag
|
|
|
|
@tag ||= repository.tag(params[:tag_name])
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_tag!
|
|
|
|
not_found!('Tag') unless tag.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|