|
| 1 | +"""Sync categories and labels to the issue template form |
| 2 | +
|
| 3 | +projects.yaml → .github/ISSUE_TEMPLATE/01_suggest-project.yml |
| 4 | +
|
| 5 | +Note that this script does NOT update `.vscode/projects.schema.json`. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from argparse import ArgumentParser |
| 11 | +from collections import deque |
| 12 | +from pathlib import Path |
| 13 | +from typing import TYPE_CHECKING |
| 14 | + |
| 15 | +from ruamel.yaml import YAML |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from collections.abc import Generator |
| 19 | + from typing import Literal |
| 20 | + |
| 21 | + |
| 22 | +def build_parser() -> ArgumentParser: |
| 23 | + parser = ArgumentParser( |
| 24 | + description=__doc__, |
| 25 | + epilog="Read from source, substitute the content in destination between " |
| 26 | + "“# sync-{categories,labels}: start” and “# sync-{categories,labels}: end”.", |
| 27 | + ) |
| 28 | + parser.add_argument("source", type=Path, help="projects.yaml") |
| 29 | + parser.add_argument( |
| 30 | + "destination", type=Path, help=".github/ISSUE_TEMPLATE/01_suggest-project.yml" |
| 31 | + ) |
| 32 | + return parser |
| 33 | + |
| 34 | + |
| 35 | +def format_categories(projects_yaml: dict, *, tab: str) -> Generator[str, None, None]: |
| 36 | + return (f"{tab}- {c['title']}" for c in projects_yaml["categories"]) |
| 37 | + |
| 38 | + |
| 39 | +def format_labels(projects_yaml: dict, *, tab: str) -> Generator[str, None, None]: |
| 40 | + return ( |
| 41 | + f"""{tab}- label: {label["name"]} — {label["description"]}""".strip("\n") |
| 42 | + for label in projects_yaml["labels"] |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def transform(issue_template: str, projects_yaml: dict) -> str: |
| 47 | + original_rows = issue_template.splitlines() |
| 48 | + |
| 49 | + rows: deque[str] = deque() |
| 50 | + interlude: None | Literal["categories", "labels"] = None |
| 51 | + for r in original_rows: |
| 52 | + match (interlude, r.strip()): |
| 53 | + case (None, "# sync-categories: start"): |
| 54 | + rows.append(r) |
| 55 | + |
| 56 | + tab = r[: r.index("#")] |
| 57 | + rows.extend(format_categories(projects_yaml, tab=tab)) |
| 58 | + interlude = "categories" |
| 59 | + case (_, "# sync-categories: end"): |
| 60 | + rows.append(r) |
| 61 | + interlude = None |
| 62 | + |
| 63 | + case (None, "# sync-labels: start"): |
| 64 | + rows.append(r) |
| 65 | + |
| 66 | + tab = r[: r.index("#")] |
| 67 | + rows.extend(format_labels(projects_yaml, tab=tab)) |
| 68 | + interlude = "labels" |
| 69 | + case (_, "# sync-labels: end"): |
| 70 | + rows.append(r) |
| 71 | + interlude = None |
| 72 | + |
| 73 | + case (None, _): |
| 74 | + rows.append(r) |
| 75 | + |
| 76 | + case _: |
| 77 | + # Skip original interludes |
| 78 | + pass |
| 79 | + |
| 80 | + return "\n".join(rows) + "\n" |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + args = build_parser().parse_args() |
| 85 | + src: Path = args.source |
| 86 | + dst: Path = args.destination |
| 87 | + |
| 88 | + yaml = YAML(typ="safe") |
| 89 | + projects_yaml = yaml.load(src.read_text(encoding="utf-8")) |
| 90 | + issue_template = dst.read_text(encoding="utf-8") |
| 91 | + |
| 92 | + dst.write_text(transform(issue_template, projects_yaml), encoding="utf-8") |
0 commit comments