Home > Net >  Is there automated way to recognize and remove gitlab repo branches?
Is there automated way to recognize and remove gitlab repo branches?

Time:08-19

I want to remove branches from projects that are more than year old. There are just too many to go through manually. I was wondering if there is an automated way to do this.

CodePudding user response:

You can use the API. You can list your projects (or list a groups projects) and use the results from that call to invoke the list branches API with each project ID to list all the branches for each project and retrieve the commit.committed_date field to get the date of the commit at the HEAD of that branch then use the delete branch API to delete branches older than a particular date.

In Python using the python-gitlab API wrapper:

import gitlab
import datetime
from dateutil.parser import parse
gl = gitlab.Gitlab('https://gitlab.example.com', private_token='Your API token')

now = datetime.datetime.now().astimezone(datetime.timezone.utc)
THRESHOLD = now - datetime.timedelta(days=365)

for project in gl.projects.list(all=True):
    for branch in project.branches.list(all=True):
        commit = branch.commit
        date = parse(commit['committed_date'])
        if date < THRESHOLD:
            branch.delete()
  • Related