-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild_adr_registry.py
More file actions
235 lines (199 loc) · 7.13 KB
/
build_adr_registry.py
File metadata and controls
235 lines (199 loc) · 7.13 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
"""
Build an ADR (Architecture Decision Record) registry for the `/builders/adrs`
UI page. Issue #1048 / Epic #1053.
Inputs : `docs/architecture/adrs/adr-*.md`
Output : `apps/ui/public/adrs/registry.json`
Front-matter contract
=====================
The current ADR template uses **bold-key** fields rather than YAML front-matter.
Recognized fields (case-insensitive on the key, value taken to end of line):
**Status**: Accepted | Proposed | Superseded | Deprecated | ...
**Date**: 2025-11-04
**Tags**: tag-a, tag-b, ...
**Deciders**: Name1, Name2
The ADR title comes from the first H1 (`# ADR-NNN: Title`).
The ADR number comes from the filename (`adr-035-ui-design-system.md` → 35).
Strict mode
===========
- Default (`--strict` NOT set):
* Parse errors are LOGGED but do not exit non-zero.
* Output JSON has `stale: true` and a `parse_errors` list.
* Empty input directory produces an empty-but-valid registry.
- `--strict`:
* Any parse error or missing required field exits 1.
* Output JSON has `stale: false`.
This honors Epic #1053's "default produces empty-but-valid JSON with stale
banner so a malformed ADR doesn't block the SWA build" rule.
Usage
=====
python scripts/ops/build_adr_registry.py
python scripts/ops/build_adr_registry.py --strict
python scripts/ops/build_adr_registry.py --source docs/architecture/adrs --output apps/ui/public/adrs/registry.json
"""
from __future__ import annotations
import argparse
import dataclasses
import datetime as dt
import json
import re
import sys
from pathlib import Path
from typing import Iterable
ADR_FILE_RE = re.compile(r"^adr-(\d+)-(.+)\.md$", re.IGNORECASE)
H1_RE = re.compile(r"^#\s+(?:ADR-\d+:?\s*)?(?P<title>.+?)\s*$")
BOLD_KEY_RE = re.compile(
r"^\*\*(?P<key>Status|Date|Tags|Deciders|References)\*\*\s*[::]\s*(?P<value>.+?)\s*$",
re.IGNORECASE,
)
@dataclasses.dataclass
class AdrEntry:
number: int
slug: str
title: str
status: str
date: str
tags: list[str]
deciders: list[str]
source_path: str
def to_json(self) -> dict[str, object]:
return {
"number": self.number,
"slug": self.slug,
"title": self.title,
"status": self.status,
"date": self.date,
"tags": self.tags,
"deciders": self.deciders,
"source_path": self.source_path,
}
def parse_adr(path: Path) -> tuple[AdrEntry | None, list[str]]:
"""Parse a single ADR file. Return (entry, errors)."""
errors: list[str] = []
match = ADR_FILE_RE.match(path.name)
if not match:
errors.append(f"{path.name}: filename does not match adr-NNN-*.md")
return None, errors
number = int(match.group(1))
slug = match.group(2).lower()
title: str | None = None
status: str | None = None
date: str | None = None
tags: list[str] = []
deciders: list[str] = []
text = path.read_text(encoding="utf-8")
for line in text.splitlines():
if title is None:
m = H1_RE.match(line)
if m:
title = m.group("title").strip()
continue
m = BOLD_KEY_RE.match(line)
if m:
key = m.group("key").lower()
value = m.group("value").strip()
if key == "status":
status = value.split(" ")[0].rstrip(".,;:")
elif key == "date":
date = value
elif key == "tags":
tags = [t.strip() for t in value.split(",") if t.strip()]
elif key == "deciders":
deciders = [d.strip() for d in value.split(",") if d.strip()]
if title is None:
errors.append(f"{path.name}: missing H1 title")
title = f"ADR-{number:03d}"
if status is None:
errors.append(f"{path.name}: missing **Status**")
status = "unknown"
if date is None:
errors.append(f"{path.name}: missing **Date**")
date = "unknown"
entry = AdrEntry(
number=number,
slug=slug,
title=title,
status=status,
date=date,
tags=tags,
deciders=deciders,
source_path=str(path).replace("\\", "/"),
)
return entry, errors
def build_registry(
source_dir: Path,
strict: bool,
) -> tuple[dict[str, object], list[str], int]:
"""Return (registry, parse_errors, exit_code)."""
parse_errors: list[str] = []
entries: list[AdrEntry] = []
if not source_dir.exists():
msg = f"source directory not found: {source_dir}"
parse_errors.append(msg)
if strict:
return _build_payload([], parse_errors, stale=True, source_dir=source_dir), parse_errors, 1
return _build_payload([], parse_errors, stale=True, source_dir=source_dir), parse_errors, 0
for path in sorted(source_dir.glob("adr-*.md")):
entry, errors = parse_adr(path)
if errors:
parse_errors.extend(errors)
if entry is not None:
entries.append(entry)
stale = bool(parse_errors)
if strict and parse_errors:
return _build_payload(entries, parse_errors, stale=False, source_dir=source_dir), parse_errors, 1
return _build_payload(entries, parse_errors, stale=stale, source_dir=source_dir), parse_errors, 0
def _build_payload(
entries: Iterable[AdrEntry],
parse_errors: list[str],
*,
stale: bool,
source_dir: Path,
) -> dict[str, object]:
return {
"schema_version": 1,
"generated_at": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
"source_dir": str(source_dir).replace("\\", "/"),
"stale": stale,
"parse_errors": parse_errors,
"adrs": [e.to_json() for e in entries],
}
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Build the ADR registry JSON.")
parser.add_argument(
"--source",
type=Path,
default=Path("docs/architecture/adrs"),
help="ADR source directory (default: docs/architecture/adrs).",
)
parser.add_argument(
"--output",
type=Path,
default=Path("apps/ui/public/adrs/registry.json"),
help="Output JSON path (default: apps/ui/public/adrs/registry.json).",
)
parser.add_argument(
"--strict",
action="store_true",
help="Fail on parse errors; default mode emits empty-but-valid JSON.",
)
args = parser.parse_args(argv)
registry, parse_errors, exit_code = build_registry(args.source, args.strict)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(registry, indent=2, ensure_ascii=False) + "\n",
encoding="utf-8",
)
n_adrs = len(registry["adrs"]) # type: ignore[arg-type]
print(
f"build_adr_registry: wrote {n_adrs} ADR(s) to {args.output} "
f"(stale={registry['stale']}, errors={len(parse_errors)})"
)
if parse_errors and not args.strict:
for e in parse_errors:
print(f" warn: {e}", file=sys.stderr)
elif parse_errors and args.strict:
for e in parse_errors:
print(f" error: {e}", file=sys.stderr)
return exit_code
if __name__ == "__main__":
sys.exit(main())