|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail CI when pyproject.toml version changes without a matching CHANGELOG entry.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def git_show(path: str, ref: str) -> str: |
| 13 | + try: |
| 14 | + return subprocess.check_output(["git", "show", f"{ref}:{path}"], text=True) |
| 15 | + except subprocess.CalledProcessError: |
| 16 | + return "" |
| 17 | + |
| 18 | + |
| 19 | +def read_version(text: str) -> str: |
| 20 | + match = re.search(r'(?m)^version = "([^"]+)"', text) |
| 21 | + if not match: |
| 22 | + raise SystemExit("could not read version from pyproject.toml") |
| 23 | + return match.group(1) |
| 24 | + |
| 25 | + |
| 26 | +def has_changelog_section(version: str) -> bool: |
| 27 | + changelog = Path("CHANGELOG.md") |
| 28 | + if not changelog.exists(): |
| 29 | + return False |
| 30 | + return bool(re.search(rf"^## \[{re.escape(version)}\]", changelog.read_text(), re.M)) |
| 31 | + |
| 32 | + |
| 33 | +def main() -> None: |
| 34 | + base = "origin/main" |
| 35 | + for candidate in ("origin/main", "origin/master"): |
| 36 | + if subprocess.call(["git", "rev-parse", "--verify", candidate], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0: |
| 37 | + base = candidate |
| 38 | + break |
| 39 | + |
| 40 | + current = Path("pyproject.toml").read_text() |
| 41 | + previous = git_show("pyproject.toml", base) |
| 42 | + if not previous: |
| 43 | + print("skip: no base pyproject.toml to compare") |
| 44 | + return |
| 45 | + |
| 46 | + old_version = read_version(previous) |
| 47 | + new_version = read_version(current) |
| 48 | + if old_version == new_version: |
| 49 | + print(f"version unchanged ({new_version})") |
| 50 | + return |
| 51 | + |
| 52 | + if not has_changelog_section(new_version): |
| 53 | + raise SystemExit( |
| 54 | + f"pyproject.toml version bumped to {new_version} but CHANGELOG.md " |
| 55 | + f"has no '## [{new_version}]' section" |
| 56 | + ) |
| 57 | + |
| 58 | + print(f"release metadata ok for {new_version}") |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + main() |
0 commit comments