Repository navigation
Expand file tree
/
Copy pathpr_review_digest.py
More file actions
338 lines (322 loc) · 14.4 KB
/
Copy pathpr_review_digest.py
File metadata and controls
338 lines (322 loc) · 14.4 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
"""The review gate's digest, rendered off one payload, for the gate in `pr_review.py`.
This is a library module and has no command line. `pr_review.py` imports it as a sibling, and
`pr_review.digest` does the reads: the payload, the normalized checks, and the run log behind a
refusal.
`render` takes those results as arguments and makes no call. The header is an ordered list of
fields, so a new one is one entry. The fixed advisory lines are an ordered table, one row each.
"""
from __future__ import annotations
from datetime import datetime
import pr_review_answers as answers
import pr_review_copilot as adapter
from pr_review_check_lines import check_lines
from pr_review_checks import checks_stuck, checks_tally
from pr_review_payload import covering_bot
from pr_review_promotion import coverage
from pr_review_rounds import (
CODERABBIT_LOGIN,
KNOWN_REVIEWERS,
OTHER_REVIEWERS,
QODO_LOGIN,
REVIEWER,
answered_outside_review,
collected,
excluded_refusal,
first_comment,
head_nodes,
head_reviews,
is_open,
newest_of,
open_threads,
opened_by,
possible_quota,
qodo_comment,
qodo_comments_blind,
quota_refusal,
rate_limited_by,
reading,
refusing_review,
review_effort,
reviewer_nodes,
reviewer_requested,
shape_hold,
stopping_refusal,
thread_author,
threads_truncated,
uncounted_verdict,
window_blind,
)
from pr_review_summary import rabbit_on_head, summary_blind
NO_THREAD, NOTED = answers.NO_THREAD, answers.NOTED
BEHIND_THE_WINDOW = (
" BEHIND THE WINDOW ({value}): the newest {window} carry "
"none from the reviewer and older ones exist, so this cannot decide"
)
THREADS_TRUNCATED = (
" THREADS TRUNCATED: this pull request carries more review threads than the "
"100 read here, and the connection reads oldest-first, so the ones cut off are "
"the newest rather than the oldest, exactly the ones most likely to still be "
"open. `threads=` and `unresolved=` above undercount. Read the rest with "
"`reply`'s own paginated walk before trusting either number"
)
def digest_refusal(pr: dict) -> dict | None:
"""The refusal the digest reports: a stopping one, else one standing in for a missing head round."""
return stopping_refusal(pr) or (None if head_reviews(pr) else refusing_review(pr))
def run_log_reading(state: str, line: str) -> str:
"""The digest line naming the reading `transient_state` gave, empty where it gave none."""
readings = {
"owed": "transient. The run log names a fetch failure, not a rate limit, so one "
"re-request is owed",
"pending": "transient. The one re-request it is owed is pending",
"sent": "possible quota. The one re-request it was owed was sent and nothing answered it",
"spent": "possible quota. The run log names a fetch failure, but the round before was "
"an error refusal too",
}
return f" RUN LOG READING: {readings[state]}: {line}" if state else ""
def state_fields(pr: dict) -> list[tuple[str, object]]:
"""`state=` and when it changed, where the pull request is no longer open, else nothing."""
if is_open(pr):
return []
merged, commit = pr.get("mergedAt"), (pr.get("mergeCommit") or {}).get("oid")
stamp = ("merged_at", merged) if merged else ("closed_at", pr.get("closedAt"))
return [("state", pr["state"]), stamp, ("merge_commit", commit[:8] if commit else None)]
def header(fields: list[tuple[str, object]]) -> str:
"""Join each `key=value` pair by one space, leaving out a field whose value is None."""
return " ".join(f"{key}={value}" for key, value in fields if value is not None)
def render(
owner: str,
repo: str,
num: int,
pr: dict,
checks: list[dict],
now: datetime,
grace: float,
stall: float,
quota: str,
run_log: str,
seen: set[str] | None,
hub: str,
window: int,
fields_out: list[tuple[str, object]] | None = None,
) -> tuple[str, int]:
"""Render the digest and return it with the unresolved count.
`quota` is the run log's rate-limit sentence for the refusal `digest_refusal` names, read by the
caller. `run_log` is the caller's `RUN LOG READING` line for that refusal, empty for none.
Where `seen` is a set, only threads absent from it print, marked new, and are added.
Where `fields_out` is a list, the header's fields and the counts a gate reads are appended.
"""
head = pr["headRefOid"]
revs = reviewer_nodes(pr, "reviews")
on_head = head_reviews(pr)
newest = newest_of(on_head)
found = reading(newest, pr=pr) if newest is not None else None
effort, effort_source = review_effort(pr)
cover, cover_why = coverage(pr)
hold = shape_hold(pr)
notes = answers.noted(pr)
acks = answers.acknowledged(pr)
threads = pr["reviewThreads"]["nodes"]
truncated = threads_truncated(pr)
unresolved = open_threads(threads)
by_login = {
login: sum(1 for t in unresolved if thread_author(t) == login) for login in KNOWN_REVIEWERS
}
by_login["other"] = sum(1 for t in unresolved if thread_author(t) not in KNOWN_REVIEWERS)
contributors = {login: n for login, n in by_login.items() if n}
breakdown = (
" (" + " ".join(f"{login}={n}" for login, n in contributors.items()) + ")"
if len(contributors) > 1 or "other" in contributors
else ""
)
missed, missed_field = collected(pr, REVIEWER, "previously_missed")
outside, outside_field = collected(pr, CODERABBIT_LOGIN, "previously_missed")
listed = len(opened_by(pr, newest)) if newest is not None else 0
flagged = uncounted_verdict(pr)
advisory = uncounted_verdict(pr, "clean")
answer = answered_outside_review(pr)
refusal = digest_refusal(pr)
refusal_field = (
"no"
if not refusal
else "QUOTA"
if quota_refusal(refusal) or quota
else "ERROR"
if possible_quota(refusal)
else "EXCLUDED"
if excluded_refusal(refusal)
else "YES"
)
blind = "comments" if window_blind(pr, "comments") else ""
answered = "yes" if answer else ("unknown" if blind else "no")
ok, total = checks_tally(checks)
stuck = checks_stuck(checks, now, grace, stall)
other_on_head = [
login
for login in OTHER_REVIEWERS
if (
rabbit_on_head(pr)
if login == CODERABBIT_LOGIN
else head_nodes(pr, reviewer_nodes(pr, "reviews", login))
)
]
covered = covering_bot(pr, "coderabbit") is not None
unread = covered and summary_blind(pr) and CODERABBIT_LOGIN not in other_on_head
other_limited = {
login: name for login in OTHER_REVIEWERS if (name := rate_limited_by(pr, login)) is not None
}
qodo = qodo_comment(pr)
qodo_open = [s for s in reading(qodo, QODO_LOGIN).sections if s.kind == "open"] if qodo else []
shown = [t for t in unresolved if seen is None or t["id"] not in seen]
if seen is not None:
seen.update(t["id"] for t in shown)
plus = "+" if truncated else ""
# The repository leads the line, since a digest of the wrong pull request is well-formed.
fields: list[tuple[str, object]] = [
("repo", f"{owner}/{repo}"),
("pr", num),
*state_fields(pr),
("head", head[:8]),
("rounds", len(revs)),
("review_on_head", "yes" if on_head else "local" if cover == "local" else "NO"),
("other_reviewed", ",".join(other_on_head) or None),
("other_unknown", CODERABBIT_LOGIN if unread else None),
(
"other_rate_limited",
",".join(f"{login}:{name}" if name else login for login, name in other_limited.items())
or None,
),
("effort", effort),
("effort_source", effort_source),
("coverage", cover.upper() if cover in ("partial", "unknown") else cover),
(
"shapes",
("ACKNOWLEDGED" if hold[2] else "HELD") if hold else "NOTED" if notes else "ok",
),
("refusal", refusal_field),
("threads", f"{len(threads)}{plus}"),
("unresolved", f"{len(unresolved)}{plus}{breakdown}"),
("suppressed", collected(pr, REVIEWER, "suppressed")[1]),
("previously_missed", missed_field if missed else None),
(
"overview",
f"{'?' if found.open is None else found.open}/{listed}"
if found is not None and found.marked
else None,
),
("cr_outside_diff", outside_field if outside else None),
("qodo_open", len(qodo_open) if qodo else "unknown" if qodo_comments_blind(pr) else None),
("answered_outside_review", answered),
("requested", "yes" if reviewer_requested(pr) else "no"),
("merge", str(pr.get("mergeStateStatus"))),
("checks", f"{ok}/{total}"),
("stuck", ",".join(sorted({s for _, s in stuck})) or None),
("new", len(shown) if seen is not None else None),
]
owed, marked = answers.owed(pr)
keyed = {it.kind: answers.mark(it, acks) for it in answers.items(pr)}
if fields_out is not None:
fields_out += [*fields, (NO_THREAD, owed), (NOTED, len(notes))]
lines = [header(fields)]
if refusal:
lines.append(
" COPILOT REFUSED THIS ROUND: the newest Copilot review on this pull request says "
"it did not review, so it covers nothing. The body below says which remedy applies, "
"and any run log line under it says whether a re-request is owed"
)
lines += [
f" {ln.rstrip()}" for ln in (refusal.get("body") or "").splitlines() if ln.strip()
]
if quota:
lines.append(f" RATE LIMIT FROM THE REVIEWER'S RUN LOG: {quota}")
if run_log:
lines.append(run_log)
if notes:
lines.append(
f" {NOTED} ({len(notes)}): the reader placed part of what the reviewer "
"sent nowhere, so read the body itself before closing the review loop. A spelling "
f"to add belongs in `scripts/reviewers/` on {hub}, which hosts this script"
)
lines += [
f" {note.text}" + (f" ({m.strip()})" if (m := answers.mark(note, acks)) else "")
for note in notes
]
if hold:
lines.append(
f" UNPLACED ON THE HEAD (fingerprint={hold[0]}): a round covering the head carries "
+ (
"parts an owner, member, or collaborator acknowledged with written outcomes"
if hold[2]
else "text the reader cannot place, where a finding can sit. Read each part whole "
"in the review body, the excerpt only locating it, and give each item an outcome, "
"fixed with its SHA, declined with evidence, or listed as pre-existing in the PR "
f"body. Post them with `comment {num} --repo {owner}/{repo} --fingerprint "
f"{hold[0]} --body <outcomes>`"
)
)
for part in hold[1]:
lines += [f" {part.note}", *([] if hold[2] else adapter.excerpt(part.text))]
if owed:
lines.append(
f" {NO_THREAD} ({owed}){keyed.get(NO_THREAD, '')}: the round covering the head names more findings "
"than its open entries link to threads on this pull request, or lists one no thread "
f"sits on, so {owed} {'is' if owed == 1 else 'are'} raised where polling "
"threads cannot see them. Read the review body and give each the triage a suppressed "
"finding gets. A thread past the hundred read here counts too, and `threads=` "
"carries a trailing `+` where that page was cut"
+ (". Each findings list item prints below" if marked else "")
)
lines += marked
if flagged and found is not None and found.listed:
lines.append(
f" VERDICT WITH NO COUNTED FINDING{keyed.get(answers.VERDICT, '')}: the round covering the head opens on "
f"`{flagged[0]}` and states no open finding, so its headline and its findings list "
"or file table are where what it flags is named. Give both the triage a suppressed "
"finding gets"
)
elif flagged:
lines.append(
f" VERDICT WITH NO COUNTED FINDING{keyed.get(answers.VERDICT, '')}: the round covering the head opens on "
f"`{flagged[0]}` and counts no finding, so its headline is the one place what it "
"flags is named. Give the headline the triage a suppressed finding gets"
)
if flagged:
lines.append(f" {flagged[1] or '(the headline carries no prose)'}")
if advisory and advisory[1]:
lines.append(
f" CLEAN VERDICT HEADLINE: the round covering the head opens on `{advisory[0]}` and "
"counts no finding. Its headline may name a remaining nit, new or already answered, "
"which nothing here tells apart, so this is advisory and changes no exit code"
)
lines.append(f" {advisory[1]}")
if cover in ("table", "carried:table", "local", "dependabot", "partial", "unknown"):
lines.append(f" COVERAGE IS {cover.upper()}: {cover_why}")
elif cover == "none" and on_head:
lines.append(
" NO COVERAGE IS SHOWN: no round covering the head states coverage, and "
f"{cover_why or 'nothing stands in'}. Merge Gate item 2 reads this field"
)
lines.extend(check_lines(pr, checks, stuck, now))
for value, template in (
(blind, BEHIND_THE_WINDOW),
(truncated, THREADS_TRUNCATED),
):
if value:
lines.append(template.format(value=value, window=window))
if answer:
# Printed whole, since its wording is the only thing separating a refusal from a remark.
lines.append(
f" COPILOT COMMENT ({answer.get('createdAt')}, newer than any review): "
"the reviewer answered without reviewing, so read the body below and "
"decide, since a refusal is terminal and a remark is not"
)
lines += [
f" {ln.rstrip()}" for ln in (answer.get("body") or "").splitlines() if ln.strip()
]
mark = "NEW " if seen is not None else ""
for t in shown:
c = first_comment(t)
body = " ".join((c.get("body") or "").split())
lines.append(f" {mark}{t['id']} {c.get('path')}:{c.get('line')} {body[:160]}")
# Printed whole, since a finding with no thread appears nowhere but here.
lines += answers.unthreaded_lines(pr, owner, repo, num)
return "\n".join(lines), len(unresolved)