From 46d67e2e0bb0721f7ca4f1576017a58fcba0dfaf Mon Sep 17 00:00:00 2001 From: Ildar Valiullin Date: Sun, 27 Feb 2022 03:40:30 +0300 Subject: [PATCH 1/3] fixing usage for different cases Signed-off-by: Ildar Valiullin --- github_backup/github_backup.py | 45 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/github_backup/github_backup.py b/github_backup/github_backup.py index 03f5a65..ff6a377 100755 --- a/github_backup/github_backup.py +++ b/github_backup/github_backup.py @@ -71,14 +71,13 @@ def main(): args.backupdir = args.backupdir.rstrip("/") # Make the connection to Github here. - config = {} + config = {'login_or_token': args.login_or_token} if args.password == False: # no password option given, continue unauthenticated # unauthenticated users can only use http git method args.type = 'http' elif args.password == None: # password option given, but no password value given - config = {'login_or_token': args.login_or_token} if os.path.isfile(CONFFILE): cfg = ConfigParser() cfg.read(CONFFILE) @@ -91,7 +90,6 @@ def main(): if password: config['password'] = password else: - config = {'login_or_token': args.login_or_token} config['password'] = args.password LOGGER.debug("Github config: %r", config) @@ -101,28 +99,33 @@ def main(): if not os.path.exists(args.backupdir): mkdir_p(args.backupdir) - if args.organization: - if args.password: - account = gh.get_organization(args.organization) - else: - account = gh.get_organization(args.login_or_token) - else: - if args.username: - account = gh.get_user(args.username) - elif config.get('password', None): - account = gh.get_user() - else: - account = gh.get_user(args.login_or_token) - - IS_AUTHORIZED = isinstance(account, github.AuthenticatedUser.AuthenticatedUser) - assert not (bool(config.get('password', None)) ^ IS_AUTHORIZED), account + while True: + try: + if args.organization: + account = gh.get_organization(args.organization) + else: + if args.username: + account = gh.get_user(args.username) + elif config.get('password', None): + account = gh.get_user() + else: + account = gh.get_user(args.login_or_token) + + break + except github.BadCredentialsException: + LOGGER.info("Given login_or_token is not authenticated, ignoring...") + # Reset gh client, try without auth + gh = github.Github() + + IS_AUTHORIZED = isinstance(account, (github.AuthenticatedUser.AuthenticatedUser, github.Organization.Organization)) + assert (bool(config.get('password', None)) or IS_AUTHORIZED), account if args.include_keys and not IS_AUTHORIZED: LOGGER.info("Cannot backup keys with unauthenticated account, ignoring...") args.include_keys = False filters = {} - if IS_AUTHORIZED: + if isinstance(account, github.AuthenticatedUser.AuthenticatedUser): # Get all repos filters = { 'affiliation': ','.join(args.affiliation), @@ -329,12 +332,12 @@ def __init__(self, repo, args): if self.is_gist: url = repo.git_pull_url - elif args.type == 'http' or not IS_AUTHORIZED: - url = repo.clone_url elif args.type == 'ssh': url = repo.ssh_url elif args.type == 'git': url = repo.git_url + elif args.type == 'http' or not IS_AUTHORIZED: + url = repo.clone_url self.url = url self.wiki_url = None From 88eb9fa52c947aff045ca70579c380281b1d4107 Mon Sep 17 00:00:00 2001 From: Ildar Valiullin Date: Sun, 27 Feb 2022 04:24:50 +0300 Subject: [PATCH 2/3] fixed "--git" flag definition Signed-off-by: Ildar Valiullin --- github_backup/github_backup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github_backup/github_backup.py b/github_backup/github_backup.py index ff6a377..59fd19f 100755 --- a/github_backup/github_backup.py +++ b/github_backup/github_backup.py @@ -165,7 +165,7 @@ def init_parser(): parser.add_argument("-m", "--mirror", help="Create a bare mirror", action="store_true") parser.add_argument("-f", "--skip-forks", help="Skip forks", action="store_true") parser.add_argument("--skip-repos", help="Skip backing up repositories", action="store_true") - parser.add_argument("-g", "--git", nargs="+", help="Pass extra arguments to git", type=list, default=[], metavar="ARGS") + parser.add_argument("-g", "--git", help="Pass extra arguments to git", action='append', default=[], metavar="ARGS") parser.add_argument("-t", "--type", help="Select the protocol for cloning", choices=['git', 'http', 'ssh'], default='ssh') parser.add_argument("-s", "--suffix", help="Add suffix to repository directory names", default="") parser.add_argument("-u", "--username", help="Backup USER account", metavar="USER") From 696f8c4839e359c1f4b14edcf8e6410772391635 Mon Sep 17 00:00:00 2001 From: Ildar Valiullin Date: Mon, 28 Feb 2022 03:31:46 +0300 Subject: [PATCH 3/3] fixed gh token auth flow Signed-off-by: Ildar Valiullin --- github_backup/github_backup.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/github_backup/github_backup.py b/github_backup/github_backup.py index 59fd19f..a2d99ad 100755 --- a/github_backup/github_backup.py +++ b/github_backup/github_backup.py @@ -10,6 +10,7 @@ import os +import re import errno import codecs import json @@ -337,7 +338,15 @@ def __init__(self, repo, args): elif args.type == 'git': url = repo.git_url elif args.type == 'http' or not IS_AUTHORIZED: - url = repo.clone_url + if re.match('^ghp_.+$', args.login_or_token): + url = re.sub('^https://', f'https://{args.login_or_token}:x-auth-basic@', repo.clone_url) + elif args.password: + if args.username: + url = re.sub('^https://', f'https://{args.username}:{args.password}@', repo.clone_url) + else: + url = re.sub('^https://', f'https://{args.login_or_token}:{args.password}@', repo.clone_url) + else: + url = repo.clone_url self.url = url self.wiki_url = None