Skip to content

Commit b5972aa

Browse files
committed
Correctly download repos when user arg != authenticated user
1 parent 0de341e commit b5972aa

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

bin/github-backup

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,33 @@ def _request_url_error(template, retry_timeout):
503503
return False
504504

505505

506+
def get_authenticated_user(args):
507+
template = 'https://{0}/user'.format(get_github_api_host(args))
508+
data = retrieve_data(args, template, single_request=True)
509+
return data[0]
510+
511+
506512
def check_git_lfs_install():
507513
exit_code = subprocess.call(['git', 'lfs', 'version'])
508514
if exit_code != 0:
509515
log_error('The argument --lfs requires you to have Git LFS installed.\nYou can get it from https://git-lfs.github.com.')
510516
sys.exit(1)
511517

512518

513-
def retrieve_repositories(args):
519+
def retrieve_repositories(args, authenticated_user):
514520
log_info('Retrieving repositories')
515521
single_request = False
516-
template = 'https://{0}/user/repos'.format(
517-
get_github_api_host(args))
522+
if args.user == authenticated_user['login']:
523+
# we must use the /user/repos API to be able to access private repos
524+
template = 'https://{0}/user/repos'.format(
525+
get_github_api_host(args))
526+
else:
527+
if args.private:
528+
log_error('Authenticated user is different from user being backed up, thus private repositories cannot be accessed')
529+
template = 'https://{0}/users/{1}/repos'.format(
530+
get_github_api_host(args),
531+
args.user)
532+
518533
if args.organization:
519534
template = 'https://{0}/orgs/{1}/repos'.format(
520535
get_github_api_host(args),
@@ -981,7 +996,8 @@ def main():
981996

982997
log_info('Backing up user {0} to {1}'.format(args.user, output_directory))
983998

984-
repositories = retrieve_repositories(args)
999+
authenticated_user = get_authenticated_user(args)
1000+
repositories = retrieve_repositories(args, authenticated_user)
9851001
repositories = filter_repositories(args, repositories)
9861002
backup_repositories(args, output_directory, repositories)
9871003
backup_account(args, output_directory)

0 commit comments

Comments
 (0)