|
#!/usr/bin/python |
|
import os |
|
import argparse |
|
import getpass |
|
import configparser |
|
import github3 |
|
|
|
def mfa(): |
|
"""Get the MFA code |
|
""" |
|
try: |
|
prompt = raw_input |
|
except NameError: |
|
prompt = input |
|
|
|
code = '' |
|
while not code: |
|
code = prompt('Enter 2FA code : ') |
|
|
|
return code |
|
|
|
def setup_username(user, configfilename): |
|
"""Set up the username configuration file |
|
Use the configparser to structure the file |
|
""" |
|
config = configparser.ConfigParser() |
|
config['DEFAULT']['username'] = user |
|
with open(configfilename, 'w') as configfile: |
|
config.write(configfile) |
|
|
|
return user |
|
|
|
def load_username(configfilename): |
|
"""Load the username. |
|
If the configuration file exists |
|
""" |
|
|
|
config = configparser.ConfigParser() |
|
config.read(configfilename) |
|
|
|
return config['DEFAULT']['username'] |
|
|
|
if __name__ == '__main__': |
|
|
|
# Parse the arguments |
|
parser = argparse.ArgumentParser(description='GitHub Release Information') |
|
|
|
parser.add_argument('--repo', help='GitHub Repository', required=True) |
|
parser.add_argument('--tag', help='GitHub Repo Release Tag', required=True) |
|
parser.add_argument('--user', help='GitHub username') |
|
|
|
args = parser.parse_args() |
|
|
|
# Configuration filename |
|
configfilename = "%s/.view-github-release" % os.path.expanduser('~') |
|
|
|
# If the username is set in the cli, set up the config file |
|
# If the username is NOT set, load it from the config file, IF it exists |
|
username = None |
|
if args.user: |
|
username = setup_username(args.user, configfilename) |
|
elif os.path.isfile(configfilename): |
|
username = load_username(configfilename) |
|
|
|
# If our username is set, get the password and login |
|
if username: |
|
password = getpass.getpass("GitHub Password : ") |
|
gh = github3.login(username, password, two_factor_callback=mfa) |
|
# Otherwise, exit and tell the username |
|
else: |
|
print("Please set a username!") |
|
parser.print_help() |
|
exit() |
|
|
|
# Split out the repository path into owner and name |
|
try: |
|
repo_owner,repo_name = args.repo.split("/") |
|
except ValueError: |
|
print("Repository param is not well formed!") |
|
parser.print_help() |
|
exit() |
|
|
|
# Get the Release |
|
release = gh.repository(repo_owner, repo_name).release_from_tag(args.tag) |
|
|
|
if not release.tag_name: |
|
print("No Release loaded; Check Repo path and tag name!") |
|
parser.print_help() |
|
exit() |
|
|
|
# Output the release info |
|
|
|
print("Release %s" % args.tag) |
|
print("URL | %s" % release.html_url) |
|
print("Publish Date | %s" % release.published_at) |
|
print("Author | %s" % release.author['login']) |
|
print("Tag Name | %s" % release.tag_name) |
|
print("Name | %s" % release.name) |
|
print("#################") |
|
print("Body") |
|
print("#################") |
|
print(release.body) |