-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.py
More file actions
210 lines (160 loc) · 5.55 KB
/
commit.py
File metadata and controls
210 lines (160 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import requests
import os
import re
import time
from datetime import datetime
GITHUB_GRAPHQL_URL = "https://api.github.com/graphql"
def normalize_issue_links(body, owner, repo):
protected = {}
def protect(match):
key = f"__LINK_{len(protected)}__"
protected[key] = match.group(0)
return key
# 1. Protect existing Markdown links
body = re.sub(r"\[[^\]]+\]\([^)]+\)", protect, body)
# 2. Normalize raw PR URLs
body = re.sub(
rf"https://github\.com/{owner}/{repo}/pull/(\d+)",
rf"[#\1](https://github.com/{owner}/{repo}/pull/\1)",
body,
)
# 3. Normalize raw issue URLs
body = re.sub(
rf"https://github\.com/{owner}/{repo}/issues/(\d+)",
rf"[#\1](https://github.com/{owner}/{repo}/issues/\1)",
body,
)
# 4. Normalize raw commit URLs
body = re.sub(
rf"https://github\.com/{owner}/{repo}/commit/([0-9a-f]{{7,40}})",
rf"[\1](https://github.com/{owner}/{repo}/commit/\1)",
body,
)
# 5. Protect Markdown links we just created
body = re.sub(r"\[[^\]]+\]\([^)]+\)", protect, body)
# 6. Normalize bare #123 references (safe now)
body = re.sub(
r"(?<!\w)#(\d+)\b",
rf"[#\1](https://github.com/{owner}/{repo}/issues/\1)",
body,
)
# 7. Restore all protected links
for key, link in protected.items():
body = body.replace(key, link)
return body
def post_with_retry(headers, payload, max_retries=5):
for attempt in range(max_retries):
response = requests.post(
GITHUB_GRAPHQL_URL,
headers=headers,
json=payload
)
if response.status_code == 200:
return response
if response.status_code == 401:
raise RuntimeError("GitHub token is invalid or expired.")
if response.status_code == 403:
reset_time = response.headers.get("X-RateLimit-Reset")
if reset_time:
sleep_seconds = max(0, int(reset_time) - int(time.time()) + 5)
reset_dt = datetime.fromtimestamp(int(reset_time))
print(f"Rate limit hit. Sleeping until {reset_dt} ({sleep_seconds}s)...")
time.sleep(sleep_seconds)
continue
else:
backoff = 2 ** attempt
print(f"Secondary rate limit. Backing off {backoff}s...")
time.sleep(backoff)
continue
response.raise_for_status()
raise RuntimeError("Exceeded maximum retries due to GitHub API limits.")
def save_all_commits(repo, github_token):
owner, project_name = repo.split("/")
repo_dir = project_name
commit_dir = os.path.join(repo_dir, "COMMIT")
os.makedirs(commit_dir, exist_ok=True)
headers = {
"Authorization": f"Bearer {github_token}",
"Content-Type": "application/json",
}
query = """
query($owner: String!, $name: String!, $cursor: String) {
repository(owner: $owner, name: $name) {
defaultBranchRef {
target {
... on Commit {
history(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
oid
message
committedDate
author {
name
email
}
}
}
}
}
}
}
}
"""
cursor = None
while True:
variables = {
"owner": owner,
"name": project_name,
"cursor": cursor,
}
response = post_with_retry(
headers=headers,
payload={"query": query, "variables": variables},
)
data = response.json()
if "errors" in data:
raise RuntimeError(data["errors"])
history = (
data["data"]["repository"]["defaultBranchRef"]
["target"]["history"]
)
commits = history["nodes"]
page_info = history["pageInfo"]
for commit in commits:
sha = commit["oid"]
short_sha = sha[:7]
raw_message = commit["message"]
title_raw, _, body_raw = raw_message.partition("\n")
title = normalize_issue_links(
title_raw.strip(), owner, project_name
)
body = normalize_issue_links(
body_raw.strip(), owner, project_name
)
content = (
f"{title}\n\n"
f"{body}\n"
)
filename = f"{project_name}_commit_{short_sha}.txt"
filepath = os.path.join(commit_dir, filename)
with open(filepath, "w", encoding="utf-8") as f:
f.write(content)
print(f"Saved Commit {short_sha}")
if not page_info["hasNextPage"]:
break
cursor = page_info["endCursor"]
print("Done.")
# ------------------ ENTRY POINT ------------------
if __name__ == "__main__":
repo = input("Enter your repo (owner/repo): ").strip()
github_token = os.getenv("GITHUB_TOKEN")
if not github_token:
github_token = input("Enter GitHub token: ").strip()
save_all_commits(
repo=repo,
github_token=github_token
)