diff --git a/scripts/api/delete_e2e_subgroups.rb b/scripts/api/delete_e2e_subgroups.rb new file mode 100755 index 0000000000000000000000000000000000000000..253d0e22945bbc0c2691592fdfb20fc7fe30b20c --- /dev/null +++ b/scripts/api/delete_e2e_subgroups.rb @@ -0,0 +1,71 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'optparse' +require_relative 'base' + +class DeleteE2eSubgroups < Base + def initialize(options) + super + @sandbox = options.delete(:sandbox) + @max_subgroups_to_process = options.delete(:max_subgroups_to_process) + end + + def execute + deletion_responses = {} + + client.group_subgroups(sandbox, per_page: 20, active: false).auto_paginate do |subgroup| + next unless subgroup.marked_for_deletion_on + + if deletion_responses.size >= max_subgroups_to_process + puts "\nMaximum subgroups to delete reached (#{max_subgroups_to_process})!" + break + end + + response = client.delete("/groups/#{subgroup.id}?permanently_remove=true&full_path=#{subgroup.full_path}") + puts "#{deletion_responses.size + 1}. Destroying subgroup (#{subgroup.full_path}): " \ + "#{response.message == '202 Accepted' ? 'SUCCESS' : 'FAILURE'}..." + + deletion_responses[subgroup.full_path] = response.message + end + + success, failures = deletion_responses.partition { |_k, v| v == '202 Accepted' } + puts "\n\nGroup deletion results:" + + puts "Successes: #{success.size}" + puts "Failures: #{failures.size}" + end + + private + + attr_reader :sandbox, :max_subgroups_to_process +end + +if $PROGRAM_NAME == __FILE__ + options = API::DEFAULT_OPTIONS.dup + + OptionParser.new do |opts| + opts.on("-s", "--sandbox SANDBOX", String, "Sandbox in which deleting subgroups") do |value| + options[:sandbox] = value + end + opts.on("-m", "--max MAX", Integer, "Maximum subgroups to delete") do |value| + options[:max_subgroups_to_process] = value + end + + opts.on("-t", "--api-token API_TOKEN", String, "A value API token with the `api` scope") do |value| + options[:api_token] = value + end + + opts.on("-E", "--endpoint ENDPOINT", String, "The API endpoint for the API token. " \ + "(defaults to $CI_API_V4_URL and fallback to https://gitlab.com/api/v4)") do |value| + options[:endpoint] = value + end + + opts.on("-h", "--help", "Prints this help") do + puts opts + exit + end + end.parse! + + DeleteE2eSubgroups.new(options).execute +end