github3.py icon indicating copy to clipboard operation
github3.py copied to clipboard

Code example for using pull_request object

Open jberkus opened this issue 9 years ago • 4 comments

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])

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/33365858-code-example-for-using-pull_request-object?utm_campaign=plugin&utm_content=tracker%2F183477&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F183477&utm_medium=issues&utm_source=github).

jberkus avatar Apr 26 '16 23:04 jberkus

@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

gkrizek avatar Apr 21 '17 17:04 gkrizek

From that issue, you seem to have solved it?

jberkus avatar Apr 21 '17 19:04 jberkus

Yes, I did. Version problems. Thank you

gkrizek avatar Apr 21 '17 19:04 gkrizek

@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.

gabor-boros avatar Aug 11 '20 08:08 gabor-boros