diff --git a/scripts/delete_integration_branches.py b/scripts/delete_integration_branches.py new file mode 100755 index 0000000000000000000000000000000000000000..7364fb183c9cc37897874b0d4fbba322379994d3 --- /dev/null +++ b/scripts/delete_integration_branches.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +""" +Deletes all branches starting with 'integrate/' in a given project tree +in gitlab. By default the 'seco-ne' group on git.seco.com is chosen. +No further checks are done before deleting, so if run in parallel with +pending pipelines, things might get messed up a little. +""" + + +import argparse +import logging +import sys + +import gitlab as gl + +import common + +__author__ = "Jonas Höppner" +__email__ = "jonas.hoeppner@seco.com" + +# ID of the Seco Northern Europe +GITLAB_GROUP_ID = "556" + + +def get_projects_from_group(gitlab, base_group): + """Recurse through all subgroups and create a flat list of all projects""" + + p_list = [ + gitlab.projects.get(group_project.id) + for group_project in base_group.projects.list( + iterator=True, retry_transient_errors=True + ) + ] + for subgroup in base_group.subgroups.list( + iterator=True, retry_transient_errors=True + ): + group = gitlab.groups.get(subgroup.id) + p_list += get_projects_from_group(gitlab, group) + return p_list + + +def main(args): + + parser = argparse.ArgumentParser(description=__doc__, usage="%(prog)s [OPTIONS]") + + parser.add_argument( + "--gitlab-url", + help="""URL to the GitLab instance""", + dest="gitlab_url", + action="store", + default=common.GITLAB_URL, + ) + parser.add_argument( + "--token", + help="""GitLab REST API private access token""", + dest="token", + required=True, + ) + parser.add_argument( + "-g", + "--group-id", + action="store", + dest="groupid", + default=GITLAB_GROUP_ID, + help=("Specify the group by id to query projects in."), + ) + parser.add_argument( + "-v", + "--verbose", + action="count", + dest="verbose", + default=0, + help=("Increase verbosity."), + ) + + options = parser.parse_args(args) + if options.verbose: + logging.basicConfig(level=logging.DEBUG) + + logging.debug(options) + + gitlab = gl.Gitlab(options.gitlab_url, private_token=options.token) + group = gitlab.groups.get(options.groupid) + + print(f"Getting projects in group {group.name}") + + projects = get_projects_from_group(gitlab, group) + for project in projects: + print(f"Searching branches in project {project.name}") + for branch in project.branches.list(iterator=True, retry_transient_errors=True): + if branch.name.startswith("integrate/"): + branch.delete() + print(f"Deleted branch {branch.name}") + + +if __name__ == "__main__": + main(sys.argv[1:])