簡體   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