[英]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上,如屏幕截图所示:
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.