| 274 | |
| 275 | |
| 276 | class PullRequestCache: |
| 277 | def __init__(self, path: Path): |
| 278 | self.path = path |
| 279 | self.path.parent.mkdir(parents=True, exist_ok=True) |
| 280 | self._data: dict[str, dict[str, object]] = {} |
| 281 | if self.path.exists(): |
| 282 | self._data = json.loads(self.path.read_text()) |
| 283 | |
| 284 | def get_many(self, numbers: Iterable[int]) -> dict[int, PullRequestRecord]: |
| 285 | records = {} |
| 286 | for number in numbers: |
| 287 | cached = self._data.get(str(number)) |
| 288 | if cached is not None: |
| 289 | cached = dict(cached) |
| 290 | cached.setdefault('title', f'PR #{number}') |
| 291 | records[number] = PullRequestRecord(**cached) |
| 292 | return records |
| 293 | |
| 294 | def update_many(self, records: Iterable[PullRequestRecord]) -> None: |
| 295 | changed = False |
| 296 | for record in records: |
| 297 | self._data[str(record.number)] = asdict(record) |
| 298 | changed = True |
| 299 | if changed: |
| 300 | self.path.write_text( |
| 301 | json.dumps(self._data, indent=2, sort_keys=True) + '\n' |
| 302 | ) |
| 303 | |
| 304 | |
| 305 | class GitHubClient: |