【无标题】

python 实现gitlab代码批量下载

环境准备:

1、电脑上安装git环境

2、根据实际情况修改下面三个参数

# 仓库前缀
GIT_PROTOCOL = "http://"
# 仓库地址
GIT_HOST = "git.xxx.com.cn"
# 仓库Token、
GIT_ACCESS_TOKEN = "Uxxxx"
# 认证信息

3、per_page=200  需要根据实际修改,默认20条

import requests
import json
import subprocess

# 仓库前缀
GIT_PROTOCOL = "http://"
# 仓库地址
GIT_HOST = "git.xxx.com.cn"
# 仓库Token、
GIT_ACCESS_TOKEN = "Uxxxx"
# 认证信息
GIT_AUTH = f"oauth2:{GIT_ACCESS_TOKEN}"

# 获取的json文件名称
JSON_FILE = "data.json"

# 获取所有的仓库
headers = {
    "PRIVATE-TOKEN": GIT_ACCESS_TOKEN,
    "Content-Type": "application/json"
}
url = f"{GIT_PROTOCOL}{GIT_HOST}/api/v4/projects?per_page=200"
response = requests.get(url, headers=headers)

# 检查响应状态码
if response.status_code == 200:
    with open(JSON_FILE, 'w') as f:
        json.dump(response.json(), f)
    
    # json解析
    with open(JSON_FILE, 'r') as f:
        data = json.load(f)
        for obj in data:
            # 拼接 clone 地址信息
            clone_url = f"{GIT_PROTOCOL}{GIT_AUTH}@{GIT_HOST}/{obj['path_with_namespace']}"
            # 打印信息
            print(clone_url)
            # git clone
            subprocess.run(['git', 'clone', clone_url])
else:
    print(f"Failed to fetch projects, status code: {response.status_code}")

print("handle end")

上一篇:B4X编程语言:B4X文件操作(File对象的使用)


下一篇:XXE-labs靶场通关攻略