python3拉取gitlab上所有项目

1、安装git、设置git 临时缓存账号密码

git config --global credential.helper 'cache --timeout=3600'
2、使用git 拉取一个项目并输入用户名密码用于缓存

git clone http://173.18.34.2:8929/syjzh/ywq/b06-tyjc/tyjc-server.git

##输入用户名和密码、之后修改运行下面脚本

import os
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import subprocess

# 忽略不安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# GitLab访问令牌
access_token = 'glpat-s8U2SnxfCwxs3EmipYFz'

# GitLab实例URL
gitlab_url = 'https://173.18.34.2:8929'

# API请求头
headers = {
    'Private-Token': access_token
}

# 分页参数
per_page = 100
page = 1

# 存储所有项目
all_projects = []

while True:
    # 获取当前页面的项目
    response = requests.get(
        f'{gitlab_url}/api/v4/projects',
        headers=headers,
        params={'per_page': per_page, 'page': page},
        verify=False
    )
    
    # 检查响应状态码
    if response.status_code != 200:
        print(f'Error: Received status code {response.status_code}')
        break
    
    # 将当前页面的项目添加到all_projects列表中
    projects = response.json()
    if not projects:
        break
    
    all_projects.extend(projects)
    
    # 检查是否有下一页
    next_page = response.headers.get('X-Next-Page')
    if not next_page:
        break
    
    # 更新分页参数
    page = int(next_page)

# 输出获取到的项目列表
print(f"Total projects found: {len(all_projects)}")

for project in all_projects:
    project_id = project['id']
    project_name = project['name']
    project_path_with_namespace = project['path_with_namespace']
    project_http_url = project['http_url_to_repo']  # 使用HTTP URL
    
    # 将路径中的斜杠替换为破折号
    safe_path = project_path_with_namespace.replace('/', '-')
    
    # 创建目录,加上项目ID
    directory_path = f"{project_id}_{safe_path}"
    os.makedirs(directory_path, exist_ok=True)
    
    # 克隆项目到目录中
    print(f'Cloning {project_name} into {directory_path}...')
    try:
        subprocess.check_call(['git', 'clone', project_http_url, directory_path])
    except subprocess.CalledProcessError as e:
        print(f'Failed to clone {project_name}: {e}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值