From c73a4dd0d7bd90f82c9a32173f83e64ac0df30e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=C5=91d=20Ill=C3=A9s?= Date: Mon, 5 Dec 2022 19:23:24 +0100 Subject: [PATCH] EOL branch delete script speed up The script originally clones every repository and branch, which takes long time. This patch queries GITEA instead whether the branch exists there and only clones a repository if it is necessary. Change-Id: I0e164e15a5157f6e6a42ef7ca154f207308641b7 --- tools/delete_stable_branch.py | 66 ++++++++++++++++++++++++++++---- tools/list_eol_stale_branches.sh | 7 ++-- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/tools/delete_stable_branch.py b/tools/delete_stable_branch.py index edb795415d..491ea52462 100755 --- a/tools/delete_stable_branch.py +++ b/tools/delete_stable_branch.py @@ -18,11 +18,14 @@ import argparse import getpass +import json +import sys import requests GERRIT_URL = 'https://review.opendev.org/' +GITEA_URL = 'https://opendev.org/api/v1' def delete_branch(username, project_name, branch_id): @@ -37,27 +40,74 @@ def delete_branch(username, project_name, branch_id): if response.status_code == 204: print(f'Branch stable/{branch_id} successfully deleted ' f'from {project_name}!') + return 0 elif response.status_code == 401: print('401 Unauthorized.') + return 1 else: # NOTE(elod.illes): other possible errors from gerrit: # 404: In case of project or branch is not found # 409: Branch has open changes print(f'Delete failed ({response.status_code}): {response.text}') + return 2 + + +def is_branch_open(project_name, branch_id, quiet): + url = (f'{GITEA_URL}/repos/{project_name.replace("/", "%2F")}/' + f'branches/stable%2F{branch_id}') + response = requests.get(url) + try: + response_details = response.json() + except json.decoder.JSONDecodeError as exc: + print(f'ERROR: JSON decode failed ({exc})') + print(f'ERROR: ({response.status_code}): {response.text}') + print('Is the project name correct, like "openstack/nova"?') + return 4 + if response.status_code == 200: + if not quiet: + print(f'stable/{branch_id} exists in {project_name}.') + return 0 + elif ((response.status_code == 404) and + (response_details['errors'] == [f'branch does not exist [name: stable/{branch_id}]']) and + (response_details['message'] == "The target couldn't be found.")): + if not quiet: + print(f'stable/{branch_id} does not exist in {project_name}.') + return 1 + else: + print(f'ERROR: ({response.status_code}): {response.text}') + return 2 def main(): parser = argparse.ArgumentParser( description='Deletes stable/ from .') - parser.add_argument('username', help='Gerrit Username') - parser.add_argument('project', help='Project to delete from') - parser.add_argument('branch', help='Branch to delete') + subparsers = parser.add_subparsers(required=True, dest='command', + metavar='{delete,check}') + + delete_parser = subparsers.add_parser( + 'delete', help='Delete stable/ from ') + delete_parser.add_argument('username', help='Gerrit Username') + delete_parser.add_argument('project', help='Project to delete from') + delete_parser.add_argument('branch', help='Branch to delete') + + check_parser = subparsers.add_parser( + 'check', help='Check if stable/ exists for ') + check_parser.add_argument('project', help='Project to check') + check_parser.add_argument('branch', help='Branch to check if exists') + check_parser.add_argument( + '-q', '--quiet', action='store_true', + help='Return code only (0 means branch exists)') + args = parser.parse_args() - delete_branch( - args.username, - args.project, - args.branch.replace('stable/', '')) + if args.command == 'delete': + return delete_branch(args.username, + args.project, + args.branch.replace('stable/', '')) + elif args.command == 'check': + return is_branch_open(args.project, + args.branch.replace('stable/', ''), + args.quiet) if __name__ == '__main__': - main() + sys.exit(main()) diff --git a/tools/list_eol_stale_branches.sh b/tools/list_eol_stale_branches.sh index 5df2416e43..e9c3bf8dca 100755 --- a/tools/list_eol_stale_branches.sh +++ b/tools/list_eol_stale_branches.sh @@ -86,18 +86,19 @@ function eol_tag_matches_head { } function is_eol { - clone_repo ${repo} stable/${em_branch} - cd ${repo} && git checkout -f -q stable/${em_branch} 2>/dev/null + ${TOOLSDIR}/delete_stable_branch.py check --quiet ${repo} ${em_branch} if [[ $? -eq 0 ]]; then echo echo "${repo} contains eol stale branch (${em_branch})" + clone_repo ${repo} stable/${em_branch} + cd ${repo} if no_open_patches && eol_tag_matches_head; then read -p "> Do you want to delete the branch stable/${em_branch} from ${repo} repository? [y/N]: " YN if [ "${YN,,}" == "y" ]; then if [ -z "$gerrit_username" ]; then read -p "Gerrit username: " gerrit_username fi - ${TOOLSDIR}/delete_stable_branch.py ${gerrit_username} ${repo} ${em_branch} + ${TOOLSDIR}/delete_stable_branch.py delete ${gerrit_username} ${repo} ${em_branch} fi fi cd ..