From 6a8941429f3c34f0c4fc732cb1af1010ee85ef62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=C5=91d=20Ill=C3=A9s?= Date: Wed, 24 Feb 2021 19:11:41 +0100 Subject: [PATCH] Add delete option to list_eol_stale_branches.sh This patch adds a question whether to delete a stale eol branch if it does not contain open patches. The deletion is done via Gerrit API, which requires 'delete reference' access category for the user who is running this script. Change-Id: Iea33c2bd4bd20dd0f92fd9a7e12008d841176974 --- tools/delete_stable_branch.py | 63 ++++++++++++++++++++++++++++++++ tools/list_eol_stale_branches.sh | 14 ++++++- 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100755 tools/delete_stable_branch.py diff --git a/tools/delete_stable_branch.py b/tools/delete_stable_branch.py new file mode 100755 index 0000000000..edb795415d --- /dev/null +++ b/tools/delete_stable_branch.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# Copyright 2021 Ericsson Software Technology +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# This script helps deleting stable branches. Requires 'Delete reference' +# access category for 'refs/for/stable/*' for the user who executes it. + +import argparse +import getpass + +import requests + + +GERRIT_URL = 'https://review.opendev.org/' + + +def delete_branch(username, project_name, branch_id): + print(f'!!! WARNING: You are about to delete stable/{branch_id} ' + f'from {project_name} !!!') + gerrit_auth = requests.auth.HTTPBasicAuth( + username, + getpass.getpass('Gerrit password: ')) + url = (f'{GERRIT_URL}a/projects/{project_name.replace("/", "%2F")}/' + f'branches/stable%2F{branch_id}') + response = requests.delete(url, auth=gerrit_auth) + if response.status_code == 204: + print(f'Branch stable/{branch_id} successfully deleted ' + f'from {project_name}!') + elif response.status_code == 401: + print('401 Unauthorized.') + 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}') + + +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') + args = parser.parse_args() + delete_branch( + args.username, + args.project, + args.branch.replace('stable/', '')) + + +if __name__ == '__main__': + main() diff --git a/tools/list_eol_stale_branches.sh b/tools/list_eol_stale_branches.sh index 051a25da4d..2e0390bff1 100755 --- a/tools/list_eol_stale_branches.sh +++ b/tools/list_eol_stale_branches.sh @@ -20,7 +20,8 @@ function help { cat <] -Provide a list of repositories that contains eol stale branches +Provide a list of repositories that contains eol stale branches, and +give option to delete them. Arguments: -d, --debug Turn on the debug mode @@ -46,6 +47,7 @@ for i in "$@"; do done +gerrit_username=${GERRIT_USER:-} GERRIT_URL="https://review.opendev.org" TOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" BASEDIR=$(dirname $TOOLSDIR) @@ -68,8 +70,16 @@ function is_eol { req="${GERRIT_URL}/changes/?q=status:open+project:${repo}+branch:stable/${em_serie}" patches=$(curl -s ${req} | sed 1d | jq --raw-output '.[] | .change_id') if [ ! -z "${patches}" ]; then - echo "Patches remain opened on stale branch:" + echo "Patches remained open on stale branch (make sure to abandon them):" echo "https://review.opendev.org/q/status:open+project:${repo}+branch:stable/${em_serie}" + else + read -p "> Do you want to delete the branch stable/${em_serie} 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_serie} + fi fi fi }