简体   繁体   English

如何使用 GitPython 从 GitLab 组中读取项目列表?

[英]How can I read the projects list from a GitLab group with GitPython?

I am trying to find a way to clone all the git repositories from a remote GitLab group with Python ( gitPython for example).我正在尝试找到一种方法来克隆远程 GitLab 组和 Python (例如gitPython )中的所有 git 存储库。

Can you help me, please?你能帮我吗?

You should install python-gitlab (to iterate over the GitLab project structures) and GitPython (to clone the Git repos) packages first.您应该首先安装python-gitlab (以迭代 GitLab 项目结构)和GitPython (以克隆 Git 存储库)包。

If you have a self-hosted GitLab instance, then you also need to get a personal token.如果您有自托管的 GitLab 实例,那么您还需要获取个人令牌。 Go to https://gitlab.your-company.com/-/profile/personal_access_tokens to access the token interface, like on the screenshot: Go 到https://gitlab.your-company.com/-/profile/personal_access_tokens访问令牌界面,如屏幕截图所示:

个人令牌

Save the obtained token (step 1).保存获得的令牌(步骤 1)。

You also need a deploy token for each repository that you are going to clone.对于要克隆的每个存储库,您还需要一个部署令牌。 To do so, go to https://gitlab.your-company.com/your_group/your_project/-/settings/repository -> "Deploy tokens", like on the screenshot below:为此,go 到https://gitlab.your-company.com/your_group/your_project/-/settings/repository ->“部署令牌”,如下面的屏幕截图所示:

部署令牌示例

You'll get a token username (like gitlab+deploy-token-1488 ) and the token itself.您将获得令牌用户名(如gitlab+deploy-token-1488 )和令牌本身。 Save this username/token pair (step 2) and repeat this step for each repo.保存此用户名/令牌对(步骤 2)并为每个 repo 重复此步骤。

As the last step, you should get the ID of the group which contains your repos.作为最后一步,您应该获取包含您的存储库的组的 ID。 It should be displayed on the web interface of your group at https://gitlab.your-company.com/your_group like on the screenshot:它应该显示在您组的 web 界面上https://gitlab.your-company.com/your_group上,如屏幕截图所示:

组 ID 示例

Save this ID too (step 3).也保存此 ID(步骤 3)。

Now you are all set to run this code snippet:现在您已准备好运行此代码段:

import git
import os
import gitlab

local_cloned_repos_dir = 'local_cloned_repos_dir'

gl = gitlab.Gitlab('https://gitlab.your-company.com', private_token='insert_token_from_step_1')

deploy_token_map = {
    'your_project_name': 'gitlab+deploy-token-1488:insert_token_from_step_2'
}

for project in gl.groups.get('insert_id_from_step_3').projects.list():
    local_cloned_project_dir = os.path.join(local_cloned_repos_dir, project.name)
    os.makedirs(local_cloned_project_dir, exist_ok=True)
    git.Repo.clone_from('https://{}@gitlab.your-company.com/{}'.format(deploy_token_map[project.name], project.path_with_namespace), local_cloned_project_dir)

Note that I've created a map to match the name of the project with the corresponding token username / deploy token pair, but you don't have to handle it exactly this way.请注意,我创建了一个 map 以将项目名称与相应的令牌用户名/部署令牌对匹配,但您不必完全以这种方式处理它。

Hope it helps!希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除 gitlab 中的组? - How can I delete group in gitlab? 如何在 Python-Gitlab 中列出项目中的所有用户及其权限? - How to list all users and their permissions from projects in Python-Gitlab? 如何为gitlab组中的所有项目设置一个gitlab代理,将项目单独部署到kuberenetes集群 - How to setup one gitlab agent for all projects in gitlab group to deploy projects separately to the kuberenetes cluster 将私有组导入到新的 GitLab 实例时,如何找到 GitLab 源 URL ? - How can I find the GitLab source URL for a private group when importing it to a new GitLab instance? 作为对整个实例没有管理员权限的用户,如何为 GitLab 上的所有新项目设置默认分支名称? - How can I set the default branch name for all new projects on GitLab, as an user without admin right over entire instance? 如何通过 API 在 gitlab 中获取名为“gpt”的组下的所有项目 - How to get all projects under a group called "gpt" in gitlab through API 如何备份 gitlab 组? - How to make a backup of a gitlab group? 我如何在 lambdafunction aws 中列出所有登录组 - How i can list all log in a group in lambdafunction aws 我如何允许 Firebase RTB schema.read 中的 Org Object 的“所有者”访问属于该组的所有“用户”? - How can I allow the "owners" of an Org Object in a Firebase RTB schema .read access to all "Users" that are part of the group? 如何优化从 S3 读取? - How can I optimize the read from S3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM