Add AR client bulk_delete_versions

What does this MR do and why?

This MR adds #bulk_delete_versions to the Artifact Registry (AR) HTTP client in ee/lib/artifact_registry/client.rb. This is Step 4 of the artifact delete surface plan. The method deletes a chosen list of versions of one Maven or npm package in a single request.

#bulk_delete_versions(slug:, repository_name:, format:, package_id:, version_ids:) sends a POST with {"version_ids": [...]} to .../{format}/packages/{package_id}/versions/bulk_delete. The shape follows monolith/S14, sections "The bulk selector" and "Ruby client":

  • It sends only the subset branch of the contract: a list of ids under its own keyword. It has no delete_all: keyword. No surface sends the whole-collection branch, and no confirmation design exists for it. A whole-collection permanent delete that anyone can reach with a direct call is the same exposure monolith/S05 closes for its repository clear.
  • It raises ArgumentError, without making a request, if the list is empty or has more than 1000 entries. A list of exactly 1000 entries is sent.
  • Only Maven and npm are accepted (PACKAGE_FORMATS). A blank or dot-segment slug, repository name, or package id raises before any request. The package id is percent-encoded.

How it behaves against AR:

  • POST is not in RETRY_OPTIONS[:methods], so the transport never replays a batch. A 503 means AR recorded no job, so the caller can safely resubmit the same list.
  • A 413 means the body's size in bytes went over AR's per-route limit. This is a different limit from the 1000-entry count.
  • The client checks only the count. AR's 400 response handles entry syntax. Repeated entries are sent as given, because AR counts them toward the cap and applies the list as a set.
  • A 202 has no body, and the method returns nil. Any other 2xx raises UnavailableError as contract drift.
  • A 404 raises ApiError and does not become nil. A delete that never happened must not look like a success.

