简体   繁体   English

是否有自动识别和删除 gitlab 回购分支的方法?

[英]Is there automated way to recognize and remove gitlab repo branches?

I want to remove branches from projects that are more than year old.我想从超过一年的项目中删除分支。 There are just too many to go through manually. go 手动操作太多了。 I was wondering if there is an automated way to do this.我想知道是否有一种自动化的方法可以做到这一点。

You can use the API.您可以使用 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.您可以 列出您的项目(或列出一组项目)并使用该调用的结果来调用列表分支 API以及每个项目 ID 以列出每个项目的所有分支并检索commit.committed_date字段以获取日期在该分支的 HEAD 提交,然后使用删除分支 API删除早于特定日期的分支。

In Python using the python-gitlab API wrapper:在 Python 中使用python-gitlab API 包装器:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM