简体   繁体   English

python脚本从github代码组织克隆所有代码存储库

[英]python script to clone all the code repos from github code organisation

There are many ways to clone the GitHub code repos from an organization.有很多方法可以从组织中克隆 GitHub 代码存储库。 Now considering there are some repos which are private and public, I was looking into a solution where an automated script or code logic would solve my purpose of cloning the code repos irrespective of repo type.现在考虑到有一些私有和公共存储库,我正在研究一种解决方案,其中自动化脚本或代码逻辑将解决我克隆代码存储库的目的,而不管存储库类型如何。 I tried various measures but couldn't land to simple automated solution.我尝试了各种措施,但无法采用简单的自动化解决方案。 I tried using curl command, but it was slow as the density of code repos to be cloned was above 200+ code repos.我尝试使用 curl 命令,但它很慢,因为要克隆的代码存储库的密度超过 200 多个代码存储库。

Can you try below python code snippet to clone all repos:您可以尝试以下 python 代码片段来克隆所有存储库:

Here we use git ssh to clone each repos in a secure way to accommodate both public/private code.在这里,我们使用 git ssh 以安全的方式克隆每个存储库,以容纳公共/私有代码。

Sometimes due to network issues , it may show remote hung/packet-loss messages which leads to repo partial/no cloning.有时由于网络问题,它可能会显示远程挂起/数据包丢失消息,导致 repo 部分/无克隆。 to avoid that please set below parameter in the shell.为避免这种情况,请在 shell 中设置以下参数。

git config --global http.postBuffer 157286400

Assuming that your git global credentials are updated.假设您的 git 全局凭据已更新。

Code :代码 :

import os
# Define your repo list
list = ["repo1","repo2"]

#Loop it through repo's list
for repo in list:
    #Clone the each repo using ssh git clone command in most secure way
    cmd = "git clone git@<git ssh url>/{}".format(repo)
    print("Starting to clone {}".format(repo))
    os.system(cmd)
    print("Finshed cloning {}".format(repo))
    print("#####################################")
    print("")

putted together info from two resources to clone all organizations private repositories:将来自两个资源的信息放在一起以克隆所有组织的私有存储库:

# https://www.thepythoncode.com/article/using-github-api-in-python
# https://github.com/libgit2/pygit2/issues/554

# pip3 install PyGithub pygit2

from github import Github
import pygit2

# using an access token
g = Github('TOKEN')
org = g.get_organization('ORG NAME')

callbacks = pygit2.RemoteCallbacks(pygit2.UserPass('TOKEN', 'x-oauth-basic'))
# Clone repo
for repo in org.get_repos():
    pygit2.clone_repository(
        url=repo.clone_url,
        path=f'/home/<Username>/PycharmProjects/Backup/{repo.name}',
        callbacks=callbacks)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM