使用Gitlab V4 API 批量Clone代码
2023-03-06 本文已影响0人
赵客缦胡缨v吴钩霜雪明

最近需要从Gitlab上拉取多份代码,一个个操作有点麻烦,就使用使用Gitlab提供的API来批量Clone代码。

gitlab有提供api来获取git仓库的信息,利用这些信息clone下项目
http://your-gitlab-server//api/v4/projects
--每页显示150个项目
http://your-gitlab-server//api/v4/projects?per_page=150
参见文档:https://docs.gitlab.com/ee/api/projects.html#list-all-projects
操作步骤
1.获取Personal Access Token(注意不是Feed token)

2.配置环境
配置python环境,我这里安装的是python3.1

3.执行脚本
以下代码保存为batchClone.py
打开命令提示框, 执行 py batchClone.py

from urllib.request import urlopen
import json
import os
def clone_ssh(thisProject):
# ssh 下载
thisProjectURL = thisProject['ssh_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
print('download: ', thisProjectURL, ' ', thisProjectPath)
if os.path.exists(thisProjectPath):
command = 'git -C "%s" pull' % (thisProjectPath)
else:
command = 'git clone %s %s' % (thisProjectURL, thisProjectPath)
os.system(command)
def clone_http(thisProject):
thisProjectURL = thisProject['http_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
print('download: ', thisProjectURL, ' ', thisProjectPath)
if os.path.exists(thisProjectPath):
command = 'git clone %s' % (thisProjectPath)
else:
command = 'git clone %s %s' % (thisProjectURL, thisProjectPath)
os.system(command)
def download(gitlabAddr, gitlabToken, ssh_or_http):
for index in range(10):
url = "%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % (
gitlabAddr, gitlabToken, index)
print(url)
allProjects = urlopen(url)
allProjectsDict = json.loads(allProjects.read().decode())
print(allProjectsDict)
print("###################")
print("###################")
if len(allProjectsDict) == 0:
print("###################")
print("nothing")
print("###################")
break
for thisProject in allProjectsDict:
path = thisProject['http_url_to_repo'] + ' ' + thisProject['path_with_namespace']
print(path)
print("download all")
filter_str = input()
filter = []
if len(filter_str.lstrip()) != 0:
filter = filter_str.split(',')
for thisProject in allProjectsDict:
try:
# http下载
path = thisProject['http_url_to_repo'] + ' ' + thisProject['path_with_namespace']
if len(filter) == 0:
if ssh_or_http == "http":
clone_http(thisProject)
else:
clone_ssh(thisProject)
else:
for f in filter:
if f in path:
if ssh_or_http == "http":
clone_http(thisProject)
else:
clone_ssh(thisProject)
else:
print('skip download: ', path)
# time.sleep(1)
except Exception as e:
print(e)
if __name__ == '__main__':
while True:
try:
print("gitlabAddr:(eg:http://127.0.0.1)")
gitlabAddr = input()
print("gitlabToken:(eg:2yxfaBjWLpugzP7wNyZG")
gitlabToken = input()
download(gitlabAddr, gitlabToken, "http")
except Exception as e:
print(e)
