在提交pull请求之前,等待子进程调用内的git命令完成

时间:2013-09-30 01:57:55

标签: python python-3.x subprocess github-api

我正在尝试在我使用pyGithub(https://github.com/jacquev6/PyGithub)API创建的新分支上提交拉取请求。每次运行脚本时,我都会得到不同的结果。有时我能够在循环结束时创建pull请求,有时它会错误地说我的master分支和我创建的新分支之间的提交没有区别。我确定这是因为“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)

0 个答案:

没有答案