forked from EndogenAI/dogma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_github_read.py
More file actions
358 lines (296 loc) · 12.2 KB
/
Copy pathbulk_github_read.py
File metadata and controls
358 lines (296 loc) · 12.2 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
"""
scripts/bulk_github_read.py — Batch GitHub issue/PR metadata reads.
Purpose
-------
Fetch structured metadata for one or more GitHub issues and/or pull requests,
then format the results as a table, JSON, or CSV. All fetches route through
the ``gh`` CLI via subprocess with list-of-args (never shell=True). Supports
reading individual items by number, or running a GitHub search query.
Inputs
------
- ``--issues NUMBERS`` Comma-separated issue numbers to fetch (e.g. 1,2,3).
- ``--prs NUMBERS`` Comma-separated PR numbers to fetch.
- ``--query QUERY`` GitHub search string (passed to ``gh issue list --search``).
At least one of the above is required.
Outputs
-------
- stdout: formatted results (table by default; JSON or CSV via ``--format``).
- stderr: error messages on fetch failure.
Flags
-----
--issues NUMBERS Comma-separated issue numbers.
--prs NUMBERS Comma-separated PR numbers.
--query QUERY GitHub search string for issue list.
--fields FIELDS Comma-separated field names to include in output.
Default: number,title,state,labels,milestone,assignee
--format FORMAT Output format: table | json | csv. Default: table.
--help Show this help and exit.
Exit codes
----------
0 All fetches succeeded.
1 One or more fetches failed (gh error, network issue, etc.).
Usage examples
--------------
# Fetch issues 1, 5, and 10 as a table
uv run python scripts/bulk_github_read.py --issues 1,5,10
# Fetch PR 42 as JSON
uv run python scripts/bulk_github_read.py --prs 42 --format json
# Search for open bugs and export as CSV
uv run python scripts/bulk_github_read.py --query "is:open label:type:bug" --format csv
# Fetch specific fields only
uv run python scripts/bulk_github_read.py --issues 1,2 --fields number,title,state
# Mix issues and PRs
uv run python scripts/bulk_github_read.py --issues 10,11 --prs 5 --format json
"""
from __future__ import annotations
import argparse
import csv
import io
import json
import subprocess
import sys
from typing import Any
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
DEFAULT_FIELDS = ["number", "title", "state", "labels", "milestone", "assignee"]
# All fields we ever request from gh JSON output (superset of DEFAULT_FIELDS)
_GH_JSON_FIELDS = "number,title,state,labels,milestone,assignee"
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Batch GitHub issue/PR metadata reads — outputs table, JSON, or CSV.",
)
parser.add_argument(
"--issues",
default=None,
metavar="NUMBERS",
help="Comma-separated issue numbers to fetch (e.g. 1,2,3).",
)
parser.add_argument(
"--prs",
default=None,
metavar="NUMBERS",
help="Comma-separated PR numbers to fetch.",
)
parser.add_argument(
"--query",
default=None,
metavar="QUERY",
help="GitHub search string for issue list (passed to gh issue list --search).",
)
parser.add_argument(
"--fields",
default=",".join(DEFAULT_FIELDS),
metavar="FIELDS",
help=(f"Comma-separated field names to include. Default: {','.join(DEFAULT_FIELDS)}"),
)
parser.add_argument(
"--format",
default="table",
choices=["table", "json", "csv"],
help="Output format: table | json | csv. Default: table.",
)
return parser
# ---------------------------------------------------------------------------
# Field helpers
# ---------------------------------------------------------------------------
def _parse_fields(fields_str: str) -> list[str]:
"""Split and clean a comma-separated fields string."""
return [f.strip() for f in fields_str.split(",") if f.strip()]
def _filter_record(data: dict[str, Any], fields: list[str]) -> dict[str, Any]:
"""Return a new dict containing only the requested fields."""
out: dict[str, Any] = {}
for field in fields:
if field in data:
out[field] = data[field]
else:
out[field] = None
return out
def _simplify_record(record: dict[str, Any]) -> dict[str, Any]:
"""Flatten nested gh JSON structures for readable display/export.
Only transforms keys that are already present in the record — never adds
new keys. Transformations applied:
- labels: list[{name, ...}] → comma-joined string of names
- milestone: {title, ...} | null → title string or ""
- assignee: {login, ...} | null → login string or ""
- assignees: list[{login, ...}] → comma-joined logins (gh pr view uses 'assignees')
"""
simplified = dict(record)
# labels — only if key is present
if "labels" in simplified and isinstance(simplified["labels"], list):
simplified["labels"] = ", ".join(
(lbl["name"] if isinstance(lbl, dict) else str(lbl)) for lbl in simplified["labels"]
)
# milestone — only if key is present
if "milestone" in simplified:
ms = simplified["milestone"]
if isinstance(ms, dict):
simplified["milestone"] = ms.get("title", "")
elif ms is None:
simplified["milestone"] = ""
# assignee (issues) — only if key is present
if "assignee" in simplified:
asn = simplified["assignee"]
if isinstance(asn, dict):
simplified["assignee"] = asn.get("login", "")
elif asn is None:
simplified["assignee"] = ""
# assignees (PRs) — only if key is present
if "assignees" in simplified and isinstance(simplified["assignees"], list):
simplified["assignees"] = ", ".join(
(a["login"] if isinstance(a, dict) else str(a)) for a in simplified["assignees"]
)
return simplified
# ---------------------------------------------------------------------------
# Fetch helpers
# ---------------------------------------------------------------------------
class FetchError(Exception):
"""Raised when a gh CLI call fails."""
def _fetch_issue(number: int, fields: list[str]) -> dict[str, Any]:
"""Fetch a single issue by number and return a filtered record dict."""
cmd = ["gh", "issue", "view", str(number), "--json", _GH_JSON_FIELDS]
result = subprocess.run(cmd, capture_output=True, text=True) # noqa: S603
if result.returncode != 0:
raise FetchError(f"Issue #{number}: {result.stderr.strip() or result.stdout.strip()}")
data = json.loads(result.stdout)
return _filter_record(data, fields)
def _fetch_pr(number: int, fields: list[str]) -> dict[str, Any]:
"""Fetch a single PR by number and return a filtered record dict."""
# gh pr view returns 'assignees' (plural), not 'assignee' — request it
pr_json_fields = "number,title,state,labels,milestone,assignees"
cmd = ["gh", "pr", "view", str(number), "--json", pr_json_fields]
result = subprocess.run(cmd, capture_output=True, text=True) # noqa: S603
if result.returncode != 0:
raise FetchError(f"PR #{number}: {result.stderr.strip() or result.stdout.strip()}")
data = json.loads(result.stdout)
# gh pr view returns 'assignees' (plural list). Normalise so downstream
# field filtering is uniform: preserve the full list as 'assignees' AND
# add 'assignee' (first item) as a convenience field for single-assignee use.
if "assignees" in data and "assignee" not in data:
assignees = data["assignees"]
data["assignee"] = assignees[0] if assignees else None
return _filter_record(data, fields)
def _search_issues(query: str, fields: list[str]) -> list[dict[str, Any]]:
"""Run a GitHub search query and return a list of filtered record dicts."""
cmd = [
"gh",
"issue",
"list",
"--search",
query,
"--json",
_GH_JSON_FIELDS,
"--limit",
"100",
]
result = subprocess.run(cmd, capture_output=True, text=True) # noqa: S603
if result.returncode != 0:
raise FetchError(f"Search query '{query}': {result.stderr.strip() or result.stdout.strip()}")
items = json.loads(result.stdout)
return [_filter_record(item, fields) for item in items]
# ---------------------------------------------------------------------------
# Formatting
# ---------------------------------------------------------------------------
def _format_json(records: list[dict[str, Any]]) -> str:
simplified = [_simplify_record(r) for r in records]
return json.dumps(simplified, indent=2)
def _format_csv(records: list[dict[str, Any]], fields: list[str]) -> str:
simplified = [_simplify_record(r) for r in records]
buf = io.StringIO()
writer = csv.DictWriter(buf, fieldnames=fields, extrasaction="ignore", lineterminator="\n")
writer.writeheader()
writer.writerows(simplified)
return buf.getvalue()
def _format_table(records: list[dict[str, Any]], fields: list[str]) -> str:
simplified = [_simplify_record(r) for r in records]
if not simplified:
return "(no results)"
# Compute column widths
widths: dict[str, int] = {f: len(f) for f in fields}
for rec in simplified:
for f in fields:
val = str(rec.get(f, "") or "")
# Truncate long values for table display
if len(val) > 60:
val = val[:57] + "..."
widths[f] = max(widths[f], len(val))
sep = " "
header = sep.join(f.upper().ljust(widths[f]) for f in fields)
divider = sep.join("-" * widths[f] for f in fields)
rows = []
for rec in simplified:
row_parts = []
for f in fields:
val = str(rec.get(f, "") or "")
if len(val) > 60:
val = val[:57] + "..."
row_parts.append(val.ljust(widths[f]))
rows.append(sep.join(row_parts))
lines = [header, divider] + rows
return "\n".join(lines)
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def main(argv: list[str] | None = None) -> int:
parser = _build_parser()
args = parser.parse_args(argv)
if not args.issues and not args.prs and not args.query:
parser.error("At least one of --issues, --prs, or --query is required.")
fields = _parse_fields(args.fields)
records: list[dict[str, Any]] = []
any_failed = False
# --- Fetch issues by number ---
if args.issues:
for part in args.issues.split(","):
part = part.strip()
if not part:
continue
try:
number = int(part)
except ValueError:
print(f"ERROR: Invalid issue number: '{part}'", file=sys.stderr)
any_failed = True
continue
try:
records.append(_fetch_issue(number, fields))
except FetchError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
any_failed = True
# --- Fetch PRs by number ---
if args.prs:
for part in args.prs.split(","):
part = part.strip()
if not part:
continue
try:
number = int(part)
except ValueError:
print(f"ERROR: Invalid PR number: '{part}'", file=sys.stderr)
any_failed = True
continue
try:
records.append(_fetch_pr(number, fields))
except FetchError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
any_failed = True
# --- Search ---
if args.query:
try:
records.extend(_search_issues(args.query, fields))
except FetchError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
any_failed = True
# --- Format & output ---
fmt = args.format
if fmt == "json":
print(_format_json(records))
elif fmt == "csv":
print(_format_csv(records, fields), end="")
else:
print(_format_table(records, fields))
return 1 if any_failed else 0
if __name__ == "__main__":
sys.exit(main())