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.

4.3 KB
Raw Blame
"""Shared helpers for changelog.d/ fragment-name validation.

Used by the blocking ``check-changelog-fragments`` hook and the advisory
``recommend-release-notes`` hook so the fragment grammar and category
list can't drift between them.
"""

import os
import re
import tomllib
from fnmatch import fnmatch
from pathlib import Path

# changelog.d/<id>.<category>[.<n>].md or changelog.d/+<slug>.<category>[.<n>].md
# - <id>: integer PR/issue number
# - +<slug>: orphan fragment with no PR/issue, slug is [A-Za-z0-9_-]+
# - .<n>: optional integer counter suffix for multiple fragments of the
#   same (id, category) — towncrier renders each as a separate bullet,
#   all linked back to the same PR/issue.
FRAGMENT_RE = re.compile(
    r"^(?:\d+|\+[A-Za-z0-9_-]+)\.(?P<category>[a-z]+)(?:\.\d+)?\.md$"
)


def load_categories(pyproject_path):
    """Read ``[[tool.towncrier.type]].directory`` entries from pyproject.toml.

    Raises on an unreadable file or a missing/empty type table. The
    advisory hook catches and falls back to a default list; the blocking
    hook lets the error propagate so a broken towncrier config fails
    loud instead of validating against a guessed category list.
    """
    with Path(pyproject_path).open("rb") as fh:
        cfg = tomllib.load(fh)
    types = cfg["tool"]["towncrier"]["type"]
    cats = tuple(t["directory"] for t in types if "directory" in t)
    if not cats:
        raise ValueError(
            f"no [[tool.towncrier.type]] entries in {pyproject_path}"
        )
    return cats


def classify_fragment(name, categories):
    """Classify a fragment filename against the grammar and *categories*.

    Returns ``("ok", category)`` for a valid fragment, ``("bad-category",
    category)`` for a well-formed name whose category isn't declared, or
    ``("bad-name", None)`` for a filename that doesn't match the fragment
    pattern at all.
    """
    m = FRAGMENT_RE.match(name)
    if not m:
        return "bad-name", None
    category = m.group("category")
    if category not in categories:
        return "bad-category", category
    return "ok", category


# towncrier 24.8's built-in ignore list (``_builder.find_fragments``). It is
# matched with ``fnmatch`` against the lower-cased basename.
TOWNCRIER_IGNORED = (
    ".gitignore",
    ".gitkeep",
    ".keep",
    "readme",
    "readme.md",
    "readme.rst",
)


def load_towncrier_ignores(pyproject_path):
    """Return the extra ignore patterns towncrier adds from *pyproject_path*.

    Mirrors ``find_fragments``: a string ``template`` contributes its
    basename verbatim and every ``ignore`` entry is lower-cased.
    """
    with Path(pyproject_path).open("rb") as fh:
        cfg = tomllib.load(fh)["tool"]["towncrier"]
    extra = []
    template = cfg.get("template")
    if isinstance(template, str):
        extra.append(Path(template).name)
    extra.extend(str(pattern).lower() for pattern in cfg.get("ignore", ()))
    return tuple(extra)


def towncrier_category(name, categories, ignores=()):
    """Return the category towncrier renders *name* under, or ``None``.

    This mirrors towncrier 24.8 exactly, not the stricter repo grammar in
    ``FRAGMENT_RE``: ``find_fragments`` lists every directory entry
    (dot-prefixed names and non-``.md`` extensions included), skips the
    ignore list, and ``parse_newfragment_basename`` takes the last
    dot-separated part after the first that names a declared category.
    ``tests/ci/test_version_bump_selection.py`` checks it against
    towncrier's own ``find_fragments``.
    """
    lowered = name.lower()
    if any(fnmatch(lowered, pattern) for pattern in TOWNCRIER_IGNORED):
        return None
    if any(fnmatch(lowered, pattern) for pattern in ignores):
        return None
    parts = name.split(".")
    for part in reversed(parts[1:]):
        if part in categories:
            return part
    return None


def towncrier_fragments(changelog_dir, pyproject_path):
    """Map each entry towncrier would render from *changelog_dir* to its category."""
    categories = load_categories(pyproject_path)
    ignores = load_towncrier_ignores(pyproject_path)
    try:
        names = sorted(os.listdir(changelog_dir))
    except FileNotFoundError:
        names = []
    found = {}
    for name in names:
        category = towncrier_category(name, categories, ignores)
        if category is not None:
            found[name] = category
    return found