Skip to content

Commit d820dd9

Browse files
author
Eric Wheeler
committed
Fix -R flag to allow backups of repositories not owned by user
Previously, using -R flag would show zero issues/PRs for repositories not owned by the primary user due to incorrect pagination parameters being added to single repository API calls. - Remove pagination parameters for single repository requests - Support owner/repo format in -R flag (e.g., -R owner/repo-name) - Skip filtering when specific repository is requested - Fix URL construction for requests without query parameters This enables backing up any repository, not just those owned by the primary user specified in -u flag.
1 parent 1bad563 commit d820dd9

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

github_backup/github_backup.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,15 @@ def retrieve_data_gen(args, template, query_args=None, single_request=False):
578578
page = 0
579579

580580
while True:
581-
page = page + 1
581+
if single_request:
582+
request_page, request_per_page = None, None
583+
else:
584+
page = page + 1
585+
request_page, request_per_page = page, per_page
586+
582587
request = _construct_request(
583-
per_page,
584-
page,
588+
request_per_page,
589+
request_page,
585590
query_args,
586591
template,
587592
auth,
@@ -715,14 +720,22 @@ def _get_response(request, auth, template):
715720
def _construct_request(
716721
per_page, page, query_args, template, auth, as_app=None, fine=False
717722
):
718-
querystring = urlencode(
719-
dict(
720-
list({"per_page": per_page, "page": page}.items())
721-
+ list(query_args.items())
722-
)
723-
)
723+
all_query_args = {}
724+
if per_page:
725+
all_query_args["per_page"] = per_page
726+
if page:
727+
all_query_args["page"] = page
728+
if query_args:
729+
all_query_args.update(query_args)
730+
731+
request_url = template
732+
if all_query_args:
733+
querystring = urlencode(all_query_args)
734+
request_url = template + "?" + querystring
735+
else:
736+
querystring = ""
724737

725-
request = Request(template + "?" + querystring)
738+
request = Request(request_url)
726739
if auth is not None:
727740
if not as_app:
728741
if fine:
@@ -735,7 +748,11 @@ def _construct_request(
735748
request.add_header(
736749
"Accept", "application/vnd.github.machine-man-preview+json"
737750
)
738-
logger.info("Requesting {}?{}".format(template, querystring))
751+
752+
log_url = template
753+
if querystring:
754+
log_url += "?" + querystring
755+
logger.info("Requesting {}".format(log_url))
739756
return request
740757

741758

@@ -885,9 +902,13 @@ def retrieve_repositories(args, authenticated_user):
885902
)
886903

887904
if args.repository:
905+
if "/" in args.repository:
906+
repo_path = args.repository
907+
else:
908+
repo_path = "{0}/{1}".format(args.user, args.repository)
888909
single_request = True
889-
template = "https://{0}/repos/{1}/{2}".format(
890-
get_github_api_host(args), args.user, args.repository
910+
template = "https://{0}/repos/{1}".format(
911+
get_github_api_host(args), repo_path
891912
)
892913

893914
repos = retrieve_data(args, template, single_request=single_request)
@@ -928,6 +949,8 @@ def retrieve_repositories(args, authenticated_user):
928949

929950

930951
def filter_repositories(args, unfiltered_repositories):
952+
if args.repository:
953+
return unfiltered_repositories
931954
logger.info("Filtering repositories")
932955

933956
repositories = []

0 commit comments

Comments
 (0)