简体   繁体   English

如何通过 python 脚本从现有分支克隆 github 回购分支

[英]How to clone github repo branch from existing branch by python script

My organization is migrating from GitLab to GitHub and we were using some existing Python scripts to check commit difference and to create multiple release branches in one go by cloning the previous release branch. My organization is migrating from GitLab to GitHub and we were using some existing Python scripts to check commit difference and to create multiple release branches in one go by cloning the previous release branch. I know how we do this for GitLab but not able to find some solution to the same in GitHub.我知道我们如何为 GitLab 执行此操作,但无法在 GitHub 中找到相同的解决方案。 If someone can please help me with how can I do the same in GitHub too it will be really helpful.如果有人可以帮助我如何在 GitHub 中做同样的事情,那将非常有帮助。 We are using below code in gitlab for now我们现在在 gitlab 中使用以下代码

def createBranch(projectName, existingBranch, newBranchName):
    projectId=projMap[projectName]
    gl = gitlab.Gitlab('github URL', private_token='git token', ssl_verify=False)
    project = gl.projects.get(projectId)
     
    try:
        project.branches.get(newBranchName)
        log.info("    %s %s already exist", projectName, newBranchName)
        return 0
    except:   
        log.info("createBranch %s from:%s to:%s", project.name, existingBranch, newBranchName)       
        try:        
            project.branches.create({"branch": newBranchName,
                                 "ref": existingBranch})
        except:
            raise Exception(project.name, " error creating " + newBranchName + " from " + existingBranch) 

projMap -> this is one text file which stores all project name and their projectIDs. projMap -> 这是一个存储所有项目名称及其项目 ID 的文本文件。

I tried multiple threads in Stackoverflow but none one seems to help me.我在 Stackoverflow 中尝试了多个线程,但似乎没有一个能帮助我。

PyGithub has the functionality you're looking for. PyGithub具有您正在寻找的功能。

  • Connecting to the GitHub API连接到 GitHub API
gh = Github(params)
  • Getting a repo:获得回购:
repo = gh.get_repo(id)
  • Getting a branch:获取分支:
branch = repo.get_branch(branch)
  • Making a branch:建立一个分支:
repo.create_git_ref(ref='refs/heads/' + 'new_branch_name', sha=branch.commit.sha)

All a copy paste from the PyGithub reference page .所有来自PyGithub 参考页面的复制粘贴。

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

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