Files
poli0981andClaude Opus 5.5 1407f0ef61 fix(links): the project URL in the User-Agent points at Gitea
The bot's User-Agent named the GitHub repository, which went with the
account. It now names the canonical source on git.poli0981.dev
(runbook §3.4). The release-notes script's docstring says Gitea.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 21:05:44 +07:00

71 lines
2.3 KiB
Python

"""
Print the changelog entry for one version, for use as Gitea release notes.
python scripts/release_notes.py v1.2.0
Entries live in `changelog/<major>.x.md` under `## x.y.z — YYYY-MM-DD` headings.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
HEADING_RE = re.compile(r"^## (?P<version>\S+) — \d{4}-\d{2}-\d{2}\s*$")
# A release tag: v1.2.0, or a pre-release such as v2.0.0-beta.1.
VERSION_RE = re.compile(r"v?(?P<major>\d+)\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?")
CHANGELOG_DIR = Path(__file__).resolve().parent.parent / "changelog"
def normalize_version(version: str) -> str:
return version.strip().removeprefix("v")
def changelog_file(version: str, changelog_dir: Path) -> Path:
"""`<major>.x.md` for a release version; the file name is built from the number alone."""
m = VERSION_RE.fullmatch(version.strip())
if m is None:
raise ValueError(f"Not a release version: {version!r}")
return changelog_dir / f"{int(m.group('major'))}.x.md"
def extract_section(text: str, version: str) -> str:
"""Body of the `## <version> — <date>` entry, without the heading."""
wanted = normalize_version(version)
lines = text.splitlines()
start: int | None = None
for i, line in enumerate(lines):
if start is None:
m = HEADING_RE.match(line)
if m and m.group("version") == wanted:
start = i + 1
elif line.startswith("## "):
return "\n".join(lines[start:i]).strip() + "\n"
if start is None:
raise LookupError(f"No changelog entry for {wanted}")
return "\n".join(lines[start:]).strip() + "\n"
def main(argv: list[str], changelog_dir: Path = CHANGELOG_DIR) -> int:
if len(argv) != 2:
print("Usage: release_notes.py <version>", file=sys.stderr)
return 2
try:
path = changelog_file(argv[1], changelog_dir)
except ValueError as e:
print(f"[ERROR] {e}", file=sys.stderr)
return 1
try:
text = path.read_text(encoding="utf-8")
section = extract_section(text, argv[1])
except (OSError, LookupError) as e:
print(f"[ERROR] {e} (looked in {path})", file=sys.stderr)
return 1
sys.stdout.write(section)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))