Skip to content
Snippets Groups Projects
Commit 01d6973b authored by Jonas Höppner's avatar Jonas Höppner
Browse files

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.
parent c05acbe1
No related branches found
No related tags found
1 merge request!404Scripts: Add script to delete all integration/ branches
Pipeline #130232 passed with stage
in 18 minutes and 7 seconds
#!/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:])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment