debian-mirror-gitlab/app/services/projects/container_repository/destroy_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
module Projects
module ContainerRepository
class DestroyService < BaseService
2023-03-04 22:38:38 +05:30
CLEANUP_TAGS_SERVICE_PARAMS = {
'name_regex_delete' => '.*',
'container_expiration_policy' => true, # to avoid permissions checks
'keep_latest' => false
}.freeze
def execute(container_repository, disable_timeout: true)
2023-04-23 21:23:45 +05:30
return error('Unauthorized access') unless can_destroy?
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
# Delete tags outside of the transaction to avoid hitting an idle-in-transaction timeout
2023-04-23 21:23:45 +05:30
if delete_tags(container_repository, disable_timeout) &&
2023-03-04 22:38:38 +05:30
destroy_container_repository(container_repository)
2023-04-23 21:23:45 +05:30
success
else
2023-03-04 22:38:38 +05:30
container_repository.delete_failed!
2023-04-23 21:23:45 +05:30
error('Deletion failed for container repository')
2023-03-04 22:38:38 +05:30
end
end
private
def delete_tags(container_repository, disable_timeout)
service = Projects::ContainerRepository::CleanupTagsService.new(
container_repository: container_repository,
params: CLEANUP_TAGS_SERVICE_PARAMS.merge('disable_timeout' => disable_timeout)
)
result = service.execute
return true if result[:status] == :success
log_error(error_message(container_repository, 'error in deleting tags'))
false
end
def destroy_container_repository(container_repository)
return true if container_repository.destroy
log_error(error_message(container_repository, container_repository.errors.full_messages.join('. ')))
false
end
2023-04-23 21:23:45 +05:30
def can_destroy?
return true if skip_permission_check?
can?(current_user, :destroy_container_image, project)
end
2023-03-04 22:38:38 +05:30
def error_message(container_repository, message)
2023-04-23 21:23:45 +05:30
"Container repository with ID: #{container_repository.id} and path: #{container_repository.path} " \
"failed with message: #{message}"
end
def skip_permission_check?
!!params[:skip_permission_check]
2018-11-20 20:47:30 +05:30
end
end
end
end