Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.5 KB
Raw Blame
#!/usr/bin/env python3
"""Fail on invalid changelog.d/ fragment filenames.

towncrier only renders fragments whose category is declared in
``[[tool.towncrier.type]]``; anything else is silently dropped from the
release notes (and ``towncrier check`` errors on it). #5023's fragment
shipped as ``.refactor.md`` — an undeclared category — and would have
vanished from the next release; the advisory reminder hook printed a
warning but nothing failed. This hook is the blocking counterpart: it
validates every file in changelog.d/ (not just staged ones, so CI's
``pre-commit run --all-files`` enforces it on the whole tree) and exits
non-zero on any problem.
"""

import argparse
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))

from _changelog_fragments import (
    classify_fragment,
    load_categories,
    towncrier_category,
)

REPO_ROOT = Path(__file__).resolve().parent.parent


def validate_directory(changelog_dir, categories):
    """Return one problem string per invalid file in *changelog_dir*.

    Every regular file except README.md must be a valid fragment.
    Dotfiles that towncrier would not render (e.g. .DS_Store, .gitkeep)
    are skipped so local editor/OS droppings don't fail commits that never
    stage them. A dotfile that towncrier *would* render (``.123.breaking.md``
    parses as issue ``.123``, category ``breaking``) is refused: it is
    published in the release notes while hiding from shell globs such as
    the release-type selector's.
    """
    problems = []
    for path in sorted(Path(changelog_dir).iterdir()):
        if not path.is_file():
            continue
        if path.name.startswith("."):
            category = towncrier_category(path.name, categories)
            if category is not None:
                problems.append(
                    f"{path.name} — dot-prefixed fragment; towncrier renders "
                    f"it under `{category}` but globs skip it. Rename it to "
                    f"`<id>.{category}.md` or `+<slug>.{category}.md`"
                )
            continue
        if path.name == "README.md":
            continue
        kind, category = classify_fragment(path.name, categories)
        if kind == "bad-name":
            problems.append(
                f"{path.name} — does not match `<id>.<category>.md` or "
                f"`+<slug>.<category>.md`"
            )
        elif kind == "bad-category":
            problems.append(
                f"{path.name} — unknown category `{category}`; towncrier "
                f"silently drops it from the release notes. Use one of: "
                f"{', '.join(categories)}"
            )
    return problems


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "changelog_dir",
        nargs="?",
        default=str(REPO_ROOT / "changelog.d"),
        help="Directory to validate (defaults to the repo's changelog.d/)",
    )
    parser.add_argument(
        "--pyproject",
        default=str(REPO_ROOT / "pyproject.toml"),
        help="pyproject.toml declaring [[tool.towncrier.type]] categories",
    )
    args = parser.parse_args(argv)

    categories = load_categories(args.pyproject)
    problems = validate_directory(args.changelog_dir, categories)
    if problems:
        print("Invalid changelog.d/ fragment(s):")
        for problem in problems:
            print(f"  - {problem}")
        print("See changelog.d/README.md for the naming convention.")
        return 1
    return 0


if __name__ == "__main__":
    sys.exit(main())