Last active
May 30, 2017 16:54
-
-
Save mnkhouri/25507a639127c93b43975519789f6e11 to your computer and use it in GitHub Desktop.
List open PRs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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