0

我想克隆我的 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('..')

如何迭代组和子组以克隆所有项目,同时在本地维护文件夹结构?

4

1 回答 1

0

我最终直接调用这些项目并使用它们各自的命名空间来创建 pathToFolder。因此,与其 cd'ing 进出,不如创建绝对路径。

import os
import gitlab
from git import Repo

GITLAB_TOKEN = os.getenv('GITLAB_TOKEN')

gl = gitlab.Gitlab(url='https://gitlab.example.priv', private_token=GITLAB_TOKEN)

gitBasePathRelative = "repos/"
gitBasePathRelativeAbsolut = os.path.expanduser("~/" + gitBasePathRelative)
os.makedirs(gitBasePathRelativeAbsolut,exist_ok=True)

for p in gl.projects.list(all=True):
    print("Cloning project " + p.name)
    pathToFolder = p.namespace['name'] + "/" + p.name
    if not os.path.exists(pathToFolder):
        print("\tCreating directory for project " + pathToFolder)
        os.makedirs(pathToFolder)
        Repo.clone_from(p.ssh_url_to_repo, pathToFolder)
于 2022-01-08T12:39:05.977 回答