使用python脚本一次性克隆 gitlab 指定分组下的所有项目(包括自分组中的所有项目),且保持文件结构
1. 生成私人访问令牌
- 进入gitlab
- 点击头像
- 点击setting
- 点击左侧 access tokens
- 创建访问令牌(注意:访问令牌只在创建的时候显示,需要自己保存,系统不会帮我们记住生成的访问令牌)
2. 下载并安装python
-
下载python
python官网下载 -
安装(记住保存位置,我是保存在【D:\software\python37】)
-
配置环境变量
3. 执行 python 脚本
- 进入目标文件夹中
- 打开powershell窗口(shift+鼠标右键)
- 输入命令:python
- 输入下列脚本
- 注意:脚本中的 private_token 和 search 的值要替换
- private_token 的值为自己账号的访问令牌
- search 的值为目标分组(想要获取哪个分组下的所有项目就写哪个分组的名称,分组名称在 自己 gitlab 账号中找)
from urllib.request import urlopen
import json
import shlex
import subprocess
import os
import requests
def getProjects(group_id=None,full_path=None):
url = f"http://8.129.23.111:5720/api/v4/groups/{group_id}/projects?private_token=123456&per_page=100000"
allProjectsDict = requests.get(url=url).json()
for thisProject in allProjectsDict:
try:
thisProjectURL = thisProject['http_url_to_repo']
target_path = full_path + '//' + thisProject['path']
print(('git clone %s %s' % (thisProjectURL,target_path)))
command = shlex.split('git clone %s %s' % (thisProjectURL,target_path))
resultCode = subprocess.Popen(command)
resultCode.wait() # 等待子进程执行完毕
except Exception as e:
print("Error on {}: {}".format(thisProjectURL, str(e)))
def get_mark(group_dict=None):
for i in group_dict:
group_id = i['id']
full_path = i['full_path']
full_path = './' + full_path
if not os.path.exists(full_path):
os.makedirs(full_path)
print("文件夹创建成功!")
else:
print("文件夹已存在!")
getProjects(group_id=group_id,full_path=full_path)
subgroup_url = f"http://8.129.23.111:5720/api/v4/groups/{group_id}/subgroups?private_token=123456&per_page=100000"
subgroup_info = urlopen(subgroup_url)
subgroups = json.loads(subgroup_info.read().decode())
if subgroups:
get_mark(group_dict=subgroups)
else:
pass
# 获取最外层分组的分组id
url = "http://8.129.23.111:5720/api/v4/groups?private_token=123456&search=target-group"
group_url = url
group_info = urlopen(group_url)
group_dict = json.loads(group_info.read().decode())
get_mark(group_dict=group_dict)