Skip to content

Mutation to delete multiple package files

Steve Abrams requested to merge 342436-package-file-bulk-delete-graphql into master

🏗 What does this MR do and why?

We introduce a new GraphQL mutation for deleting package files in bulk: DestroyPackageFiles

In this mutation we have a few constraints:

  • Files being requested for deletion must all be within the same project
  • A maximum of 100 files can be requested in one request
  • The user must have :destroy_package permission, which requires maintainer role on the project.

🎥 Screenshots or screen recordings

Scenario Screenshot
Successful request Screen_Shot_2022-06-14_at_4.09.20_PM
Without permission Screen_Shot_2022-06-14_at_3.46.10_PM
With files from another project Screen_Shot_2022-06-14_at_4.05.49_PM
With more than 100 files Screen_Shot_2022-06-14_at_4.15.46_PM

💻 How to set up and validate locally

🗺 Setup

  1. Pick a project, note its ID and full path. This can be done in the rails console:
    Project.first.id
    Project.first.full_path
  2. In your terminal create some files to upload to the package registry (if you have packages with files in your project already you can skip this step)
    echo hello > foo.txt
    echo goodbye > bar.txt
  3. In the terminal, push the files to the package registry. You will need a personal access token to do this: These commands assume you are making the request from the directory you just created the files in
    curl --header "PRIVATE-TOKEN: <your_access_token>" \
      --upload-file foo.txt \
      "http://gdk.test:3000/api/v4/projects/<project_id>/packages/generic/my_package/0.0.1/foo.txt"
    curl --header "PRIVATE-TOKEN: <your_access_token>" \
      --upload-file bar.txt \
      "http://gdk.test:3000/api/v4/projects/<project_id>/packages/generic/other_package/0.0.2/bar.txt"
  4. You should be able to see the files in the rails console:
    Packages::PackageFile.last(2)
  5. Note the global ids of the files you created to be used in the GraphQL query:
    Packages::PackageFile.last(2).map { |pf| pf.to_global_id.to_s }
    Or if you know the package_file IDs, you can use the format: "gid://gitlab/Packages::PackageFile/<id>"

Test

  1. Make sure you are logged in to your local instance with a user that has maintainer or above access to the project you chose.
  2. Using the graphql explorer (http://gdk.test:3000/-/graphql-explorer, make a request to delete the package files:
    mutation {
      destroyPackageFiles(input: {
        projectPath: "<your-project-path>",
        ids: ["<package_file_global_id_1>", "<package_file_global_id_2>"]
      }) {
        errors
      }
    }
  3. The response should not include any errors.
  4. Checking in the rails console, the package files should now have status: "pending_destruction" (The files are removed separately with a background job).

🛃 MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #342436 (closed)

Edited by Steve Abrams

Merge request reports