Skip to content

Fix -R flag to allow backups of repositories not owned by user #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bin/github-backup
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import logging
import os
import sys


# If we are running from a git-checkout, we can run against the development
# tree without installing.
if os.path.exists(os.path.join(os.path.dirname(__file__), "..", ".git")):
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
)


from github_backup.github_backup import (
backup_account,
backup_repositories,
Expand Down
49 changes: 36 additions & 13 deletions github_backup/github_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,15 @@ def retrieve_data_gen(args, template, query_args=None, single_request=False):
page = 0

while True:
page = page + 1
if single_request:
request_page, request_per_page = None, None
else:
page = page + 1
request_page, request_per_page = page, per_page

request = _construct_request(
per_page,
page,
request_per_page,
request_page,
query_args,
template,
auth,
Expand Down Expand Up @@ -715,14 +720,22 @@ def _get_response(request, auth, template):
def _construct_request(
per_page, page, query_args, template, auth, as_app=None, fine=False
):
querystring = urlencode(
dict(
list({"per_page": per_page, "page": page}.items())
+ list(query_args.items())
)
)
all_query_args = {}
if per_page:
all_query_args["per_page"] = per_page
if page:
all_query_args["page"] = page
if query_args:
all_query_args.update(query_args)

request_url = template
if all_query_args:
querystring = urlencode(all_query_args)
request_url = template + "?" + querystring
else:
querystring = ""

request = Request(template + "?" + querystring)
request = Request(request_url)
if auth is not None:
if not as_app:
if fine:
Expand All @@ -735,7 +748,11 @@ def _construct_request(
request.add_header(
"Accept", "application/vnd.github.machine-man-preview+json"
)
logger.info("Requesting {}?{}".format(template, querystring))

log_url = template
if querystring:
log_url += "?" + querystring
logger.info("Requesting {}".format(log_url))
return request


Expand Down Expand Up @@ -885,9 +902,13 @@ def retrieve_repositories(args, authenticated_user):
)

if args.repository:
if "/" in args.repository:
repo_path = args.repository
else:
repo_path = "{0}/{1}".format(args.user, args.repository)
single_request = True
template = "https://{0}/repos/{1}/{2}".format(
get_github_api_host(args), args.user, args.repository
template = "https://{0}/repos/{1}".format(
get_github_api_host(args), repo_path
)

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


def filter_repositories(args, unfiltered_repositories):
if args.repository:
return unfiltered_repositories
logger.info("Filtering repositories")

repositories = []
Expand Down