From 01d6973b997cb2879e8af7e28bd1eab1482d26c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonas=20H=C3=B6ppner?= <jonas.hoeppner@garz-fricke.com>
Date: Fri, 24 May 2024 06:34:30 +0000
Subject: [PATCH] Scripts: Add script to delete all integration/ branches

It happens that the integration branches are left over from provious
runs of the pipeline, when MRs are closed without merging, for example.

Also a bug in the gitlab-ci makes the pipeline fail, when an integration
branch should be reused. The new scripts just deletes all integration
branch in the given project tree without further checks.

Needs to be run manually.
---
 scripts/delete_integration_branches.py | 97 ++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100755 scripts/delete_integration_branches.py

diff --git a/scripts/delete_integration_branches.py b/scripts/delete_integration_branches.py
new file mode 100755
index 00000000..7364fb18
--- /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:])
-- 
GitLab