Skip to content

Commit 0c8dbfd

Browse files
committed
Refactor: DRY save_json
1 parent 623f717 commit 0c8dbfd

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

needs_backport.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
# /// script
13-
# requires-python = ">=3.9"
13+
# requires-python = ">=3.10"
1414
# dependencies = [
1515
# "ghapi",
1616
# "rich",
@@ -26,10 +26,11 @@
2626
from functools import cache
2727
from typing import Any, TypeAlias
2828

29+
from fastcore.xtras import obj2dict
2930
from ghapi.all import GhApi, paged # pip install ghapi
3031
from rich import print # pip install rich
3132

32-
from potential_closeable_issues import sort_by_to_sort_and_direction
33+
from potential_closeable_issues import save_json, sort_by_to_sort_and_direction
3334

3435
PR: TypeAlias = dict[str, Any]
3536

@@ -131,7 +132,11 @@ def main() -> None:
131132
"-s", "--start", default=1, type=int, help="start at this PR number"
132133
)
133134
parser.add_argument(
134-
"-n", "--number", default=100, type=int, help="number of PRs to check"
135+
"-n",
136+
"--number",
137+
default=100,
138+
type=int,
139+
help="number of PRs to check per branch",
135140
)
136141
parser.add_argument(
137142
"--sort",
@@ -146,6 +151,7 @@ def main() -> None:
146151
),
147152
help="Sort by",
148153
)
154+
parser.add_argument("-j", "--json", action="store_true", help="output to JSON file")
149155
parser.add_argument(
150156
"-o", "--open-prs", action="store_true", help="open PRs in browser"
151157
)
@@ -190,6 +196,17 @@ def report(prs: list[PR]):
190196
total = len({pr["number"] for pr in prs})
191197
print(f"* Found {total} {reason}")
192198

199+
if args.json:
200+
data = {
201+
"reasons": [
202+
{reason: [obj2dict(pr) for pr in prs]}
203+
for reason, prs in total_candidates.items()
204+
],
205+
}
206+
# Use same name as this .py but with .json
207+
filename = os.path.splitext(__file__)[0] + ".json"
208+
save_json(data, filename)
209+
193210

194211
if __name__ == "__main__":
195212
main()

orphaned_backports.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from __future__ import annotations
1717

1818
import argparse
19-
import datetime as dt
20-
import json
2119
import os
2220
import urllib
2321
from typing import Any, TypeAlias
@@ -27,7 +25,7 @@
2725
from ghapi.all import GhApi, paged # pip install ghapi
2826
from rich import print # pip install rich
2927

30-
from potential_closeable_issues import sort_by_to_sort_and_direction
28+
from potential_closeable_issues import save_json, sort_by_to_sort_and_direction
3129

3230
PR: TypeAlias = dict[str, Any]
3331

@@ -164,19 +162,10 @@ def main() -> None:
164162
os.system(cmd)
165163

166164
if args.json:
165+
data = {"candidates": [obj2dict(c) for c in candidates]}
167166
# Use same name as this .py but with .json
168167
filename = os.path.splitext(__file__)[0] + ".json"
169-
now = dt.datetime.now(dt.UTC).isoformat()
170-
with open(filename, "w", encoding="utf-8") as f:
171-
json.dump(
172-
{
173-
"last_update": now,
174-
"candidates": [obj2dict(c) for c in candidates],
175-
},
176-
f,
177-
indent=2,
178-
)
179-
print(f"Saved candidates to {filename}")
168+
save_json(data, filename)
180169

181170

182171
if __name__ == "__main__":

potential_closeable_issues.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ def check_issues(
170170
return candidates
171171

172172

173+
def save_json(data: Any, filename: str) -> None:
174+
# Put last_update at the start
175+
data = {"last_update": dt.datetime.now(dt.UTC).isoformat(), **data}
176+
with open(filename, "w", encoding="utf-8") as f:
177+
json.dump(data, f, indent=2)
178+
print(f"Saved candidates to {filename}")
179+
180+
173181
def main() -> None:
174182
parser = argparse.ArgumentParser(
175183
description=__doc__,
@@ -219,19 +227,10 @@ def main() -> None:
219227
os.system(cmd)
220228

221229
if args.json:
230+
data = {"candidates": [obj2dict(c) for c in candidates]}
222231
# Use same name as this .py but with .json
223232
filename = os.path.splitext(__file__)[0] + ".json"
224-
now = dt.datetime.now(dt.UTC).isoformat()
225-
with open(filename, "w", encoding="utf-8") as f:
226-
json.dump(
227-
{
228-
"last_update": now,
229-
"candidates": [obj2dict(c) for c in candidates],
230-
},
231-
f,
232-
indent=2,
233-
)
234-
print(f"Saved candidates to {filename}")
233+
save_json(data, filename)
235234

236235

237236
if __name__ == "__main__":

0 commit comments

Comments
 (0)