Repository navigation
Expand file tree
/
Copy pathdetail.py
More file actions
383 lines (325 loc) · 16.1 KB
/
Copy pathdetail.py
File metadata and controls
383 lines (325 loc) · 16.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""Detail-drawer data — host-free async functions behind the gated data routes.
The in-console PR/issue drawer (epic bd-nm78) needs the FULL object for one PR or
issue, not the lean board row ``api.fetch_prs`` returns. These functions are the data
foundation the drawer cards build on (no UI, no write routes).
Same contract as ``api.fetch_prs``: every `gh` call goes through ``run_gh``, the repo
is validated with ``bad_repo``, failures are classified with ``check_gh_error``, the
body is parsed with ``parse_json``, and each function returns a ``dict`` (the parsed
object) or ``{"error": "..."}`` — never raises. ``number`` must be a positive int;
anything else is an ``{"error"}`` returned WITHOUT calling `gh`.
A small in-process TTL cache (``cached``) memoises a detail read for a few seconds, so
the drawer's open + its immediate refreshes don't each fork `gh`. Error results are
never cached, and a caller passes ``fresh=True`` to bypass a live entry.
"""
from __future__ import annotations
import json
import re
import time
from collections.abc import Awaitable, Callable
from .gh_cli import bad_repo, check_gh_error, dicts, parse_json, run_gh
# The JSON fields the drawer needs for one PR — the full merge-readiness picture plus
# the metadata the UI renders (labels / assignees / milestone / review requests /
# linked issues). All verified against `gh pr view --json` on this gh (see the PR body).
_PR_DETAIL_FIELDS = (
"number,title,state,isDraft,author,body,url,createdAt,updatedAt,"
"headRefName,baseRefName,labels,assignees,milestone,reviewRequests,"
"reviewDecision,mergeable,mergeStateStatus,latestReviews,additions,"
"deletions,changedFiles,files,closingIssuesReferences"
)
# A PR body can be large; cap what we hand the drawer (and flag the cut) so one giant
# description can't blow the response budget. Matches read_tools._MAX_BODY_CHARS.
_MAX_BODY_CHARS = 60000
_MAX_COMMENT_CHARS = 20000
# Verified with gh 2.92.0's `issue view 1 --json invalidField` field list.
_ISSUE_DETAIL_FIELDS = (
"number,title,state,stateReason,author,body,url,createdAt,updatedAt,"
"labels,assignees,milestone,comments,closedByPullRequestsReferences"
)
# Fields verified against https://docs.github.com/en/graphql/reference/pulls
# and /issues (IssueComment). Keep identifiers in variables, never in query text.
_PR_CONVERSATION_QUERY = (
"query($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){"
"pullRequest(number:$number){"
"comments(first:100){nodes{author{login} body createdAt url}} "
"reviews(first:50){nodes{author{login} state body submittedAt}} "
"reviewThreads(first:100){nodes{id isResolved isOutdated path line "
"comments(first:50){nodes{author{login} body createdAt url diffHunk}}}}}}}"
)
_DEFAULT_TTL = 15 # seconds — a drawer open + its reflexive refreshes share one `gh` call.
# ── the TTL cache ──────────────────────────────────────────────────────────────────
# key (e.g. ("pr", repo, number)) -> (expiry_monotonic, value). Process-wide and tiny;
# detail reads are bursty (open the drawer, refresh it) and cheap to recompute on miss.
_CACHE: dict[tuple, tuple[float, dict]] = {}
def _now() -> float:
"""Monotonic clock — never goes backwards on a wall-clock adjustment (patched in tests)."""
return time.monotonic()
def clear_cache() -> None:
"""Drop every cached entry (tests; a config change that should invalidate reads)."""
_CACHE.clear()
async def cached(
key: tuple,
coro_fn: Callable[[], Awaitable[dict]],
ttl: int = _DEFAULT_TTL,
*,
fresh: bool = False,
) -> dict:
"""Memoise ``coro_fn()``'s dict result under ``key`` for ``ttl`` seconds.
- a live entry is returned without awaiting ``coro_fn`` (ONE `gh` call per window);
- an ERROR result (a dict carrying ``"error"``) is never cached — a transient
failure must not pin a drawer to a stale error for the whole TTL;
- ``fresh=True`` bypasses a live entry (and refreshes it from ``coro_fn``).
"""
if not fresh:
hit = _CACHE.get(key)
if hit is not None:
expiry, value = hit
if expiry > _now():
return value
del _CACHE[key] # expired — fall through and recompute
value = await coro_fn()
if isinstance(value, dict) and "error" not in value:
_CACHE[key] = (_now() + ttl, value)
return value
# ── PR detail ───────────────────────────────────────────────────────────────────────
def _positive_int(value) -> bool:
"""True only for a real positive int — ``bool`` (an int subclass) and 0/negatives are out."""
return isinstance(value, int) and not isinstance(value, bool) and value > 0
async def fetch_pr_detail(repo: str, number: int, fresh: bool = False) -> dict:
"""The full detail object for ONE pull request as a dict (or ``{"error": "..."}``).
One ``gh pr view N --repo o/n --json <fields>`` call; the parsed dict carries the
merge-readiness picture (``mergeable`` / ``mergeStateStatus`` / ``reviewDecision`` /
``latestReviews``) and the metadata the drawer renders (labels / assignees /
milestone / review requests / linked issues / files). The body is capped at
60000 chars with ``body_truncated: true`` set when it's cut.
Validated and bounded like ``api.fetch_prs``: a bad repo or a non-positive-int
``number`` is an ``{"error"}`` returned WITHOUT calling `gh`. Memoised through
``cached`` (``("pr", repo, number)``); ``fresh=True`` bypasses the cache.
"""
if err := bad_repo(repo):
return {"error": err}
if not _positive_int(number):
return {"error": f"Error: PR number must be a positive integer (got {number!r})."}
async def _run() -> dict:
rc, out, serr = await run_gh(["pr", "view", str(number), "--repo", repo, "--json", _PR_DETAIL_FIELDS])
if gh_err := check_gh_error(rc, serr, repo=repo):
return {"error": gh_err}
d, perr = parse_json(out, dict) # a non-dict body is an error, never a crash
if perr:
return {"error": perr}
body = d.get("body")
if isinstance(body, str) and len(body) > _MAX_BODY_CHARS:
d["body"] = body[:_MAX_BODY_CHARS]
d["body_truncated"] = True
return d
return await cached(("pr", repo, number), _run, fresh=fresh)
def _cap_body(item: dict, limit: int, flag: str = "truncated") -> dict:
body = item.get("body")
item[flag] = isinstance(body, str) and len(body) > limit
if item[flag]:
item["body"] = body[:limit]
return item
def _nodes(connection) -> list[dict]:
return dicts(connection.get("nodes")) if isinstance(connection, dict) else []
async def fetch_pr_conversation(repo: str, number: int, fresh: bool = False) -> dict:
"""Comments, reviews and threads in one query, retaining every returned reply.
Counts describe the returned window: first 100 comments / 50 reviews / 100
threads, each with up to 50 comments. Bodies are capped at 20000 characters.
"""
if err := bad_repo(repo):
return {"error": err}
if not _positive_int(number):
return {"error": "Error: PR number must be a positive integer."}
async def _run() -> dict:
owner, name = repo.split("/", 1)
rc, out, serr = await run_gh(
[
"api",
"graphql",
"-f",
f"query={_PR_CONVERSATION_QUERY}",
"-f",
f"owner={owner}",
"-f",
f"name={name}",
"-F",
f"number={number}",
]
)
if err := check_gh_error(rc, serr, repo=repo):
return {"error": err}
data, err = parse_json(out, dict)
if err:
return {"error": err}
if data.get("errors"):
messages = "; ".join(str(e.get("message", e)) for e in dicts(data["errors"]))
return {"error": check_gh_error(1, messages or str(data["errors"]), repo=repo)}
pr = data
for key in ("data", "repository", "pullRequest"):
pr = pr.get(key) if isinstance(pr, dict) else None
if not isinstance(pr, dict):
return {"error": check_gh_error(1, "HTTP 404: pullRequest missing or inaccessible", repo=repo)}
comments = [_cap_body(c, _MAX_COMMENT_CHARS) for c in _nodes(pr.get("comments"))]
reviews = [_cap_body(r, _MAX_COMMENT_CHARS) for r in _nodes(pr.get("reviews"))]
threads = _nodes(pr.get("reviewThreads"))
for thread in threads:
thread["comments"] = [_cap_body(c, _MAX_COMMENT_CHARS) for c in _nodes(thread.get("comments"))]
return {
"comments": comments,
"reviews": reviews,
"threads": threads,
"counts": {
"comments": len(comments),
"threads": len(threads),
"unresolved": sum(not t.get("isResolved") for t in threads),
},
}
return await cached(("pr_conversation", repo, number), _run, fresh=fresh)
async def fetch_issue_detail(repo: str, number: int, fresh: bool = False) -> dict:
"""One issue with metadata, closing PRs and bounded description / comments."""
if err := bad_repo(repo):
return {"error": err}
if not _positive_int(number):
return {"error": "Error: Issue number must be a positive integer."}
async def _run() -> dict:
rc, out, serr = await run_gh(["issue", "view", str(number), "--repo", repo, "--json", _ISSUE_DETAIL_FIELDS])
if err := check_gh_error(rc, serr, repo=repo):
return {"error": err}
data, err = parse_json(out, dict)
if err:
return {"error": err}
_cap_body(data, _MAX_BODY_CHARS, "body_truncated")
data["comments"] = [
_cap_body({key: c.get(key) for key in ("author", "body", "createdAt", "url")}, _MAX_COMMENT_CHARS)
for c in dicts(data.get("comments"))
]
return data
return await cached(("issue", repo, number), _run, fresh=fresh)
async def fetch_pr_checks(repo: str, number: int, fresh: bool = False, *, run_id: int | None = None) -> dict:
"""Normalised rollup, or the last 200 failed-step log lines for an Actions run."""
from .read_tools import classify_check, fetch_failure_log, is_status_context
if err := bad_repo(repo):
return {"error": err}
if not _positive_int(number):
return {"error": "Error: PR number must be a positive integer."}
if run_id is not None and not _positive_int(run_id):
return {"error": "Error: run_id must be a positive integer."}
async def _run():
if run_id is not None:
result = await fetch_failure_log(repo, run_id)
if "error" in result:
return result
return {"log_tail": "\n".join(result["log"].splitlines()[-200:])}
rc, out, serr = await run_gh(["pr", "view", str(number), "--repo", repo, "--json", "statusCheckRollup"])
if err := check_gh_error(rc, serr, repo=repo):
return {"error": err}
data, err = parse_json(out, dict)
if err:
return {"error": err}
checks = []
summary = dict.fromkeys(("pass", "fail", "pending", "skipped"), 0)
for c in dicts(data.get("statusCheckRollup")):
category = classify_check(c)
summary[category] += 1
context = is_status_context(c)
url = c.get("targetUrl" if context else "detailsUrl") or None
match = re.search(r"/actions/runs/([1-9][0-9]*)(?:[/?#]|$)", url) if isinstance(url, str) else None
checks.append(
{
"name": c.get("context" if context else "name") or "?",
"workflow": c.get("workflowName") or None,
"status": ("PENDING" if category == "pending" else "COMPLETED") if context else c.get("status"),
"conclusion": (None if category == "pending" else c.get("state"))
if context
else c.get("conclusion"),
"url": url,
"started_at": c.get("createdAt" if context else "startedAt"),
"completed_at": None if context else c.get("completedAt"),
"run_id": int(match[1]) if match and len(match[1]) < 100 else None,
}
)
return {"checks": checks, "summary": summary}
return await cached(("pr_checks", repo, number, run_id), _run, fresh=fresh)
def _diff_path(value: str) -> str:
"""Decode Git's quoted paths (including UTF-8 octal escapes)."""
if value.startswith('"') and value.endswith('"'):
import ast
try:
return ast.literal_eval("b" + value).decode("utf-8", errors="replace")
except (ValueError, SyntaxError):
return value[1:-1]
return value
def _split_diff(diff: str) -> dict:
files = []
for block in re.split(r"(?m)^(?=diff --git )", diff):
if not block.startswith("diff --git "):
continue
lines = block.splitlines()
# Git leaves spaces unquoted; split at the b/ prefix, not whitespace.
match = re.match(r'diff --git ("a/.*?"|a/.*?) ("b/.*"|b/.*)$', lines[0])
if not match:
return {"error": "Error: could not parse diff file header."}
old, path = (_diff_path(v)[2:] for v in match.groups())
status = "modified"
additions = deletions = 0
in_hunk = False
binary = False
for line in lines[1:]:
if line.startswith("@@ "):
in_hunk = True
elif in_hunk:
additions += line.startswith("+")
deletions += line.startswith("-")
elif line.startswith("new file mode "):
status = "added"
elif line.startswith("deleted file mode "):
status = "deleted"
elif line.startswith("rename from "):
status, old = "renamed", _diff_path(line[len("rename from ") :])
elif line.startswith("rename to "):
path = _diff_path(line[len("rename to ") :])
elif line.startswith(("Binary files ", "GIT binary patch")):
binary = True
files.append(
{
"path": path,
"old_path": old if status == "renamed" else None,
"status": status,
"additions": additions,
"deletions": deletions,
"patch": None,
"truncated": not binary and len(block) > 40000,
"binary": binary,
"_patch": None if binary else block[:40000],
}
)
patches = [f.pop("_patch") for f in files]
result = {"files": files, "total_truncated": False}
# Include JSON escaping and metadata in the total response budget.
remaining = 400000 - len(json.dumps(result))
if remaining < 0:
return {"error": "Error: diff file metadata exceeds the 400000-character response limit."}
exhausted = False
for f, patch in zip(files, patches, strict=True):
if patch is None:
continue
cost = len(json.dumps(patch)) - len("null")
if exhausted or cost > remaining:
exhausted = True
f["truncated"] = True
result["total_truncated"] = True
else:
f["patch"] = patch
remaining -= cost
return result
async def fetch_pr_diff(repo: str, number: int, fresh: bool = False) -> dict:
"""A bounded per-file unified diff, cached alongside other drawer reads."""
if err := bad_repo(repo):
return {"error": err}
if not _positive_int(number):
return {"error": "Error: PR number must be a positive integer."}
async def _run():
rc, out, serr = await run_gh(["pr", "diff", str(number), "--repo", repo])
if err := check_gh_error(rc, serr, repo=repo):
return {"error": err}
return _split_diff(out)
return await cached(("pr_diff", repo, number), _run, fresh=fresh)