diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
commit | 0ea3fcec397b69815975647f5e2aa5fe944a8486 (patch) | |
tree | 7979381b89d26011bcf9bdc989a40fcc2f1ed4ff /app/graphql/mutations/packages/destroy_files.rb | |
parent | 72123183a20411a36d607d70b12d57c484394c8e (diff) |
Add latest changes from gitlab-org/gitlab@15-1-stable-eev15.1.0-rc42
Diffstat (limited to 'app/graphql/mutations/packages/destroy_files.rb')
-rw-r--r-- | app/graphql/mutations/packages/destroy_files.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app/graphql/mutations/packages/destroy_files.rb b/app/graphql/mutations/packages/destroy_files.rb new file mode 100644 index 00000000000..3900a2c46ae --- /dev/null +++ b/app/graphql/mutations/packages/destroy_files.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Mutations + module Packages + class DestroyFiles < ::Mutations::BaseMutation + graphql_name 'DestroyPackageFiles' + + include FindsProject + + MAXIMUM_FILES = 100 + + authorize :destroy_package + + argument :project_path, + GraphQL::Types::ID, + required: true, + description: 'Project path where the packages cleanup policy is located.' + + argument :ids, + [::Types::GlobalIDType[::Packages::PackageFile]], + required: true, + description: 'IDs of the Package file.' + + def resolve(project_path:, ids:) + project = authorized_find!(project_path) + raise_resource_not_available_error! "Cannot delete more than #{MAXIMUM_FILES} files" if ids.size > MAXIMUM_FILES + + package_files = ::Packages::PackageFile.where(id: parse_gids(ids)) # rubocop:disable CodeReuse/ActiveRecord + + ensure_file_access!(project, package_files) + + result = ::Packages::MarkPackageFilesForDestructionService.new(package_files).execute + + errors = result.error? ? Array.wrap(result[:message]) : [] + + { errors: errors } + end + + private + + def ensure_file_access!(project, package_files) + project_ids = package_files.map(&:project_id).uniq + + unless project_ids.size == 1 && project_ids.include?(project.id) + raise_resource_not_available_error! 'All files must be in the requested project' + end + end + + def parse_gids(gids) + gids.map { |gid| GitlabSchema.parse_gid(gid, expected_type: ::Packages::PackageFile).model_id } + end + end + end +end |