gitlab批量删除分支

一、概述

gitlab的java项目,运行几十年了,分支特别多。开发人员一般不会主动删除分支,长期下来,分支就特别多。

按照开发流程上来说,有新需求和bug,会开一个分支出来。等功能测试完成上线后,就会合并到master分支。

那么这些多余的分支就是没用的,需要删除。

二、批量删除分支

分支有几千个,通过gitlab页面,一个个手动删除,太耗费时间了,可以通过调用api接口来实现。

使用python代码来调用

这里会保留master分支,其他分支会被删除。如果需要保留指定分支,增加branch_list.remove('分支名')

# 注意,每次只能删除20个分支
import requests
from requests.auth import HTTPBasicAuth

# GitLab 实例 URL
gitlab_url = 'https://gitlab.qq.com'

# GitLab 用户名和密码
username = 'xiao'
password = 'abcd@1234'

# GitLab 项目 ID
project_id = '1969'

# 登录获取访问令牌
login_url = f'{gitlab_url}/oauth/token'
login_data = {
    'grant_type': 'password',
    'username': username,
    'password': password
}

response = requests.post(login_url, data=login_data)
if response.status_code == 200:
    access_token = response.json()['access_token']
    print("Successfully logged in and obtained access token.")
else:
    print(f"Failed to log in: {response.status_code}")
    print(response.json())
    exit()

# 获取项目的分支列表
branches_url = f'{gitlab_url}/api/v4/projects/{project_id}/repository/branches'
headers = {
    'Authorization': f'Bearer {access_token}'
}

response = requests.get(branches_url, headers=headers)
branch_list=[]
if response.status_code == 200:
    branches = response.json()
    # print("Remote branches:")
    for branch in branches:
        # print(branch['name'])
        branch_list.append(branch['name'])
else:
    print(f"Failed to retrieve branches: {response.status_code}")
    print(response.json())

try:
# 移除master分支,不做删除 branch_list.remove('master') except Exception as e: print(e) print("branch_list",branch_list) for branch_name in branch_list: # 删除指定分支 delete_branch_url = f'{gitlab_url}/api/v4/projects/{project_id}/repository/branches/{branch_name}' headers = { 'Authorization': f'Bearer {access_token}' } response = requests.delete(delete_branch_url, headers=headers) if response.status_code == 204: print(f"Branch '{branch_name}' deleted successfully.") elif response.status_code == 404: print(f"Branch '{branch_name}' not found.") else: print(f"Failed to delete branch '{branch_name}': {response.status_code}") print(response.json())

注意事项:
修改变量gitlab_url ,username,password,改为实际的值。

project_id,这个是项目id,在gitlab上打开某个项目,就可以看到Project ID

每次执行,只能删除20个分支,因为api接口,一次只能返回20个。

二、批量删除tag

tag有几千个,通过gitlab页面,一个个手动删除,太耗费时间了,可以通过调用api接口来实现。

使用python代码来调用

这里会删除所有tag

'''
python 使用用户名和密码登录gitlab,删除git项目所有tag
'''
import requests

# GitLab 实例 URL
gitlab_url = 'https://gitlab.qq.com'

# GitLab 用户名和密码
username = 'xiao'
password = 'abcd@1234'

# GitLab 项目 ID
project_id = '1969'

# 登录获取访问令牌
login_url = f'{gitlab_url}/oauth/token'
login_data = {
    'grant_type': 'password',
    'username': username,
    'password': password
}

response = requests.post(login_url, data=login_data)
if response.status_code == 200:
    access_token = response.json()['access_token']
    print("Successfully logged in and obtained access token.")
else:
    print(f"Failed to log in: {response.status_code}")
    print(response.json())
    exit()

# 获取项目的所有标签
tags_url = f'{gitlab_url}/api/v4/projects/{project_id}/repository/tags'
headers = {
    'Authorization': f'Bearer {access_token}'
}

response = requests.get(tags_url, headers=headers)
if response.status_code == 200:
    tags = response.json()
    print("Remote tags:")
    for tag in tags:
        print(tag['name'])
else:
    print(f"Failed to retrieve tags: {response.status_code}")
    print(response.json())
    exit()

# 删除项目的所有标签
for tag in tags:
    tag_name = tag['name']
    delete_tag_url = f'{gitlab_url}/api/v4/projects/{project_id}/repository/tags/{tag_name}'
    response = requests.delete(delete_tag_url, headers=headers)
    if response.status_code == 204:
        print(f"Tag '{tag_name}' deleted successfully.")
    elif response.status_code == 404:
        print(f"Tag '{tag_name}' not found.")
    else:
        print(f"Failed to delete tag '{tag_name}': {response.status_code}")
        print(response.json())

注意事项:
修改变量gitlab_url ,username,password,改为实际的值。

project_id,这个是项目id,在gitlab上打开某个项目,就可以看到Project ID

每次执行,只能删除20个tag,因为api接口,一次只能返回20个。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值