我正在尝试在使用 pyGithub ( https://github.com/jacquev6/PyGithub ) API 创建的新分支上提交拉取请求。每次运行脚本时,我都会得到不同的结果。有时我能够在循环结束时创建拉取请求,有时它会出错,说我的主分支和我创建的新分支之间的提交没有区别。我确定这是由于“git push origin new_branch”命令没有及时完成,因为它在子进程中被调用。在继续之前如何等待 git 调用完成?我尝试在我的进程上使用 .wait() ,但我认为它会等待“子进程”完成,而不是等待 git 调用,这是一个单独的命令。任何想法如何做到这一点?或者也许我' 我以错误的方式解决这个问题。我的代码:
from github import Github
import getpass
import subprocess
username = input("Please enter your GitHub username: ")
password = getpass.getpass()
g = Github(username, password)
base_branch = input("Please enter your base branch, default --> [master]: ")
base_branch = base_branch or 'master'
new_branch = input("Please enter your new branch name: ")
authorName = input("Please enter the github organization name: ")
# author validation
try:
author = g.get_organization(authorName)
except GithubException as ghe:
print(ghe)
repos = input("Please enter a comma separated list of repo names: ").split(',')
# projects validation
if repos[0] == '':
repos = input("No repos entered. \n Please enter a comma separated list of repos names: ").split(',')
for repo in repos:
# retrieve info on the base_branch
repoObj = author.get_repo(repo)
baseBranch = repoObj.get_branch(base_branch)
# create the new branch
repoObj.create_git_ref('refs/heads/'+new_branch, baseBranch.commit.sha)
# touch readme.md in the new branch and commit the change
subprocess.call(['git', 'checkout', '-B', new_branch, 'origin/' + base_branch], cwd='./' + repo)
subprocess.call("echo '' >> README.md", shell=True, cwd='./' + repo)
subprocess.call(['git', 'commit', '-am "First Commit:"' + new_beanch + '"'], cwd='./' + repo)
pushProcess = subprocess.call(['git', 'push', 'origin', new_branch], cwd='./' + repo)
pushProcess.wait()
newBranch = repoObj.get_branch(new_branch)
# create pull request
repoObj.create_pull(release_title, 'Main Release Ticket: ',baseBranch.commit.sha,newBranch.commit.sha)