我想克隆我的 GitLab 环境中的所有项目。有许多组和子组有自己的项目。我想克隆项目,同时在本地维护组的结构。例如:
Group1
Subgroup1
Project1
Group2
Project1
Group3
Subgroup1
Project1
Subgroup2
Project1
Project2
因此,如果我在cd Group3/Subgroup2/Project2
本地运行,我将在相应的项目代码中。
我的代码做了一些工作,但它在子组上中断:
import os
from git import Repo
GITLAB_TOKEN = os.getenv('GITLAB_TOKEN')
gl = gitlab.Gitlab(url='https://gitlab.example.priv', private_token=GITLAB_TOKEN)
all_groups = gl.groups.list(all=True,top_level_only=True)
count=0
for group in all_groups:
os.chdir('/Users/ken/repos')
print("---------------- {} ----------------".format(group.full_path))
if not os.path.exists(group.full_path):
print("\tCreating directory for group " + group.full_path)
os.makedirs(group.full_path)
os.chdir(group.full_path)
print("Cloning projects for group " + group.full_path)
for project in group.projects.list(all=True):
if not os.path.exists(project.path):
Repo.clone_from(project.ssh_url_to_repo, project.path)
print("Creating directories for subgroups")
subgroups = group.subgroups.list()
for subgroup in subgroups:
if not os.path.exists(subgroup.full_path):
os.makedirs(subgroup.full_path)
os.chdir(subgroup.full_path)
print("Cloning projects for subgroup " + subgroup.full_path)
group = gl.groups.get(subgroup.id, lazy=True)
for project in group.projects.list(all=True):
Repo.clone_from(project.ssh_url_to_repo, project.path)
os.chdir('..')
如何迭代组和子组以克隆所有项目,同时在本地维护文件夹结构?