Files
Bifrost/scripts/check-changelog.sh

57 lines
2.4 KiB
Bash

#!/usr/bin/env sh
set -eu
file=${1:-CHANGELOG.md}
python_bin=${PYTHON:-python3}
"$python_bin" - "$file" <<'PY'
import datetime
import re
import sys
from pathlib import Path
text = Path(sys.argv[1]).read_text(encoding="utf-8")
if not text.startswith("# Changelog\n"):
raise SystemExit("Missing Changelog title.")
if "Keep a Changelog" not in text or "Semantic Versioning" not in text:
raise SystemExit("Missing Keep a Changelog or Semantic Versioning reference.")
if not re.search(r"^## \[Unreleased\]$", text, re.M):
raise SystemExit("Missing Unreleased section.")
headings = re.findall(r"^## (.+)$", text, re.M)
versions = []
for heading in headings:
if heading == "[Unreleased]":
continue
match = re.fullmatch(r"\[(\d+\.\d+\.\d+)\] - (\d{4}-\d{2}-\d{2})", heading)
if not match:
raise SystemExit(f"Invalid version heading: {heading}")
version, date = match.groups()
datetime.date.fromisoformat(date)
versions.append(version)
if versions != sorted(versions, key=lambda value: tuple(map(int, value.split("."))), reverse=True):
raise SystemExit("Version sections must be newest first.")
allowed = {"Added", "Changed", "Deprecated", "Removed", "Fixed", "Security"}
for category in re.findall(r"^### (.+)$", text, re.M):
if category not in allowed:
raise SystemExit(f"Invalid changelog category: {category}")
links = dict(re.findall(r"^\[([^]]+)\]: (\S+)$", text, re.M))
if "Unreleased" not in links:
raise SystemExit("Missing Unreleased comparison link.")
urls = [links["Unreleased"]] + [links.get(version, "") for version in versions]
if any(not url for url in urls):
raise SystemExit("A version comparison link is missing.")
bases = {url.split("/compare/", 1)[0] for url in urls if "/compare/" in url}
if len(bases) != 1 or len(bases) == 0:
raise SystemExit("Comparison links must use one repository URL and /compare/ paths.")
latest = versions[0] if versions else "0.1.0"
if not links["Unreleased"].endswith(f"/compare/v{latest}...HEAD"):
raise SystemExit("Unreleased comparison link does not match the latest release.")
for index, version in enumerate(versions):
previous = "v" + versions[index + 1] if index + 1 < len(versions) else "v0.0.0"
expected = f"/compare/{previous}...v{version}"
if not links[version].endswith(expected):
raise SystemExit(f"Comparison link for {version} must end with {expected}.")
print("Changelog format looks valid.")
PY