繁体   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. 我知道我们如何为 GitLab 执行此操作,但无法在 GitHub 中找到相同的解决方案。 如果有人可以帮助我如何在 GitHub 中做同样的事情,那将非常有帮助。 我们现在在 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 -> 这是一个存储所有项目名称及其项目 ID 的文本文件。

我在 Stackoverflow 中尝试了多个线程,但似乎没有一个能帮助我。

PyGithub具有您正在寻找的功能。

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

所有来自PyGithub 参考页面的复制粘贴。

暂无
暂无

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

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