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
|
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import re
import subprocess
from pathlib import Path
from typing import Any
ASSET_TARGETS = (
"linux-x86_64",
"linux-aarch64",
"macos-x86_64",
"macos-aarch64",
"windows-x86_64",
)
EXPECTED_ASSET_NAMES = {
**{target: f"herdr-{target}" for target in ASSET_TARGETS},
"windows-x86_64": "herdr-windows-x86_64.zip",
}
HIDDEN_SUBJECTS = (
"docs: publish release distribution",
"docs: update website manifest",
"docs: update preview manifest",
"chore: approve contributor",
"chore: approve merged contributor",
)
TYPE_HEADINGS = {
"feat": "Added",
"fix": "Fixed",
"perf": "Performance",
"docs": "Maintenance",
"ci": "Maintenance",
"test": "Maintenance",
"refactor": "Maintenance",
"chore": "Maintenance",
}
TYPE_ORDER = ("Added", "Fixed", "Performance", "Maintenance", "Other")
COMMIT_RE = re.compile(r"^(?P<kind>[a-z]+)(?:\([^)]+\))?!?:\s+(?P<body>.+)$")
ENDPOINT_PROTOCOL_SOURCE_PATH = Path("src/protocol/endpoint.rs")
def run_git(args: list[str]) -> str:
return subprocess.check_output(["git", *args], text=True).strip()
def normalize_version(version: str) -> str:
return version.strip().removeprefix("v")
def read_endpoint_protocol_generation(
source_path: Path = ENDPOINT_PROTOCOL_SOURCE_PATH,
) -> int:
content = source_path.read_text(encoding="utf-8")
match = re.search(r"pub const ENDPOINT_PROTOCOL_GENERATION: u32 = (\d+);", content)
if not match:
raise ValueError(
f"could not read ENDPOINT_PROTOCOL_GENERATION from {source_path}"
)
return int(match.group(1))
def latest_stable_tag(ref: str | None = None) -> str:
args = ["describe", "--tags", "--match", "v[0-9]*", "--abbrev=0"]
if ref:
args.append(ref)
return run_git(args)
def git_is_ancestor(ancestor: str, descendant: str) -> bool:
result = subprocess.run(
["git", "merge-base", "--is-ancestor", ancestor, descendant],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
return result.returncode == 0
def read_json(path: Path) -> dict[str, Any] | None:
if not path.exists():
return None
return json.loads(path.read_text(encoding="utf-8"))
def previous_preview_commit(path: Path) -> str | None:
data = read_json(path)
if not data:
return None
commit = data.get("commit")
return commit if isinstance(commit, str) and commit.strip() else None
def hidden_subject(subject: str) -> bool:
lowered = subject.strip().lower()
return any(lowered.startswith(prefix) for prefix in HIDDEN_SUBJECTS)
def latest_publishable_commit(ref: str) -> str:
output = run_git(["log", "--pretty=format:%H%x00%s", ref])
for line in output.splitlines():
commit, _, subject = line.partition("\x00")
if commit and not hidden_subject(subject):
return commit
raise SystemExit(f"no publishable commit found in {ref}")
def commit_subjects(previous: str, commit: str) -> list[str]:
output = run_git(["log", "--pretty=format:%s", f"{previous}..{commit}"])
if not output:
return []
subjects = []
for line in output.splitlines():
stripped = line.strip()
if not stripped:
continue
if hidden_subject(stripped):
continue
subjects.append(stripped)
return subjects
def preview_range_base(previous: str, commit: str) -> str:
try:
stable = latest_stable_tag(commit)
except subprocess.CalledProcessError:
return previous
if git_is_ancestor(previous, stable) and git_is_ancestor(stable, commit):
return stable
return previous
def humanize_subject(subject: str) -> tuple[str, str]:
match = COMMIT_RE.match(subject)
if not match:
return "Other", subject[0].upper() + subject[1:]
kind = match.group("kind")
body = match.group("body").strip()
heading = TYPE_HEADINGS.get(kind, "Other")
if body:
body = body[0].upper() + body[1:]
else:
body = subject
return heading, body
def build_notes(previous: str, commit: str, build_id: str, base_version: str, repo: str) -> str:
short = commit[:12]
compare = f"https://github.com/{repo}/compare/{previous}...{commit}"
lines = [
f"Preview build {build_id}",
"",
f"Built from `{short}` on `master`.",
f"Base stable: v{normalize_version(base_version)}",
f"Compare: {compare}",
"",
]
grouped: dict[str, list[str]] = {heading: [] for heading in TYPE_ORDER}
for subject in commit_subjects(previous, commit):
heading, body = humanize_subject(subject)
grouped.setdefault(heading, []).append(body)
wrote = False
for heading in TYPE_ORDER:
items = grouped.get(heading, [])
if not items:
continue
wrote = True
lines.append(f"### {heading}")
for item in items:
lines.append(f"- {item}")
lines.append("")
if not wrote:
lines.extend(["### Changed", "- Rebuilt preview from the current master branch.", ""])
return "\n".join(lines).rstrip() + "\n"
def default_asset_urls(repo: str, tag: str) -> dict[str, str]:
return {
target: f"https://github.com/{repo}/releases/download/{tag}/{EXPECTED_ASSET_NAMES[target]}"
for target in ASSET_TARGETS
}
def read_sha_file(path: Path | None) -> dict[str, str]:
if path is None:
return {}
data = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(data, dict):
raise SystemExit("sha file must be a JSON object")
return {str(key): str(value) for key, value in data.items()}
def asset_objects(urls: dict[str, str], shas: dict[str, str]) -> dict[str, dict[str, str]]:
assets: dict[str, dict[str, str]] = {}
for target in ASSET_TARGETS:
url = urls[target]
entry = {"url": url}
sha = shas.get(target)
if sha:
entry["sha256"] = sha
if target.startswith("windows-"):
if not sha or not re.fullmatch(r"[0-9a-fA-F]{64}", sha):
raise ValueError(f"{target} requires a SHA-256 digest")
entry["format"] = "zip"
assets[target] = entry
return assets
def build_manifest(
output: Path,
repo: str,
tag: str,
build_id: str,
commit: str,
built_at: str,
base_version: str,
protocol: int,
notes: str,
shas: dict[str, str],
retain: int,
) -> str:
urls = default_asset_urls(repo, tag)
assets = asset_objects(urls, shas)
current = read_json(output) or {}
builds = current.get("builds") if isinstance(current.get("builds"), dict) else {}
builds = dict(builds)
endpoint_generation = read_endpoint_protocol_generation()
builds[build_id] = {
"base_version": normalize_version(base_version),
"commit": commit,
"built_at": built_at,
"protocol": protocol,
"endpoint_generation": endpoint_generation,
"tag": tag,
"assets": assets,
}
ordered_builds = {
key: builds[key]
for key in sorted(
builds,
key=lambda key: str(builds[key].get("built_at", "")),
reverse=True,
)[:retain]
}
manifest = {
"schema_version": 1,
"channel": "preview",
"base_version": normalize_version(base_version),
"build_id": build_id,
"commit": commit,
"built_at": built_at,
"protocol": protocol,
"endpoint_generation": endpoint_generation,
"notes": notes.strip(),
"assets": assets,
"builds": ordered_builds,
}
return json.dumps(manifest, indent=2) + "\n"
def cmd_notes(args: argparse.Namespace) -> int:
previous = args.previous or previous_preview_commit(Path(args.manifest)) or latest_stable_tag()
notes = build_notes(previous, args.commit, args.build_id, args.base_version, args.repo)
Path(args.output).write_text(notes, encoding="utf-8")
return 0
def cmd_manifest(args: argparse.Namespace) -> int:
notes = Path(args.notes).read_text(encoding="utf-8")
shas = read_sha_file(Path(args.sha_file) if args.sha_file else None)
content = build_manifest(
output=Path(args.output),
repo=args.repo,
tag=args.tag,
build_id=args.build_id,
commit=args.commit,
built_at=args.built_at,
base_version=args.base_version,
protocol=args.protocol,
notes=notes,
shas=shas,
retain=args.retain,
)
Path(args.output).write_text(content, encoding="utf-8")
return 0
def cmd_current_commit(args: argparse.Namespace) -> int:
commit = previous_preview_commit(Path(args.manifest))
if commit:
print(commit)
return 0
def cmd_select_commit(args: argparse.Namespace) -> int:
print(latest_publishable_commit(args.ref))
return 0
def cmd_range_base(args: argparse.Namespace) -> int:
print(preview_range_base(args.previous, args.commit))
return 0
def main() -> int:
parser = argparse.ArgumentParser(description="Preview channel release helpers")
sub = parser.add_subparsers(required=True)
notes = sub.add_parser("notes")
notes.add_argument("--manifest", default="distribution/preview.json")
notes.add_argument("--previous")
notes.add_argument("--commit", required=True)
notes.add_argument("--build-id", required=True)
notes.add_argument("--base-version", required=True)
notes.add_argument("--repo", default="herdrdev/herdr")
notes.add_argument("--output", required=True)
notes.set_defaults(func=cmd_notes)
manifest = sub.add_parser("manifest")
manifest.add_argument("--output", default="distribution/preview.json")
manifest.add_argument("--repo", default="herdrdev/herdr")
manifest.add_argument("--tag", required=True)
manifest.add_argument("--build-id", required=True)
manifest.add_argument("--commit", required=True)
manifest.add_argument("--built-at", required=True)
manifest.add_argument("--base-version", required=True)
manifest.add_argument("--protocol", required=True, type=int)
manifest.add_argument("--notes", required=True)
manifest.add_argument("--sha-file")
manifest.add_argument("--retain", type=int, default=30)
manifest.set_defaults(func=cmd_manifest)
current = sub.add_parser("current-commit")
current.add_argument("--manifest", default="distribution/preview.json")
current.set_defaults(func=cmd_current_commit)
select = sub.add_parser("select-commit")
select.add_argument("--ref", default="origin/master")
select.set_defaults(func=cmd_select_commit)
range_base = sub.add_parser("range-base")
range_base.add_argument("--previous", required=True)
range_base.add_argument("--commit", required=True)
range_base.set_defaults(func=cmd_range_base)
args = parser.parse_args()
return args.func(args)
if __name__ == "__main__":
raise SystemExit(main())
|