Two private helpers support the method:

  • guard_batch!(name, list, max) refuses a non-Array, an empty list, or more than max entries before any request. The bulk delete passes MAX_BULK_DELETE_BATCH (1000, the contract's maxItems). The existing verifications guard now calls it too, with MAX_VERIFICATION_BATCH, instead of repeating the same check; its message changes from "... 1000 ids" to "... 1000 entries", and its specs match /between 1 and/.
  • nested_bulk_delete_path(...) adds /bulk_delete to the existing sub_collection_path. The existing bulk_delete_path only builds the collection-level route.

The stacked MR !256223 (merged) (Step 5) reuses both helpers.

This ships dark behind the artifact_registry_ui feature flag, with no GraphQL surface yet, so there is no changelog trailer.

Size: 261 lines changed. client.rb has 36 added and 3 removed, client_spec.rb has 220 added, and revoke_role_assignments_service_spec.rb has a one-line message update.

Changes since the earlier draft

  • Rebased onto master. There was one conflict, two insertions next to each other near dissociate_upstream_repository. I kept both.
  • Removed the delete_all: keyword, the two-branch selector, and its refusal matrix (both, neither, false, non-boolean). The method now takes version_ids: only.
  • Renamed the 404 test from "unknown package" to "unknown repository". The real AR accepts an unknown package id with 202 and returns 404 only for an unknown repository.
  • Fixed the description. It used to say the keyword was ids:, but it is version_ids:.
  • Review round 1 (b972915b): guard_batch! is shared with the verifications guard, raise_unexpected_success passes only slug: like the sibling deletes, the spec pins 401 and 403 as AuthorizationError and runs the happy path for both maven and npm, the refusal and AR-answered examples are tables, and the retry comment now names the retry policy rather than a double-apply risk.
  • Review round 2 (5163914d): the refusal rows are labelled with case_name, so the over-cap example is not named after 1001 ids, and the over-cap row is one id repeated 1001 times. The within-cap rows are sized by count through one canonical_uuids helper, the stale first-caller comment is gone, and the revoke service spec's stubbed error now uses the message the client raises.

The plan's Step 4 still lists #bulk_delete_files and the delete_all branch. On this point the spec replaces the plan. Per-file delete is no longer part of the product: delete_file was removed in !253712 (merged). The plan fix is a separate docs(plans) follow-up listed in gitlab-org/ops/artifact-registry!2798 (merged), and it does not block this MR.

References

Screenshots or screen recordings

Not applicable: a Ruby client method with no caller yet, no UI change.

How to set up and validate locally

The script starts a throwaway local stand-in for AR on a random port, points the client at it, and prints OK or FAIL for each check. It needs no running AR and changes no data. Paste it into bin/rails console on this branch.

require 'socket'

# A throwaway local AR stand-in. It records every request and answers by repository name:
# 'maven-repo' accepts with 202, 'no-such-repo' answers 404, and a non-UUID entry answers 400.
requests = []
server = TCPServer.new('127.0.0.1', 0)
port = server.addr[1]
thread = Thread.new do
  loop do
    socket = server.accept
    method, path = socket.gets.split
    length = 0
    while (line = socket.gets) && line != "\r\n"
      length = line.split(':', 2).last.to_i if line.downcase.start_with?('content-length:')
    end
    body = length > 0 ? socket.read(length) : ''
    requests << { method: method, path: path, body: body }
    status, payload =
      if path.include?('/no-such-repo/') then [404, { error: { code: 'not_found', message: 'repository not found' } }]
      elsif body.include?('not-a-uuid') then [400, { error: { code: 'invalid_argument', message: 'bad entry' } }]
      else [202, nil]
      end
    json = payload ? payload.to_json : ''
    socket.write("HTTP/1.1 #{status} X\r\nContent-Type: application/json\r\n" \
      "Content-Length: #{json.bytesize}\r\nConnection: close\r\n\r\n#{json}")
    socket.close
  end
end

token_exchange = Struct.new(:token) { def token_for(*) = token }.new('per-user-token')
client = ArtifactRegistry::Client.new(base_url: "http://127.0.0.1:#{port}", current_user: User.first,
  token_exchange: token_exchange)
check = ->(label, ok) { puts "#{ok ? 'OK  ' : 'FAIL'} #{label}" }
refused = lambda do |&blk|
  before = requests.size
  blk.call
  false
rescue ArgumentError => e
  requests.size == before && e.message
end

ids = %w[01a13405-80ac-7ec5-8c1f-f27858ef083e 01a13405-8168-7e01-97af-8296d0fd8ced]
base = { slug: 'gdk', repository_name: 'maven-repo', format: 'maven', package_id: 'pkg-1' }

check.('a subset delete returns nil on 202', client.bulk_delete_versions(**base, version_ids: ids).nil?)
sent = requests.last
check.('it POSTs to the nested versions bulk route',
  sent[:method] == 'POST' && sent[:path].end_with?('/maven-repo/maven/packages/pkg-1/versions/bulk_delete'))
check.('the body is the version_ids list and nothing else', sent[:body] == { version_ids: ids }.to_json)

check.('a 1000-entry list is sent', client.bulk_delete_versions(**base,
  version_ids: Array.new(1000) { SecureRandom.uuid }).nil?)
check.('an empty list is refused before any request', refused.call { client.bulk_delete_versions(**base, version_ids: []) })
check.('a 1001-entry list is refused before any request', refused.call do
  client.bulk_delete_versions(**base, version_ids: Array.new(1001) { SecureRandom.uuid })
end)
check.('delete_all is not accepted', refused.call do
  client.bulk_delete_versions(**base, version_ids: ids, delete_all: true)
end.to_s.include?('unknown keyword: :delete_all'))
check.('a container format is refused before any request', refused.call do
  client.bulk_delete_versions(**base, format: 'docker', version_ids: ids)
end)

begin
  client.bulk_delete_versions(**base, version_ids: ['not-a-uuid'])
  check.('an entry AR rejects surfaces the 400', false)
rescue ArtifactRegistry::Client::ApiError => e
  check.('an entry AR rejects surfaces the 400', e.status == 400)
end

begin
  client.bulk_delete_versions(**base, repository_name: 'no-such-repo', version_ids: ids)
  check.('an unknown repository surfaces the 404, not nil', false)
rescue ArtifactRegistry::Client::ApiError => e
  check.('an unknown repository surfaces the 404, not nil', e.status == 404)
end

check.('no request body ever carried delete_all', requests.none? { |r| r[:body].include?('delete_all') })
puts "#{requests.size} requests reached the stand-in"

thread.kill
server.close

Expected output: eleven OK lines followed by "4 requests reached the stand-in".

Tests and live check

Tests: 24 examples cover the method, and the full client spec file passes (1096 examples), as do the Artifact Registry authz service and mutation specs that call the verifications guard. I mutation-checked the guards. Each of these changes fails at least one example:

  • removing the count guard
  • re-adding a delete_all: keyword
  • adding a stray delete_all body key
  • de-duplicating the list
  • lowering the cap to 999
  • accepting maven only
  • re-raising a 401 or 403 as ApiError
  • dropping the verifications call to guard_batch!

Live check: I ran the client against a real local AR built from AR main (commit e6e3e7deb) in development mode.

  • Published four Maven versions of one package. A subset delete of two versions returned nil, and the list afterwards showed only the other two.
  • Re-sending an id that was already deleted returned nil, since a missing entry is a no-op for AR.
  • A malformed entry came back as AR's 400.
  • An unknown package id was accepted with 202. An unknown repository answers 404, which I checked directly against the route.
  • An empty list, a 1001-entry list, a delete_all: keyword, and a docker format were refused locally and never reached AR. The AR log shows only the four requests the client sent.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Narendran

Merge request reports

Loading
Loading