Commit 308f2a98 authored by Dan Davison's avatar Dan Davison

Merge branch 'ml-fix-delete-subgroups-rake-task' into 'master'

Allow subgroup deletion total-pages is unknown

See merge request gitlab-org/gitlab!71527
parents 6b946923 97d705f9
...@@ -20,16 +20,10 @@ module QA ...@@ -20,16 +20,10 @@ module QA
end end
def run def run
$stdout.puts 'Running...' $stdout.puts 'Fetching subgroups for deletion...'
# Fetch group's id sub_group_ids = fetch_subgroup_ids
group_id = fetch_group_id $stdout.puts "\nNumber of Sub Groups not already marked for deletion: #{sub_group_ids.length}"
sub_groups_head_response = head Runtime::API::Request.new(@api_client, "/groups/#{group_id}/subgroups", per_page: "100").url
total_sub_group_pages = sub_groups_head_response.headers[:x_total_pages]
sub_group_ids = fetch_subgroup_ids(group_id, total_sub_group_pages)
$stdout.puts "Number of Sub Groups not already marked for deletion: #{sub_group_ids.length}"
delete_subgroups(sub_group_ids) unless sub_group_ids.empty? delete_subgroups(sub_group_ids) unless sub_group_ids.empty?
$stdout.puts "\nDone" $stdout.puts "\nDone"
...@@ -52,12 +46,20 @@ module QA ...@@ -52,12 +46,20 @@ module QA
JSON.parse(group_search_response.body)["id"] JSON.parse(group_search_response.body)["id"]
end end
def fetch_subgroup_ids(group_id, group_pages) def fetch_subgroup_ids
group_id = fetch_group_id
sub_groups_ids = [] sub_groups_ids = []
page_no = '1'
# When we reach the last page, the x-next-page header is a blank string
while page_no.present?
$stdout.print '.'
sub_groups_response = get Runtime::API::Request.new(@api_client, "/groups/#{group_id}/subgroups", page: page_no, per_page: '100').url
sub_groups_ids.concat(JSON.parse(sub_groups_response.body)
.reject { |subgroup| !subgroup["marked_for_deletion_on"].nil? }.map { |subgroup| subgroup['id'] })
group_pages.to_i.times do |page_no| page_no = sub_groups_response.headers[:x_next_page].to_s
sub_groups_response = get Runtime::API::Request.new(@api_client, "/groups/#{group_id}/subgroups", page: (page_no + 1).to_s, per_page: "100").url
sub_groups_ids.concat(JSON.parse(sub_groups_response.body).reject { |subgroup| !subgroup["marked_for_deletion_on"].nil? }.map { |subgroup| subgroup["id"] })
end end
sub_groups_ids.uniq sub_groups_ids.uniq
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment