Repository navigation
Expand file tree
/
Copy pathgithub_utils.py
More file actions
328 lines (278 loc) · 10.6 KB
/
Copy pathgithub_utils.py
File metadata and controls
328 lines (278 loc) · 10.6 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""GitHub Utilities"""
from __future__ import annotations
import json
import os
import warnings
from dataclasses import dataclass
from typing import Any, cast, TYPE_CHECKING
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.request import Request, urlopen
if TYPE_CHECKING:
from collections.abc import Callable
GITHUB_API_URL = "https://api.github.com"
@dataclass
class GitHubComment:
body_text: str
created_at: str
author_login: str
author_url: str | None
author_association: str
editor_login: str | None
database_id: int
url: str
def gh_fetch_url_and_headers(
url: str,
*,
headers: dict[str, str] | None = None,
data: dict[str, Any] | None | str = None,
method: str | None = None,
reader: Callable[[Any], Any] = lambda x: x.read(),
timeout: float | None = None,
) -> tuple[Any, Any]:
if headers is None:
headers = {}
token = os.environ.get("GITHUB_TOKEN")
if token is not None and url.startswith(f"{GITHUB_API_URL}/"):
headers["Authorization"] = f"token {token}"
data_ = None
if data is not None:
data_ = data.encode() if isinstance(data, str) else json.dumps(data).encode()
try:
request = Request(url, headers=headers, data=data_, method=method)
# urlopen's own default applies when no timeout is given.
kwargs = {} if timeout is None else {"timeout": timeout}
with urlopen(request, **kwargs) as conn:
return conn.headers, reader(conn)
except HTTPError as err:
if (
err.code == 403
and all(
key in err.headers
for key in ["X-RateLimit-Limit", "X-RateLimit-Remaining"]
)
and int(err.headers["X-RateLimit-Remaining"]) == 0
):
print(
f"""{url}
Rate limit exceeded:
Used: {err.headers["X-RateLimit-Used"]}
Limit: {err.headers["X-RateLimit-Limit"]}
Remaining: {err.headers["X-RateLimit-Remaining"]}
Resets at: {err.headers["x-RateLimit-Reset"]}"""
)
else:
print(f"Error fetching {url} {err}")
raise
def gh_fetch_url(
url: str,
*,
headers: dict[str, str] | None = None,
data: dict[str, Any] | None | str = None,
method: str | None = None,
reader: Callable[[Any], Any] = json.load,
timeout: float | None = None,
) -> Any:
return gh_fetch_url_and_headers(
url, headers=headers, data=data, reader=reader, method=method, timeout=timeout
)[1]
def gh_fetch_json(
url: str,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
method: str | None = None,
) -> list[dict[str, Any]]:
headers = {"Accept": "application/vnd.github.v3+json"}
if params is not None and len(params) > 0:
url += "?" + "&".join(
f"{name}={quote(str(val))}" for name, val in params.items()
)
return cast(
list[dict[str, Any]],
gh_fetch_url(url, headers=headers, data=data, reader=json.load, method=method),
)
def _gh_fetch_json_any(
url: str,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> Any:
headers = {"Accept": "application/vnd.github.v3+json"}
if params is not None and len(params) > 0:
url += "?" + "&".join(
f"{name}={quote(str(val))}" for name, val in params.items()
)
return gh_fetch_url(url, headers=headers, data=data, reader=json.load)
def gh_fetch_json_list(
url: str,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> list[dict[str, Any]]:
return cast(list[dict[str, Any]], _gh_fetch_json_any(url, params, data))
def gh_fetch_json_dict(
url: str,
params: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
) -> dict[str, Any]:
return cast(dict[str, Any], _gh_fetch_json_any(url, params, data))
def _truncate(text: str, limit: int) -> str:
if len(text) <= limit:
return text
return text[:limit] + f"... ({len(text) - limit} more chars truncated)"
class GHGraphQLError(RuntimeError):
"""GraphQL response contained errors; the raw response is kept so callers
can tolerate partial failures (GraphQL still returns data for the paths
that resolved)."""
def __init__(
self,
message: str,
response: dict[str, Any],
query: str | None = None,
variables: dict[str, Any] | None = None,
) -> None:
super().__init__(message)
self.response = response
self.query = query
self.variables = variables
if query is not None and hasattr(self, "add_note"): # Python 3.11+
# Shown in tracebacks but not in str(e), which stays short for comments
self.add_note(self.details_markdown())
def details_markdown(self) -> str:
"""The query, variables and raw errors, for a collapsed comment section.
Each part is capped so the whole comment stays well under GitHub's
65536-char limit.
"""
parts: list[str] = []
if self.variables is not None:
variables = _truncate(json.dumps(self.variables, indent=2), 2000)
parts.append(f"GraphQL variables:\n```json\n{variables}\n```")
errors = _truncate(json.dumps(self.response.get("errors"), indent=2), 20000)
parts.append(f"GraphQL errors:\n```json\n{errors}\n```")
if self.query is not None:
query = _truncate(self.query.strip(), 10000)
parts.append(f"GraphQL query:\n```graphql\n{query}\n```")
return "\n\n".join(parts)
def gh_graphql(query: str, **kwargs: Any) -> dict[str, Any]:
rc = gh_fetch_url(
"https://api.github.com/graphql", # @lint-ignore
data={"query": query, "variables": kwargs},
reader=json.load,
)
if "errors" in rc:
errors = rc["errors"]
if not isinstance(errors, list):
errors = [] if errors is None else [errors]
messages = "; ".join(
str(err.get("message", err)) if isinstance(err, dict) else str(err)
for err in errors
)
# One line, so a newline in the message can't open a markdown block
messages = _truncate(" ".join(messages.split()), 2000)
err = GHGraphQLError(
f"GitHub GraphQL query failed: {messages or '(no error message)'}",
rc,
query=query,
variables=kwargs,
)
# Keep the full context in the job log; mergebot runs on Python 3.10,
# where tracebacks don't show add_note()
print(f"{err}\n{err.details_markdown()}")
raise err
return cast(dict[str, Any], rc)
def _gh_post_comment(
url: str, comment: str, dry_run: bool = False
) -> list[dict[str, Any]]:
if dry_run:
print(comment)
return []
return gh_fetch_json_list(url, data={"body": comment})
def gh_post_pr_comment(
org: str, repo: str, pr_num: int, comment: str, dry_run: bool = False
) -> list[dict[str, Any]]:
return _gh_post_comment(
f"{GITHUB_API_URL}/repos/{org}/{repo}/issues/{pr_num}/comments",
comment,
dry_run,
)
def gh_post_commit_comment(
org: str, repo: str, sha: str, comment: str, dry_run: bool = False
) -> list[dict[str, Any]]:
return _gh_post_comment(
f"{GITHUB_API_URL}/repos/{org}/{repo}/commits/{sha}/comments",
comment,
dry_run,
)
def gh_close_pr(org: str, repo: str, pr_num: int, dry_run: bool = False) -> None:
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/pulls/{pr_num}"
if dry_run:
print(f"Dry run closing PR {pr_num}")
else:
gh_fetch_url(url, method="PATCH", data={"state": "closed"})
def gh_merge_pr(
org: str,
repo: str,
pr_num: int,
*,
merge_method: str = "squash",
commit_title: str | None = None,
commit_message: str | None = None,
sha: str | None = None,
dry_run: bool = False,
) -> str:
"""Merge a PR via GitHub's merge API and return the resulting merge commit sha."""
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/pulls/{pr_num}/merge"
data: dict[str, Any] = {"merge_method": merge_method}
if commit_title is not None:
data["commit_title"] = commit_title
if commit_message is not None:
data["commit_message"] = commit_message
if sha is not None:
data["sha"] = sha
if dry_run:
print(f"[dry_run] Merging PR {pr_num} via GitHub API with {data}")
return ""
headers = {"Accept": "application/vnd.github.v3+json"}
resp = gh_fetch_url(url, headers=headers, data=data, method="PUT", reader=json.load)
return cast(str, resp["sha"])
def gh_delete_comment(org: str, repo: str, comment_id: int) -> None:
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/issues/comments/{comment_id}"
gh_fetch_url(url, method="DELETE", reader=lambda x: x.read())
def gh_fetch_merge_base(org: str, repo: str, base: str, head: str) -> str:
merge_base = ""
# Get the merge base using the GitHub REST API. This is the same as using
# git merge-base without the need to have git. The API doc can be found at
# https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits
try:
json_data = gh_fetch_url(
f"{GITHUB_API_URL}/repos/{org}/{repo}/compare/{base}...{head}",
headers={"Accept": "application/vnd.github.v3+json"},
reader=json.load,
)
if json_data:
merge_base = json_data.get("merge_base_commit", {}).get("sha", "")
else:
warnings.warn(
f"Failed to get merge base for {base}...{head}: Empty response"
)
except Exception as error:
warnings.warn(f"Failed to get merge base for {base}...{head}: {error}")
return merge_base
def gh_update_pr_state(org: str, repo: str, pr_num: int, state: str = "open") -> None:
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/pulls/{pr_num}"
try:
gh_fetch_url(url, method="PATCH", data={"state": state})
except HTTPError as err:
# When trying to open the pull request, error 422 means that the branch
# has been deleted and the API couldn't re-open it
if err.code == 422 and state == "open":
warnings.warn(
f"Failed to open {pr_num} because its head branch has been deleted: {err}"
)
else:
raise
def gh_query_issues_by_labels(
org: str, repo: str, labels: list[str], state: str = "open"
) -> list[dict[str, Any]]:
url = f"{GITHUB_API_URL}/repos/{org}/{repo}/issues"
return gh_fetch_json(
url, method="GET", params={"labels": ",".join(labels), "state": state}
)