-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch-pr-reviews.py
More file actions
executable file
·324 lines (262 loc) · 10.4 KB
/
Copy pathfetch-pr-reviews.py
File metadata and controls
executable file
·324 lines (262 loc) · 10.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
#!/Users/jpaddison/venvs/py3/bin/python3
"""Fetch all review data for a GitHub PR and output human-readable text.
Usage:
./fetch-pr-reviews.py https://github.com/owner/repo/pull/123
./fetch-pr-reviews.py owner/repo 123
./fetch-pr-reviews.py # auto-detect from current branch
"""
import json
import re
import subprocess
import sys
from datetime import datetime, timezone
# Bots whose issue comments are pure noise (deploy previews, coverage, CI status)
NOISE_BOTS = {"vercel", "netlify", "codecov", "github-actions"}
# Known review bots we track status for
KNOWN_REVIEW_BOTS = {"gemini", "copilot"}
def run_gh(args: list[str]) -> str:
"""Run a gh CLI command and return stdout."""
result = subprocess.run(
["gh"] + args,
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f"Error running gh {' '.join(args)}:", file=sys.stderr)
print(result.stderr, file=sys.stderr)
sys.exit(1)
return result.stdout
def parse_paginated_json(raw: str) -> list:
"""Parse gh --paginate output which concatenates JSON arrays.
gh --paginate outputs multiple JSON arrays concatenated, e.g.:
[{"a":1}][{"b":2}]
We need to split on ][ boundaries and parse each chunk.
"""
raw = raw.strip()
if not raw:
return []
# Try parsing as a single array first (common case: single page)
try:
return json.loads(raw)
except json.JSONDecodeError:
pass
# Split on ][ boundaries — find positions where ] is immediately followed by [
results = []
# Use a simple approach: replace ][ with ]\n[ then split
chunks = raw.replace("][", "]\n[").split("\n")
for chunk in chunks:
chunk = chunk.strip()
if chunk:
results.extend(json.loads(chunk))
return results
def parse_pr_args(args: list[str]) -> tuple[str, str, int]:
"""Parse command-line args into (owner, repo, pr_number).
Supports:
- https://github.com/owner/repo/pull/123
- owner/repo 123
- (no args) auto-detect from current branch
"""
if not args:
# Auto-detect from current branch
raw = run_gh(["pr", "view", "--json", "url", "--jq", ".url"])
url = raw.strip()
return parse_pr_url(url)
if len(args) == 1:
return parse_pr_url(args[0])
if len(args) == 2:
owner_repo, number = args
if "/" in owner_repo:
owner, repo = owner_repo.split("/", 1)
return owner, repo, int(number)
print(f"Usage: {sys.argv[0]} [PR_URL | owner/repo number]", file=sys.stderr)
sys.exit(1)
def parse_pr_url(url: str) -> tuple[str, str, int]:
"""Extract owner, repo, number from a GitHub PR URL."""
m = re.match(r"https?://github\.com/([^/]+)/([^/]+)/pull/(\d+)", url)
if not m:
print(f"Could not parse PR URL: {url}", file=sys.stderr)
sys.exit(1)
return m.group(1), m.group(2), int(m.group(3))
def fetch_endpoint(owner: str, repo: str, endpoint: str) -> list:
"""Fetch a paginated GitHub API endpoint."""
raw = run_gh([
"api", "--paginate",
f"repos/{owner}/{repo}/{endpoint}",
])
return parse_paginated_json(raw)
def classify_bot(login: str) -> str | None:
"""Return the known bot name if login matches, else None."""
login_lower = login.lower()
if "gemini" in login_lower or login_lower.startswith("google-labs"):
return "gemini"
if "copilot" in login_lower:
return "copilot"
return None
def is_noise_bot(login: str) -> bool:
"""Check if a login belongs to a noise bot we should filter out."""
login_lower = login.lower()
return any(bot in login_lower for bot in NOISE_BOTS)
def is_gemini_boilerplate_comment(body: str) -> bool:
"""Detect Gemini's summary-of-changes issue comment (not the actual review)."""
if not body:
return False
# Gemini posts a "Summary of Changes" or "Summary" issue comment
# that duplicates info from its actual review
first_lines = body[:300].lower()
return "summary of changes" in first_lines or (
first_lines.startswith("## summary") and "key changes" in first_lines
)
def compute_bot_status(reviews: list, review_comments: list, issue_comments: list) -> dict:
"""Determine which known bots have posted reviews."""
status = {}
for bot_name in KNOWN_REVIEW_BOTS:
status[bot_name] = {"status": "not_seen", "review_count": 0}
# Check reviews
for r in reviews:
login = r.get("user", {}).get("login", "")
bot = classify_bot(login)
if bot:
status[bot]["review_count"] += 1
status[bot]["status"] = "reviewed"
# Check review comments (inline)
for c in review_comments:
login = c.get("user", {}).get("login", "")
bot = classify_bot(login)
if bot and status[bot]["status"] == "not_seen":
status[bot]["status"] = "reviewed"
return status
def compute_readiness(bot_status: dict, age_minutes: float) -> dict:
"""Compute readiness heuristics for the AI to use."""
gemini_reviewed = bot_status.get("gemini", {}).get("status") == "reviewed"
copilot_reviewed = bot_status.get("copilot", {}).get("status") == "reviewed"
reasons = []
is_likely_complete = True
if gemini_reviewed:
reasons.append("gemini has reviewed")
elif age_minutes < 5:
reasons.append("gemini hasn't reviewed yet (PR is very new)")
is_likely_complete = False
else:
reasons.append("gemini hasn't reviewed (may not be enabled)")
if copilot_reviewed:
reasons.append("copilot has reviewed")
elif age_minutes < 10:
reasons.append("copilot hasn't reviewed yet (typically takes ~6 min)")
is_likely_complete = False
else:
reasons.append("copilot hasn't reviewed (may not have been triggered)")
return {
"is_likely_complete": is_likely_complete,
"reason": f"PR is {int(age_minutes)} min old; " + "; ".join(reasons),
}
def format_output(
pr_meta: dict,
age_minutes: float,
bot_status: dict,
readiness: dict,
reviews: list,
review_comments: list,
issue_comments: list,
) -> str:
"""Format all PR review data as human-readable text."""
lines = []
# Header
lines.append(f"# PR Review Data: {pr_meta['title']}")
lines.append(f"URL: {pr_meta['url']}")
lines.append(f"Created: {pr_meta['created_at']} ({int(age_minutes)} minutes ago)")
lines.append("")
# Readiness
lines.append(f"## Readiness")
complete_str = "YES" if readiness["is_likely_complete"] else "NO"
lines.append(f"Likely complete: {complete_str}")
lines.append(readiness["reason"])
lines.append("")
# Bot status
lines.append("## Bot Status")
for bot, info in bot_status.items():
if info["status"] == "reviewed":
lines.append(f"- {bot}: reviewed ({info['review_count']} review(s))")
else:
lines.append(f"- {bot}: not seen")
lines.append("")
# Counts
lines.append(f"## Counts")
lines.append(f"- Reviews: {len(reviews)}")
lines.append(f"- Inline review comments: {len(review_comments)}")
lines.append(f"- Discussion comments: {len(issue_comments)}")
lines.append("")
# Reviews
if reviews:
lines.append("## Reviews")
for r in reviews:
user = r.get("user", {}).get("login", "unknown")
state = r.get("state", "")
submitted = r.get("submitted_at", "")
body = (r.get("body") or "").strip()
lines.append(f"### {user} ({state}) — {submitted}")
if body:
lines.append(body)
else:
lines.append("(no body)")
lines.append("")
# Review comments grouped by file
if review_comments:
lines.append("## Inline Review Comments")
by_file: dict[str, list] = {}
for c in review_comments:
path = c.get("path", "(unknown file)")
by_file.setdefault(path, []).append(c)
for path, comments in by_file.items():
lines.append(f"### {path}")
for c in comments:
user = c.get("user", {}).get("login", "unknown")
line_num = c.get("line") or c.get("original_line") or "?"
body = (c.get("body") or "").strip()
reply_to = c.get("in_reply_to_id")
reply_str = f" (reply to #{reply_to})" if reply_to else ""
lines.append(f"**{user}** at line {line_num}{reply_str}:")
lines.append(body)
lines.append("")
# Issue comments
if issue_comments:
lines.append("## Discussion Comments")
for c in issue_comments:
user = c.get("user", {}).get("login", "unknown")
created = c.get("created_at", "")
body = (c.get("body") or "").strip()
lines.append(f"### {user} — {created}")
lines.append(body)
lines.append("")
if not reviews and not review_comments and not issue_comments:
lines.append("No reviews or comments found.")
return "\n".join(lines)
def main():
owner, repo, pr_number = parse_pr_args(sys.argv[1:])
# Fetch PR metadata
pr_raw = run_gh([
"api", f"repos/{owner}/{repo}/pulls/{pr_number}",
"--jq", '{url: .html_url, title: .title, created_at: .created_at}',
])
pr_meta = json.loads(pr_raw)
# Compute age
created = datetime.fromisoformat(pr_meta["created_at"].replace("Z", "+00:00"))
age_minutes = (datetime.now(timezone.utc) - created).total_seconds() / 60
# Fetch all three endpoints
reviews_raw = fetch_endpoint(owner, repo, f"pulls/{pr_number}/reviews")
review_comments_raw = fetch_endpoint(owner, repo, f"pulls/{pr_number}/comments")
issue_comments_raw = fetch_endpoint(owner, repo, f"issues/{pr_number}/comments")
# Filter issue comments: remove noise bots and Gemini boilerplate
issue_comments_filtered = [
c for c in issue_comments_raw
if not is_noise_bot(c.get("user", {}).get("login", ""))
and not is_gemini_boilerplate_comment(c.get("body", ""))
]
# Bot status and readiness
bot_status = compute_bot_status(reviews_raw, review_comments_raw, issue_comments_raw)
readiness = compute_readiness(bot_status, age_minutes)
print(format_output(
pr_meta, age_minutes, bot_status, readiness,
reviews_raw, review_comments_raw, issue_comments_filtered,
))
if __name__ == "__main__":
main()