github-backup 0.68.0__py3-none-any.whl → 0.69.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- github_backup/__init__.py +1 -1
- github_backup/github_backup.py +211 -51
- github_backup/graphql_queries.py +67 -0
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/METADATA +6 -3
- github_backup-0.69.0.dist-info/RECORD +12 -0
- github_backup-0.68.0.dist-info/RECORD +0 -12
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/WHEEL +0 -0
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/entry_points.txt +0 -0
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/licenses/LICENSE.txt +0 -0
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/top_level.txt +0 -0
- {github_backup-0.68.0.dist-info → github_backup-0.69.0.dist-info}/zip-safe +0 -0
github_backup/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "0.
|
|
1
|
+
__version__ = "0.69.0"
|
github_backup/github_backup.py
CHANGED
|
@@ -5,7 +5,6 @@ from __future__ import print_function
|
|
|
5
5
|
import argparse
|
|
6
6
|
import base64
|
|
7
7
|
import calendar
|
|
8
|
-
import codecs
|
|
9
8
|
import json
|
|
10
9
|
import logging
|
|
11
10
|
import os
|
|
@@ -42,7 +41,9 @@ from .graphql_queries import (
|
|
|
42
41
|
DISCUSSION_PAGE_SIZE,
|
|
43
42
|
DISCUSSION_REPLIES_QUERY,
|
|
44
43
|
ISSUE_CROSS_REFERENCE_COUNT_QUERY,
|
|
44
|
+
PULL_COMMENTS_QUERY,
|
|
45
45
|
PULL_CROSS_REFERENCE_COUNT_QUERY,
|
|
46
|
+
PULL_PAGE_SIZE,
|
|
46
47
|
)
|
|
47
48
|
|
|
48
49
|
FILE_URI_PREFIX = "file://"
|
|
@@ -901,23 +902,31 @@ def retrieve_data(args, template, query_args=None, paginated=True, lazy=False):
|
|
|
901
902
|
|
|
902
903
|
next_url = None
|
|
903
904
|
|
|
905
|
+
def build_request(auth):
|
|
906
|
+
return _construct_request(
|
|
907
|
+
per_page=per_page if paginated else None,
|
|
908
|
+
query_args=query_args,
|
|
909
|
+
template=next_url or template,
|
|
910
|
+
auth=auth,
|
|
911
|
+
as_app=args.as_app,
|
|
912
|
+
fine=args.token_fine is not None,
|
|
913
|
+
)
|
|
914
|
+
|
|
904
915
|
while True:
|
|
905
916
|
# FIRST: Fetch response
|
|
906
917
|
|
|
907
918
|
for attempt in range(args.max_retries + 1):
|
|
908
|
-
# Resolved per request
|
|
919
|
+
# Resolved per request (and again before each retry) so long
|
|
920
|
+
# runs pick up refreshed app tokens
|
|
909
921
|
auth = get_auth(args, encode=not args.as_app)
|
|
910
|
-
request = _construct_request(
|
|
911
|
-
per_page=per_page if paginated else None,
|
|
912
|
-
query_args=query_args,
|
|
913
|
-
template=next_url or template,
|
|
914
|
-
auth=auth,
|
|
915
|
-
as_app=args.as_app,
|
|
916
|
-
fine=args.token_fine is not None,
|
|
917
|
-
)
|
|
918
922
|
try:
|
|
919
923
|
http_response = make_request_with_retry(
|
|
920
|
-
|
|
924
|
+
build_request(auth),
|
|
925
|
+
auth,
|
|
926
|
+
args.max_retries,
|
|
927
|
+
rebuild_request=lambda: build_request(
|
|
928
|
+
get_auth(args, encode=not args.as_app)
|
|
929
|
+
),
|
|
921
930
|
)
|
|
922
931
|
except HTTPError as exc:
|
|
923
932
|
if exc.code == 451:
|
|
@@ -1028,17 +1037,29 @@ def retrieve_graphql_data(args, query, variables=None, log_context=None):
|
|
|
1028
1037
|
).encode("utf-8")
|
|
1029
1038
|
endpoint = get_github_graphql_url(args)
|
|
1030
1039
|
|
|
1031
|
-
|
|
1040
|
+
def build_request(auth):
|
|
1032
1041
|
request = Request(endpoint, data=payload, method="POST")
|
|
1033
1042
|
request.add_header("Accept", "application/json")
|
|
1034
1043
|
request.add_header("Content-Type", "application/json")
|
|
1035
1044
|
request.add_header("Authorization", "bearer " + auth)
|
|
1045
|
+
return request
|
|
1046
|
+
|
|
1047
|
+
for attempt in range(args.max_retries + 1):
|
|
1048
|
+
if attempt:
|
|
1049
|
+
# Resolved again after a retry wait so long runs pick up refreshed
|
|
1050
|
+
# app tokens
|
|
1051
|
+
auth = get_graphql_auth(args)
|
|
1036
1052
|
log_url = endpoint
|
|
1037
1053
|
if log_context:
|
|
1038
1054
|
log_url = "{0} ({1})".format(log_url, log_context)
|
|
1039
1055
|
logger.info("Requesting {0}".format(log_url))
|
|
1040
1056
|
|
|
1041
|
-
http_response = make_request_with_retry(
|
|
1057
|
+
http_response = make_request_with_retry(
|
|
1058
|
+
build_request(auth),
|
|
1059
|
+
auth,
|
|
1060
|
+
args.max_retries,
|
|
1061
|
+
rebuild_request=lambda: build_request(get_graphql_auth(args)),
|
|
1062
|
+
)
|
|
1042
1063
|
|
|
1043
1064
|
status = http_response.getcode()
|
|
1044
1065
|
if status != 200:
|
|
@@ -1097,8 +1118,14 @@ def retrieve_graphql_data(args, query, variables=None, log_context=None):
|
|
|
1097
1118
|
) # pragma: no cover
|
|
1098
1119
|
|
|
1099
1120
|
|
|
1100
|
-
def make_request_with_retry(request, auth, max_retries=5):
|
|
1101
|
-
"""Make HTTP request with automatic retry for transient errors.
|
|
1121
|
+
def make_request_with_retry(request, auth, max_retries=5, rebuild_request=None):
|
|
1122
|
+
"""Make HTTP request with automatic retry for transient errors.
|
|
1123
|
+
|
|
1124
|
+
If given, rebuild_request is called to build the request again before each
|
|
1125
|
+
retry. A rate-limit wait can last longer than a GitHub App installation
|
|
1126
|
+
token, so callers that resolve credentials per request use it to resend
|
|
1127
|
+
with the token that is current after the wait.
|
|
1128
|
+
"""
|
|
1102
1129
|
|
|
1103
1130
|
def is_retryable_status(status_code, headers):
|
|
1104
1131
|
# Server errors are always retryable
|
|
@@ -1110,6 +1137,8 @@ def make_request_with_retry(request, auth, max_retries=5):
|
|
|
1110
1137
|
return False
|
|
1111
1138
|
|
|
1112
1139
|
for attempt in range(max_retries + 1):
|
|
1140
|
+
if attempt and rebuild_request is not None:
|
|
1141
|
+
request = rebuild_request()
|
|
1113
1142
|
try:
|
|
1114
1143
|
return urlopen(request, context=https_ctx)
|
|
1115
1144
|
|
|
@@ -1420,6 +1449,25 @@ def download_attachment_file(url, path, auth, as_app=False, fine=False):
|
|
|
1420
1449
|
return metadata
|
|
1421
1450
|
|
|
1422
1451
|
|
|
1452
|
+
def needs_jwt_workaround(args, repository, url):
|
|
1453
|
+
"""Whether an attachment must be fetched through a JWT-signed URL.
|
|
1454
|
+
|
|
1455
|
+
Fine-grained PATs (#477) and GitHub App installation tokens get a 404 when
|
|
1456
|
+
downloading attachments from private repositories directly, while classic
|
|
1457
|
+
tokens do not. Image attachments, in both the modern
|
|
1458
|
+
github.com/user-attachments/assets/* and the older
|
|
1459
|
+
github.com/{owner}/{repo}/assets/* format, can be fetched through the
|
|
1460
|
+
JWT-signed URL that the Markdown API renders for them. /files/ URLs cannot.
|
|
1461
|
+
"""
|
|
1462
|
+
if args.token_fine is None and not args.as_app:
|
|
1463
|
+
return False
|
|
1464
|
+
if not repository.get("private", False):
|
|
1465
|
+
return False
|
|
1466
|
+
return "github.com/user-attachments/assets/" in url or bool(
|
|
1467
|
+
re.match(r"https://github\.com/[^/]+/[^/]+/assets/\d+/", url)
|
|
1468
|
+
)
|
|
1469
|
+
|
|
1470
|
+
|
|
1423
1471
|
def get_jwt_signed_url_via_markdown_api(url, token, repo_context):
|
|
1424
1472
|
"""Convert a user-attachments/assets URL to a JWT-signed URL via Markdown API.
|
|
1425
1473
|
|
|
@@ -1427,8 +1475,9 @@ def get_jwt_signed_url_via_markdown_api(url, token, repo_context):
|
|
|
1427
1475
|
JWT-signed private-user-images.githubusercontent.com URLs that work
|
|
1428
1476
|
without token authentication.
|
|
1429
1477
|
|
|
1430
|
-
This is a workaround for issue #477 where fine-grained PATs
|
|
1431
|
-
download
|
|
1478
|
+
This is a workaround for issue #477 where fine-grained PATs (and GitHub
|
|
1479
|
+
App installation tokens) cannot download attachments from private repos
|
|
1480
|
+
directly.
|
|
1432
1481
|
|
|
1433
1482
|
Limitations:
|
|
1434
1483
|
- Only works for /assets/ URLs (images)
|
|
@@ -1436,8 +1485,8 @@ def get_jwt_signed_url_via_markdown_api(url, token, repo_context):
|
|
|
1436
1485
|
- JWT URLs expire after ~5 minutes
|
|
1437
1486
|
|
|
1438
1487
|
Args:
|
|
1439
|
-
url: The
|
|
1440
|
-
token: Raw fine-grained PAT (github_pat_...)
|
|
1488
|
+
url: The image attachment URL (user-attachments/assets or repo assets)
|
|
1489
|
+
token: Raw fine-grained PAT (github_pat_...) or App installation token
|
|
1441
1490
|
repo_context: Repository context as "owner/repo"
|
|
1442
1491
|
|
|
1443
1492
|
Returns:
|
|
@@ -1832,16 +1881,7 @@ def download_attachments(
|
|
|
1832
1881
|
filename = get_attachment_filename(url)
|
|
1833
1882
|
filepath = os.path.join(attachments_dir, filename)
|
|
1834
1883
|
|
|
1835
|
-
|
|
1836
|
-
# from private repos directly (404). Use Markdown API workaround to get
|
|
1837
|
-
# a JWT-signed URL. Only works for /assets/ (images), not /files/.
|
|
1838
|
-
needs_jwt = (
|
|
1839
|
-
args.token_fine is not None
|
|
1840
|
-
and repository.get("private", False)
|
|
1841
|
-
and "github.com/user-attachments/assets/" in url
|
|
1842
|
-
)
|
|
1843
|
-
|
|
1844
|
-
if not needs_jwt:
|
|
1884
|
+
if not needs_jwt_workaround(args, repository, url):
|
|
1845
1885
|
# NORMAL download path
|
|
1846
1886
|
metadata = download_attachment_file(
|
|
1847
1887
|
url,
|
|
@@ -1851,7 +1891,7 @@ def download_attachments(
|
|
|
1851
1891
|
fine=args.token_fine is not None,
|
|
1852
1892
|
)
|
|
1853
1893
|
elif jwt_url := get_jwt_signed_url_via_markdown_api(
|
|
1854
|
-
url, args
|
|
1894
|
+
url, get_auth(args, encode=False), repository["full_name"]
|
|
1855
1895
|
):
|
|
1856
1896
|
# JWT needed and extracted, download via JWT
|
|
1857
1897
|
metadata = download_attachment_file(
|
|
@@ -1865,8 +1905,8 @@ def download_attachments(
|
|
|
1865
1905
|
"url": url,
|
|
1866
1906
|
"success": False,
|
|
1867
1907
|
"skipped_at": datetime.now(timezone.utc).isoformat(),
|
|
1868
|
-
"error": "Fine-grained
|
|
1869
|
-
"Markdown API workaround failed. Use --token-classic instead.",
|
|
1908
|
+
"error": "Fine-grained and GitHub App tokens cannot download private repo "
|
|
1909
|
+
"attachments. Markdown API workaround failed. Use --token-classic instead.",
|
|
1870
1910
|
}
|
|
1871
1911
|
logger.warning(
|
|
1872
1912
|
"Skipping attachment {0}: {1}".format(url, metadata["error"])
|
|
@@ -2276,7 +2316,7 @@ def gist_backup_is_current(repository, repo_cwd, repo_dir):
|
|
|
2276
2316
|
if not updated_at:
|
|
2277
2317
|
return False
|
|
2278
2318
|
try:
|
|
2279
|
-
with
|
|
2319
|
+
with open(os.path.join(repo_cwd, "gist.json"), encoding="utf-8", newline="") as f:
|
|
2280
2320
|
stored = json.load(f)
|
|
2281
2321
|
except (OSError, ValueError):
|
|
2282
2322
|
return False
|
|
@@ -2349,7 +2389,7 @@ def backup_repositories(args, output_directory, repositories):
|
|
|
2349
2389
|
# DMCA-blocked or empty gist), so make sure the directory exists
|
|
2350
2390
|
mkdir_p(repo_cwd)
|
|
2351
2391
|
output_file = "{0}/gist.json".format(repo_cwd)
|
|
2352
|
-
with
|
|
2392
|
+
with open(output_file, "w", encoding="utf-8", newline="") as f:
|
|
2353
2393
|
json_dump(repository, f)
|
|
2354
2394
|
|
|
2355
2395
|
continue # don't try to back anything else for a gist; it doesn't exist
|
|
@@ -2367,7 +2407,7 @@ def backup_repositories(args, output_directory, repositories):
|
|
|
2367
2407
|
lfs_clone=args.lfs_clone,
|
|
2368
2408
|
no_prune=args.no_prune,
|
|
2369
2409
|
)
|
|
2370
|
-
if args.include_issues or args.include_everything:
|
|
2410
|
+
if repository["has_issues"] and (args.include_issues or args.include_everything):
|
|
2371
2411
|
incremental_resource_work_attempted = True
|
|
2372
2412
|
issue_cwd = os.path.join(repo_cwd, "issues")
|
|
2373
2413
|
args.since = read_resource_last_update(
|
|
@@ -2376,7 +2416,7 @@ def backup_repositories(args, output_directory, repositories):
|
|
|
2376
2416
|
backup_issues(args, repo_cwd, repository, repos_template)
|
|
2377
2417
|
write_resource_last_update(args, issue_cwd, repository)
|
|
2378
2418
|
|
|
2379
|
-
if args.include_pulls or args.include_everything:
|
|
2419
|
+
if repository.get("has_pull_requests", True) and (args.include_pulls or args.include_everything):
|
|
2380
2420
|
incremental_resource_work_attempted = True
|
|
2381
2421
|
pulls_cwd = os.path.join(repo_cwd, "pulls")
|
|
2382
2422
|
args.since = read_resource_last_update(
|
|
@@ -2385,7 +2425,7 @@ def backup_repositories(args, output_directory, repositories):
|
|
|
2385
2425
|
backup_pulls(args, repo_cwd, repository, repos_template)
|
|
2386
2426
|
write_resource_last_update(args, pulls_cwd, repository)
|
|
2387
2427
|
|
|
2388
|
-
if args.include_discussions or args.include_everything:
|
|
2428
|
+
if repository["has_discussions"] and (args.include_discussions or args.include_everything):
|
|
2389
2429
|
backup_discussions(args, repo_cwd, repository)
|
|
2390
2430
|
|
|
2391
2431
|
if args.include_milestones or args.include_everything:
|
|
@@ -2993,7 +3033,7 @@ def backup_issues(args, repo_cwd, repository, repos_template):
|
|
|
2993
3033
|
args, issue_cwd, issues[number], number, repository, item_type="issue"
|
|
2994
3034
|
)
|
|
2995
3035
|
|
|
2996
|
-
with
|
|
3036
|
+
with open(issue_file + ".temp", "w", encoding="utf-8", newline="") as f:
|
|
2997
3037
|
json_dump(issue, f)
|
|
2998
3038
|
os.replace(issue_file + ".temp", issue_file) # Atomic write
|
|
2999
3039
|
|
|
@@ -3012,7 +3052,7 @@ def read_json_file_if_exists(path):
|
|
|
3012
3052
|
return None
|
|
3013
3053
|
|
|
3014
3054
|
try:
|
|
3015
|
-
with
|
|
3055
|
+
with open(path, "r", encoding="utf-8", newline="") as f:
|
|
3016
3056
|
return json.load(f)
|
|
3017
3057
|
except (OSError, UnicodeDecodeError, json.decoder.JSONDecodeError) as e:
|
|
3018
3058
|
logger.debug("Error reading existing JSON file {0}: {1}".format(path, e))
|
|
@@ -3050,6 +3090,51 @@ def get_pull_reviews_since(args, pulls_cwd):
|
|
|
3050
3090
|
return args_since or reviews_since, reviews_since, reviews_last_update_path
|
|
3051
3091
|
|
|
3052
3092
|
|
|
3093
|
+
def get_pull_comments_with_graphql(args, repository, number):
|
|
3094
|
+
after = None
|
|
3095
|
+
comments_page = 1
|
|
3096
|
+
comments = []
|
|
3097
|
+
comment_total_count = 0
|
|
3098
|
+
owner, name = _repository_owner_name(repository)
|
|
3099
|
+
while True:
|
|
3100
|
+
data = retrieve_graphql_data(
|
|
3101
|
+
args,
|
|
3102
|
+
PULL_COMMENTS_QUERY,
|
|
3103
|
+
{
|
|
3104
|
+
"owner": owner,
|
|
3105
|
+
"name": name,
|
|
3106
|
+
"number": number,
|
|
3107
|
+
"after": after,
|
|
3108
|
+
"pageSize": PULL_PAGE_SIZE,
|
|
3109
|
+
},
|
|
3110
|
+
log_context="pull request {0}#{1} comments page {2}".format(
|
|
3111
|
+
repository["full_name"], number, comments_page
|
|
3112
|
+
),
|
|
3113
|
+
)
|
|
3114
|
+
repository_data = data.get("repository") or {}
|
|
3115
|
+
pr_data = repository_data.get("pullRequest")
|
|
3116
|
+
if pr_data is None:
|
|
3117
|
+
raise Exception(
|
|
3118
|
+
"Pull request #{0} not found in {1}".format(
|
|
3119
|
+
number, repository["full_name"]
|
|
3120
|
+
)
|
|
3121
|
+
)
|
|
3122
|
+
|
|
3123
|
+
comments_connection = pr_data.get("comments") or {}
|
|
3124
|
+
comment_total_count = comments_connection.get(
|
|
3125
|
+
"totalCount", comment_total_count
|
|
3126
|
+
)
|
|
3127
|
+
comments.extend(_connection_nodes(comments_connection))
|
|
3128
|
+
|
|
3129
|
+
page_info = comments_connection.get("pageInfo") or {}
|
|
3130
|
+
if not page_info.get("hasNextPage"):
|
|
3131
|
+
break
|
|
3132
|
+
|
|
3133
|
+
after = page_info.get("endCursor")
|
|
3134
|
+
comments_page += 1
|
|
3135
|
+
return comments
|
|
3136
|
+
|
|
3137
|
+
|
|
3053
3138
|
def backup_pulls(args, repo_cwd, repository, repos_template):
|
|
3054
3139
|
has_pulls_dir = os.path.isdir("{0}/pulls/.git".format(repo_cwd))
|
|
3055
3140
|
if args.skip_existing and has_pulls_dir:
|
|
@@ -3176,7 +3261,61 @@ def backup_pulls(args, repo_cwd, repository, repos_template):
|
|
|
3176
3261
|
args.include_pull_comments or args.include_everything
|
|
3177
3262
|
) and should_fetch_non_review_data:
|
|
3178
3263
|
template = comments_regular_template.format(number)
|
|
3179
|
-
|
|
3264
|
+
try:
|
|
3265
|
+
pulls[number]["comment_regular_data"] = retrieve_data(args, template)
|
|
3266
|
+
except HTTPError as e:
|
|
3267
|
+
# Issue #547: REST API endpoint for regular comments on temporary
|
|
3268
|
+
# private forks returns 404. There's nothing in repo data that would
|
|
3269
|
+
# indicate that repo is private temporary fork so best we can do is
|
|
3270
|
+
# limit this special case a bit more by limiting it to private repos.
|
|
3271
|
+
if not repository.get("private", False) or e.code != 404:
|
|
3272
|
+
raise
|
|
3273
|
+
pulls[number]["comment_regular_data"] = get_pull_comments_with_graphql(
|
|
3274
|
+
args, repository, number
|
|
3275
|
+
)
|
|
3276
|
+
after = None
|
|
3277
|
+
comments_page = 1
|
|
3278
|
+
comments = []
|
|
3279
|
+
comment_total_count = 0
|
|
3280
|
+
owner, name = _repository_owner_name(repository)
|
|
3281
|
+
while True:
|
|
3282
|
+
data = retrieve_graphql_data(
|
|
3283
|
+
args,
|
|
3284
|
+
PULL_COMMENTS_QUERY,
|
|
3285
|
+
{
|
|
3286
|
+
"owner": owner,
|
|
3287
|
+
"name": name,
|
|
3288
|
+
"number": number,
|
|
3289
|
+
"after": after,
|
|
3290
|
+
"pageSize": PULL_PAGE_SIZE,
|
|
3291
|
+
},
|
|
3292
|
+
log_context="pull request {0}#{1} comments page {2}".format(
|
|
3293
|
+
repository["full_name"], number, comments_page
|
|
3294
|
+
),
|
|
3295
|
+
)
|
|
3296
|
+
repository_data = data.get("repository") or {}
|
|
3297
|
+
pr_data = repository_data.get("pullRequest")
|
|
3298
|
+
if pr_data is None:
|
|
3299
|
+
raise Exception(
|
|
3300
|
+
"Pull request #{0} not found in {1}".format(
|
|
3301
|
+
number, repository["full_name"]
|
|
3302
|
+
)
|
|
3303
|
+
)
|
|
3304
|
+
|
|
3305
|
+
comments_connection = pr_data.get("comments") or {}
|
|
3306
|
+
comment_total_count = comments_connection.get(
|
|
3307
|
+
"totalCount", comment_total_count
|
|
3308
|
+
)
|
|
3309
|
+
comments.extend(_connection_nodes(comments_connection))
|
|
3310
|
+
|
|
3311
|
+
page_info = comments_connection.get("pageInfo") or {}
|
|
3312
|
+
if not page_info.get("hasNextPage"):
|
|
3313
|
+
break
|
|
3314
|
+
|
|
3315
|
+
after = page_info.get("endCursor")
|
|
3316
|
+
comments_page += 1
|
|
3317
|
+
pulls[number]["comment_regular_data"] = comments
|
|
3318
|
+
|
|
3180
3319
|
template = comments_template.format(number)
|
|
3181
3320
|
pulls[number]["comment_data"] = retrieve_data(args, template)
|
|
3182
3321
|
if include_pull_reviews:
|
|
@@ -3202,7 +3341,7 @@ def backup_pulls(args, repo_cwd, repository, repos_template):
|
|
|
3202
3341
|
|
|
3203
3342
|
restore_existing_pull_optional_data(pull, existing_pull)
|
|
3204
3343
|
|
|
3205
|
-
with
|
|
3344
|
+
with open(pull_file + ".temp", "w", encoding="utf-8", newline="") as f:
|
|
3206
3345
|
json_dump(pull, f)
|
|
3207
3346
|
os.replace(pull_file + ".temp", pull_file) # Atomic write
|
|
3208
3347
|
|
|
@@ -3224,13 +3363,20 @@ def backup_milestones(args, repo_cwd, repository, repos_template):
|
|
|
3224
3363
|
return
|
|
3225
3364
|
|
|
3226
3365
|
logger.info("Retrieving {0} milestones".format(repository["full_name"]))
|
|
3227
|
-
mkdir_p(repo_cwd, milestone_cwd)
|
|
3228
3366
|
|
|
3229
3367
|
template = "{0}/{1}/milestones".format(repos_template, repository["full_name"])
|
|
3230
3368
|
|
|
3231
3369
|
query_args = {"state": "all"}
|
|
3232
3370
|
|
|
3233
|
-
|
|
3371
|
+
try:
|
|
3372
|
+
_milestones = retrieve_data(args, template, query_args=query_args)
|
|
3373
|
+
except HTTPError as e:
|
|
3374
|
+
if e.code == 404:
|
|
3375
|
+
logger.info("Milestones are not available for this repository, skipping")
|
|
3376
|
+
return
|
|
3377
|
+
raise
|
|
3378
|
+
|
|
3379
|
+
mkdir_p(repo_cwd, milestone_cwd)
|
|
3234
3380
|
|
|
3235
3381
|
milestones = {}
|
|
3236
3382
|
for milestone in _milestones:
|
|
@@ -3303,7 +3449,13 @@ def backup_labels(args, repo_cwd, repository, repos_template):
|
|
|
3303
3449
|
label_cwd = os.path.join(repo_cwd, "labels")
|
|
3304
3450
|
output_file = "{0}/labels.json".format(label_cwd)
|
|
3305
3451
|
template = "{0}/{1}/labels".format(repos_template, repository["full_name"])
|
|
3306
|
-
|
|
3452
|
+
try:
|
|
3453
|
+
_backup_data(args, "labels", template, output_file, label_cwd)
|
|
3454
|
+
except HTTPError as e:
|
|
3455
|
+
if e.code == 404:
|
|
3456
|
+
logger.info("Labels are not available for this repository, skipping")
|
|
3457
|
+
return
|
|
3458
|
+
raise
|
|
3307
3459
|
|
|
3308
3460
|
|
|
3309
3461
|
def backup_hooks(args, repo_cwd, repository, repos_template):
|
|
@@ -3340,15 +3492,23 @@ def backup_hooks(args, repo_cwd, repository, repos_template):
|
|
|
3340
3492
|
def backup_releases(args, repo_cwd, repository, repos_template, include_assets=False):
|
|
3341
3493
|
repository_fullname = repository["full_name"]
|
|
3342
3494
|
|
|
3343
|
-
#
|
|
3344
|
-
release_cwd = os.path.join(repo_cwd, "releases")
|
|
3495
|
+
# log intent
|
|
3345
3496
|
logger.info("Retrieving {0} releases".format(repository_fullname))
|
|
3346
|
-
mkdir_p(repo_cwd, release_cwd)
|
|
3347
3497
|
|
|
3348
3498
|
query_args = {}
|
|
3349
3499
|
|
|
3350
3500
|
release_template = "{0}/{1}/releases".format(repos_template, repository_fullname)
|
|
3351
|
-
|
|
3501
|
+
try:
|
|
3502
|
+
releases = retrieve_data(args, release_template, query_args=query_args)
|
|
3503
|
+
except HTTPError as e:
|
|
3504
|
+
if e.code == 404:
|
|
3505
|
+
logger.info("Releases are not available for this repository, skipping")
|
|
3506
|
+
return
|
|
3507
|
+
raise
|
|
3508
|
+
|
|
3509
|
+
# give release files somewhere to live
|
|
3510
|
+
release_cwd = os.path.join(repo_cwd, "releases")
|
|
3511
|
+
mkdir_p(repo_cwd, release_cwd)
|
|
3352
3512
|
|
|
3353
3513
|
if args.skip_prerelease:
|
|
3354
3514
|
releases = [r for r in releases if not r["prerelease"] and not r["draft"]]
|
|
@@ -3567,9 +3727,9 @@ def _backup_data(args, name, template, output_file, output_directory):
|
|
|
3567
3727
|
skip_existing = args.skip_existing
|
|
3568
3728
|
if not skip_existing or not os.path.exists(output_file):
|
|
3569
3729
|
logger.info("Retrieving {0} {1}".format(args.user, name))
|
|
3570
|
-
mkdir_p(output_directory)
|
|
3571
3730
|
data = retrieve_data(args, template)
|
|
3572
3731
|
|
|
3732
|
+
mkdir_p(output_directory)
|
|
3573
3733
|
if json_dump_if_changed(data, output_file):
|
|
3574
3734
|
logger.info("Saved {0} {1} to disk".format(len(data), name))
|
|
3575
3735
|
else:
|
|
@@ -3618,7 +3778,7 @@ def json_dump_if_changed(data, output_file_path):
|
|
|
3618
3778
|
# Check if file exists and compare content
|
|
3619
3779
|
if os.path.exists(output_file_path):
|
|
3620
3780
|
try:
|
|
3621
|
-
with
|
|
3781
|
+
with open(output_file_path, "r", encoding="utf-8", newline="") as f:
|
|
3622
3782
|
existing_content = f.read()
|
|
3623
3783
|
if existing_content == new_content:
|
|
3624
3784
|
logger.debug(
|
|
@@ -3635,7 +3795,7 @@ def json_dump_if_changed(data, output_file_path):
|
|
|
3635
3795
|
|
|
3636
3796
|
# Write the file atomically using temp file + rename
|
|
3637
3797
|
temp_file = output_file_path + ".temp"
|
|
3638
|
-
with
|
|
3798
|
+
with open(temp_file, "w", encoding="utf-8", newline="") as f:
|
|
3639
3799
|
f.write(new_content)
|
|
3640
3800
|
os.replace(temp_file, output_file_path) # Atomic write
|
|
3641
3801
|
return True
|
github_backup/graphql_queries.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""GraphQL query templates used by github-backup."""
|
|
2
2
|
|
|
3
3
|
DISCUSSION_PAGE_SIZE = 100
|
|
4
|
+
PULL_PAGE_SIZE = 100
|
|
4
5
|
|
|
5
6
|
CROSS_REFERENCE_PAGE_SIZE = 100
|
|
6
7
|
|
|
@@ -62,6 +63,72 @@ query($owner: String!, $name: String!, $after: String, $pageSize: Int!,
|
|
|
62
63
|
}
|
|
63
64
|
"""
|
|
64
65
|
|
|
66
|
+
PULL_COMMENTS_QUERY = """
|
|
67
|
+
query($owner: String!, $name: String!, $number: Int!, $after: String, $pageSize: Int!) {
|
|
68
|
+
repository(owner: $owner, name: $name) {
|
|
69
|
+
pullRequest(number: $number) {
|
|
70
|
+
comments(first: $pageSize, after: $after) {
|
|
71
|
+
totalCount
|
|
72
|
+
nodes {
|
|
73
|
+
...IssueCommentFields
|
|
74
|
+
}
|
|
75
|
+
pageInfo {
|
|
76
|
+
hasNextPage
|
|
77
|
+
endCursor
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fragment ActorFields on Actor {
|
|
85
|
+
avatarUrl
|
|
86
|
+
login
|
|
87
|
+
resourcePath
|
|
88
|
+
url
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fragment ReactionGroupFields on ReactionGroup {
|
|
92
|
+
content
|
|
93
|
+
reactors {
|
|
94
|
+
totalCount
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
fragment IssueCommentFields on IssueComment {
|
|
99
|
+
author {
|
|
100
|
+
...ActorFields
|
|
101
|
+
}
|
|
102
|
+
authorAssociation
|
|
103
|
+
body
|
|
104
|
+
bodyHTML
|
|
105
|
+
bodyText
|
|
106
|
+
createdAt
|
|
107
|
+
createdViaEmail
|
|
108
|
+
databaseId
|
|
109
|
+
editor {
|
|
110
|
+
...ActorFields
|
|
111
|
+
}
|
|
112
|
+
id
|
|
113
|
+
includesCreatedEdit
|
|
114
|
+
isMinimized
|
|
115
|
+
isPinned
|
|
116
|
+
lastEditedAt
|
|
117
|
+
minimizedReason
|
|
118
|
+
pinnedAt
|
|
119
|
+
pinnedBy {
|
|
120
|
+
...ActorFields
|
|
121
|
+
}
|
|
122
|
+
publishedAt
|
|
123
|
+
reactionGroups {
|
|
124
|
+
...ReactionGroupFields
|
|
125
|
+
}
|
|
126
|
+
resourcePath
|
|
127
|
+
updatedAt
|
|
128
|
+
url
|
|
129
|
+
}
|
|
130
|
+
"""
|
|
131
|
+
|
|
65
132
|
DISCUSSION_LIST_QUERY = """
|
|
66
133
|
query($owner: String!, $name: String!, $after: String, $pageSize: Int!) {
|
|
67
134
|
repository(owner: $owner, name: $name) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: github-backup
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.69.0
|
|
4
4
|
Summary: backup a github user or organization
|
|
5
5
|
Home-page: http://github.com/josegonzalez/python-github-backup
|
|
6
6
|
Author: Jose Diaz-Gonzalez
|
|
@@ -13,6 +13,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.12
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
17
|
Requires-Python: >=3.11
|
|
17
18
|
Description-Content-Type: text/x-rst
|
|
18
19
|
License-File: LICENSE.txt
|
|
@@ -343,7 +344,7 @@ The tool automatically extracts file extensions from HTTP headers to ensure file
|
|
|
343
344
|
|
|
344
345
|
**Repository filtering** for repo files/assets handles renamed and transferred repositories gracefully. URLs are included if they either match the current repository name directly, or redirect to it (e.g., ``willmcgugan/rich`` redirects to ``Textualize/rich`` after transfer).
|
|
345
346
|
|
|
346
|
-
**Fine-grained token limitation:** Due to a GitHub platform limitation, fine-grained personal access tokens (``github_pat_...``) cannot download attachments from private repositories directly. This affects
|
|
347
|
+
**Fine-grained and GitHub App token limitation:** Due to a GitHub platform limitation, fine-grained personal access tokens (``github_pat_...``) and GitHub App installation tokens (``--as-app`` or ``--app-id``) cannot download attachments from private repositories directly. This affects ``/assets/`` (images) and ``/files/`` (documents) URLs, in both the ``github.com/user-attachments/`` and the older ``github.com/{owner}/{repo}/`` format. The tool implements a workaround for image attachments using GitHub's Markdown API, which converts URLs to temporary JWT-signed URLs that can be downloaded. The token needs read access to contents for this (included in the permissions above); without it, the Markdown API still responds but does not sign the image URLs. However, this workaround only works for images - document attachments (PDFs, text files, etc.) will fail with 404 errors when using these tokens on private repos. For full attachment support on private repositories, use a classic token (``-t``) instead. See `#477 <https://github.com/josegonzalez/python-github-backup/issues/477>`_ for details.
|
|
347
348
|
|
|
348
349
|
|
|
349
350
|
About Discussions
|
|
@@ -562,6 +563,8 @@ Development
|
|
|
562
563
|
|
|
563
564
|
This project is considered feature complete for the primary maintainer @josegonzalez. If you would like a bugfix or enhancement, pull requests are welcome. Feel free to contact the maintainer for consulting estimates if you'd like to sponsor the work instead.
|
|
564
565
|
|
|
566
|
+
If you use a coding agent, point it at `AGENTS.md <AGENTS.md>`_, which collects the conventions that reviews of past pull requests have settled on.
|
|
567
|
+
|
|
565
568
|
Contibuters
|
|
566
569
|
-----------
|
|
567
570
|
|
|
@@ -582,7 +585,7 @@ To run the test suite::
|
|
|
582
585
|
To run linting::
|
|
583
586
|
|
|
584
587
|
pip install flake8
|
|
585
|
-
flake8 --ignore=E501
|
|
588
|
+
flake8 --ignore=E501,E203,W503
|
|
586
589
|
|
|
587
590
|
|
|
588
591
|
.. |PyPI| image:: https://img.shields.io/pypi/v/github-backup.svg
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
github_backup/__init__.py,sha256=KqW3RShDvR3XpTHqOOZs4x2eNNCNE3rl8jGUOBXzcw0,23
|
|
2
|
+
github_backup/__main__.py,sha256=3yorCiC63maaxbjdtaeAs-G-yAAxKSO3mprhGfa1Fho,271
|
|
3
|
+
github_backup/cli.py,sha256=JTiXPOvB8id8LopREOEd1W_FLvuf9SOZU5buMRPFd9U,2922
|
|
4
|
+
github_backup/github_backup.py,sha256=8MVNXzpDI-tusQHlrUGXxDn45E2d3byYS7rZGZCiDi8,139502
|
|
5
|
+
github_backup/graphql_queries.py,sha256=TwzMYPVi0vgVCYSPx1-i_KjKKjmpBxNECJkni8WHfVI,7769
|
|
6
|
+
github_backup-0.69.0.dist-info/licenses/LICENSE.txt,sha256=ML-pLyn1qtAKo_DHMcQ8-p7k3f8cp-IR-mf5B49R2rk,1085
|
|
7
|
+
github_backup-0.69.0.dist-info/METADATA,sha256=7W4EdIUVHSho8KOD63_33zc35cB3Ld5PEm0ITP-6BE0,38411
|
|
8
|
+
github_backup-0.69.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
9
|
+
github_backup-0.69.0.dist-info/entry_points.txt,sha256=cLPKK7MqC77QVb6HnT2mnyL_f-ciavJG_Y9vVHlCF68,57
|
|
10
|
+
github_backup-0.69.0.dist-info/top_level.txt,sha256=Za6fKRAlxZ0b3ZBH_osPqklLe14LetdNAkvAQANT2AA,14
|
|
11
|
+
github_backup-0.69.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
12
|
+
github_backup-0.69.0.dist-info/RECORD,,
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
github_backup/__init__.py,sha256=HxxTcUQtaiYOEEtr3P4K4pOTRoC_pmpqzV7TOSUOpSQ,23
|
|
2
|
-
github_backup/__main__.py,sha256=3yorCiC63maaxbjdtaeAs-G-yAAxKSO3mprhGfa1Fho,271
|
|
3
|
-
github_backup/cli.py,sha256=JTiXPOvB8id8LopREOEd1W_FLvuf9SOZU5buMRPFd9U,2922
|
|
4
|
-
github_backup/github_backup.py,sha256=8tdSyY06L-p01QX93jigaZ4cYhmlB1At0dAEZNtvpo0,133402
|
|
5
|
-
github_backup/graphql_queries.py,sha256=XcAQyp9nF7Ip9UzxsrXMkl2pO-W7SK8hhO2oE9HVcmA,6658
|
|
6
|
-
github_backup-0.68.0.dist-info/licenses/LICENSE.txt,sha256=ML-pLyn1qtAKo_DHMcQ8-p7k3f8cp-IR-mf5B49R2rk,1085
|
|
7
|
-
github_backup-0.68.0.dist-info/METADATA,sha256=8JWYRvjPFq6v6uUWotBGBKk1rV6CIPsqgagZVTNGVnw,37903
|
|
8
|
-
github_backup-0.68.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
9
|
-
github_backup-0.68.0.dist-info/entry_points.txt,sha256=cLPKK7MqC77QVb6HnT2mnyL_f-ciavJG_Y9vVHlCF68,57
|
|
10
|
-
github_backup-0.68.0.dist-info/top_level.txt,sha256=Za6fKRAlxZ0b3ZBH_osPqklLe14LetdNAkvAQANT2AA,14
|
|
11
|
-
github_backup-0.68.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
12
|
-
github_backup-0.68.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|