Skip to content

Instantly share code, notes, and snippets.

@mnkhouri
Last active May 30, 2017 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnkhouri/25507a639127c93b43975519789f6e11 to your computer and use it in GitHub Desktop.
Save mnkhouri/25507a639127c93b43975519789f6e11 to your computer and use it in GitHub Desktop.
List open PRs
#!/usr/bin/env python
from getpass import getuser, getpass
from os.path import expanduser
from platform import node
from sys import exit
try:
import github3
except ImportError:
print('Module github3 not installed!')
print('Installation: pip install --pre github3.py')
exit()
REPOS = ['wink-hub-michelangelo', 'wink-hub', 'wink-hub-apron', 'wink-aau', 'wink-hub-mfg-tools', 'wink-hub-http-connector']
CREDENTIALS_FILE = expanduser("~/.marcs_gh_checker")
OAUTH_NAME = 'Marc\'s Open PR checker on ' + node()
OAUTH_URL = 'http://marc.khouri.ca'
OAUTH_SCOPES = ['repo']
try:
prompt = raw_input # Python 2
except NameError:
prompt = input # Python 3
def request_token():
print('Generating a new oauth token with scope "repo".')
user = ''
while not user:
user = prompt('Enter username: ')
password = ''
while not password:
password = getpass('Password for {0}: '.format(user))
def two_fa_prompt():
code = ''
while not code:
code = prompt('Enter 2FA code: ')
return code
try:
auth = github3.authorize(user, password, OAUTH_SCOPES, OAUTH_NAME, OAUTH_URL, two_factor_callback=two_fa_prompt)
except github3.exceptions.UnprocessableEntity:
print('\nToken Creation Failed!')
print('Most likely, you already have an oauth token for this application.')
print('Please log into the GitHub web UI and delete the existing token.')
exit()
return auth
def load_token(credentials_file):
with open(credentials_file, 'r') as fd:
print('Loading oauth token from ' + credentials_file)
token = fd.readline().strip() # Can't hurt to be paranoid
id = int(fd.readline().strip())
if not id or not token:
print('Malformed token, re-requesting from API')
raise FileNotFoundError
return token
def save_token(credentials_file, auth):
with open(credentials_file, 'w+') as fd:
print('Saving oauth token to ' + credentials_file)
fd.write(auth.token + '\n')
fd.write(str(auth.id))
def main():
try:
token = load_token(CREDENTIALS_FILE)
except FileNotFoundError:
auth = request_token()
save_token(CREDENTIALS_FILE, auth)
token = load_token(CREDENTIALS_FILE)
gh = github3.login(token=token)
for reponame in REPOS:
repo = gh.repository('winkapp', reponame)
#import json; print(json.dumps(json.loads(repo.as_json()), indent=4))
prs = repo.pull_requests(state = 'open')
print('{}:'.format(repo.name))
for pr in prs:
#import json; print(json.dumps(json.loads(pr.as_json()), indent=4))
print('\t#{}: {} ({})'.format(pr.number, pr.title, pr.html_url))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment