github3.py
github3.py copied to clipboard
Code example for using pull_request object
Hey, I noticed that there were zero code examples for working with pull requests. Through some trial & error I figured out how to use the object, so I thought I'd supply you with a code example for including in the docs.
#!/usr/bin/env python
"""
This script automates creating a new branch, checking it out, and merging in a numbered pull request.
Needs to be run from the root dir of the repo.
"""
import github3
import sys
from subprocess import check_call
if len(sys.argv) < 5:
print "usage: test-pull.py USER REPO #PRNUM"
sys.exit(-1)
else:
username = sys.argv[1]
reponame = sys.argv[2]
prnum = sys.argv[3]
# connect anonymously
gh = github3.GitHub();
rep = gh.repository(username, reponame)
# pull the pull request using the user-supplied number
pr = rep.pull_request(prnum)
prsrc = pr.head
# if it's a local pull request, name is branch ...
if prsrc.repo[0] == username:
prname = prsrc.ref
else:
#otherwise it's user + branch
prname = '{}-{}'.format(prsrc.repo[0],prsrc.ref)
prfrom = 'git://github.com/{}/{}'.format(prsrc.repo[0], prsrc.repo[1], prsrc.ref)
# make sure we're up to date
check_call(["git", "checkout", "master"])
check_call(["git", "pull", "origin", "master"])
# create a branch for the new pr, and pull to it
check_call(["git", "checkout", "-b", prname, "master"])
check_call(["git", "pull", prfrom, prsrc.ref])
@jberkus Do you have a code example for getting all Pull Requests? (not a specific number).
I can't seem to get it to work: #697
From that issue, you seem to have solved it?
Yes, I did. Version problems. Thank you
@omgjlk This issue is still valid, but the PullRequest objects are documented at least: https://github3py.readthedocs.io/en/master/api-reference/pulls.html?highlight=pull%20request
It would be nice to collect the example scenarios to be able to open a PR.