|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +RELEASE_HEADER_RE = re.compile(r"^## \[(?P<version>[^\]]+)\] - \d{4}-\d{2}-\d{2}\s*$") |
| 11 | +COMPARE_LINK_RE = re.compile(r"^\[(?P<version>[^\]]+)\]:\s+(?P<url>\S+)\s*$") |
| 12 | + |
| 13 | + |
| 14 | +def _normalize_version(value: str) -> str: |
| 15 | + return value[1:] if value.startswith("v") else value |
| 16 | + |
| 17 | + |
| 18 | +def _load_changelog_lines(changelog_path: Path, ref: str | None) -> list[str]: |
| 19 | + if ref is None: |
| 20 | + return changelog_path.read_text(encoding="utf-8").splitlines() |
| 21 | + |
| 22 | + content = subprocess.run( |
| 23 | + ["git", "show", f"{ref}:{changelog_path.as_posix()}"], |
| 24 | + check=True, |
| 25 | + capture_output=True, |
| 26 | + text=True, |
| 27 | + ).stdout |
| 28 | + return content.splitlines() |
| 29 | + |
| 30 | + |
| 31 | +def _extract_release_body(changelog_lines: list[str], version: str) -> str: |
| 32 | + start_index: int | None = None |
| 33 | + |
| 34 | + for index, line in enumerate(changelog_lines): |
| 35 | + match = RELEASE_HEADER_RE.match(line) |
| 36 | + if match and match.group("version") == version: |
| 37 | + start_index = index + 1 |
| 38 | + break |
| 39 | + |
| 40 | + if start_index is None: |
| 41 | + raise ValueError(f"Could not find CHANGELOG.md section for version {version!r}.") |
| 42 | + |
| 43 | + end_index = len(changelog_lines) |
| 44 | + for index in range(start_index, len(changelog_lines)): |
| 45 | + if changelog_lines[index].startswith("## ["): |
| 46 | + end_index = index |
| 47 | + break |
| 48 | + |
| 49 | + body = "\n".join(changelog_lines[start_index:end_index]).strip() |
| 50 | + if not body: |
| 51 | + raise ValueError(f"CHANGELOG.md section for version {version!r} is empty.") |
| 52 | + return body |
| 53 | + |
| 54 | + |
| 55 | +def _extract_compare_link(changelog_lines: list[str], version: str) -> str: |
| 56 | + for line in changelog_lines: |
| 57 | + match = COMPARE_LINK_RE.match(line) |
| 58 | + if match and match.group("version") == version: |
| 59 | + return match.group("url") |
| 60 | + raise ValueError(f"Could not find compare link for version {version!r} in CHANGELOG.md.") |
| 61 | + |
| 62 | + |
| 63 | +def build_release_notes(changelog_path: Path, tag: str, ref: str | None) -> str: |
| 64 | + version = _normalize_version(tag) |
| 65 | + changelog_lines = _load_changelog_lines(changelog_path=changelog_path, ref=ref) |
| 66 | + body = _extract_release_body(changelog_lines, version) |
| 67 | + compare_link = _extract_compare_link(changelog_lines, version) |
| 68 | + return f"## What's Changed\n\n{body}\n\n**Full Changelog**: {compare_link}\n" |
| 69 | + |
| 70 | + |
| 71 | +def main() -> None: |
| 72 | + parser = argparse.ArgumentParser( |
| 73 | + description="Build GitHub release notes from a CHANGELOG.md release section." |
| 74 | + ) |
| 75 | + parser.add_argument("--tag", required=True, help="Release tag, for example v2.10.2.") |
| 76 | + parser.add_argument( |
| 77 | + "--changelog", |
| 78 | + default="CHANGELOG.md", |
| 79 | + help="Path to the changelog file. Defaults to CHANGELOG.md.", |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "--ref", |
| 83 | + default=None, |
| 84 | + help="Optional git ref to read the changelog from, for example v2.10.2.", |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "--output", |
| 88 | + required=True, |
| 89 | + help="Output path for the generated GitHub release notes markdown.", |
| 90 | + ) |
| 91 | + args = parser.parse_args() |
| 92 | + |
| 93 | + release_notes = build_release_notes( |
| 94 | + changelog_path=Path(args.changelog), |
| 95 | + tag=args.tag, |
| 96 | + ref=args.ref, |
| 97 | + ) |
| 98 | + Path(args.output).write_text(release_notes, encoding="utf-8") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments