From a214855340d07d271eaaf3bac72d7e66688cccc4 Mon Sep 17 00:00:00 2001 From: Max Ghenis <6076111+MaxGhenis@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:04:26 -0500 Subject: [PATCH] Require new typed changelog fragments in pull requests --- .github/workflows/changelog_entry.yaml | 52 ++++++++++++++++++++--- CLAUDE.md | 16 +++---- changelog.d/fix-copy-performance.fixed.md | 1 + changelog.d/fix-copy-performance.yaml | 4 -- changelog.d/migrate-to-app-token.fixed.md | 1 + changelog.d/migrate-to-app-token.yaml | 4 -- changelog.d/require-pr-fragment.fixed.md | 1 + tests/ci/test_bump_version.py | 40 +++++++++++++++++ 8 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 changelog.d/fix-copy-performance.fixed.md delete mode 100644 changelog.d/fix-copy-performance.yaml create mode 100644 changelog.d/migrate-to-app-token.fixed.md delete mode 100644 changelog.d/migrate-to-app-token.yaml create mode 100644 changelog.d/require-pr-fragment.fixed.md create mode 100644 tests/ci/test_bump_version.py diff --git a/.github/workflows/changelog_entry.yaml b/.github/workflows/changelog_entry.yaml index 4cf63270..24faf429 100644 --- a/.github/workflows/changelog_entry.yaml +++ b/.github/workflows/changelog_entry.yaml @@ -4,18 +4,56 @@ on: pull_request: branches: [ main ] +permissions: + contents: read + jobs: check-changelog: name: Check changelog fragment runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 + with: + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-python@v6 + with: + python-version: '3.11' - name: Check for changelog fragment + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} run: | - FRAGMENTS=$(find changelog.d -type f ! -name '.gitkeep' | wc -l) - if [ "$FRAGMENTS" -eq 0 ]; then - echo "::error::No changelog fragment found in changelog.d/" - echo "Add one with: echo 'Description.' > changelog.d/\$(git branch --show-current)..md" - echo "Types: added, changed, fixed, removed, breaking" - exit 1 - fi + python - <<'PYTHON' + import os + import subprocess + import tomllib + from pathlib import Path + + with open("pyproject.toml", "rb") as pyproject: + config = tomllib.load(pyproject)["tool"]["towncrier"] + directory = Path(config["directory"]) + types = {entry["directory"] for entry in config["type"]} + + # HEAD is GitHub's test merge commit for this pull request. + added = subprocess.check_output([ + "git", "diff", "--find-renames", "--diff-filter=A", "--name-only", + "-z", os.environ["BASE_SHA"], "HEAD", "--", str(directory), + ]) + for name in filter(None, added.split(b"\0")): + path = Path(os.fsdecode(name)) + stem, _, category = path.name.removesuffix(".md").rpartition(".") + if ( + path.parent == directory + and stem.strip() + and category in types + and path.is_file() + and not path.is_symlink() + ): + break + else: + print( + f"::error::Add a changelog fragment at {directory}/..md." + f" Configured types: {', '.join(sorted(types))}." + ) + raise SystemExit(1) + PYTHON diff --git a/CLAUDE.md b/CLAUDE.md index 21429ebe..3f444a1c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,18 +4,13 @@ - All files must end with a newline character - **ALWAYS run `make format` before committing** - this will auto-fix most style issues - Run `make lint` after formatting to check if there are any remaining issues -- Code formatting uses `ruff format` (79-char line length configured in pyproject.toml) +- `ruff format` uses its default 88-character line length. The Makefile sets a 79-character wrap width for `docformatter`. ## Changelog Requirements -- Every PR must include a `changelog.d/` file at the root -- Format: - ```yaml - - bump: patch|minor|major - changes: - added|changed|removed|fixed: - - Description of change - ``` -- The file must not be empty and must end with a newline +- Every PR targeting `main` must add a fragment under the root `changelog.d/` directory. +- Name it `changelog.d/..md`, for example `changelog.d/fix-copy-performance.fixed.md`. +- The types in `pyproject.toml` determine the version bump. Use `fixed` or `changed` for a patch, `added` or `removed` for a minor bump, and `breaking` for a major bump. +- Write a brief description of the change in Markdown and end the file with a newline. ## Testing - Install development dependencies first: `make install` or `pip install -e ".[dev]"` @@ -37,7 +32,6 @@ ## Dependencies - Dependencies are managed in `pyproject.toml` - Optional dependencies go in `[project.optional-dependencies]` -- When removing dependencies, also remove from `microdf/_optional.py` VERSIONS dict ## Documentation - Documentation notebooks are in `docs/` directory diff --git a/changelog.d/fix-copy-performance.fixed.md b/changelog.d/fix-copy-performance.fixed.md new file mode 100644 index 00000000..c16418c9 --- /dev/null +++ b/changelog.d/fix-copy-performance.fixed.md @@ -0,0 +1 @@ +Fixed O(N²) weight linking in MicroDataFrame.copy() diff --git a/changelog.d/fix-copy-performance.yaml b/changelog.d/fix-copy-performance.yaml deleted file mode 100644 index 825ef29e..00000000 --- a/changelog.d/fix-copy-performance.yaml +++ /dev/null @@ -1,4 +0,0 @@ -- bump: patch - changes: - fixed: - - Fixed O(N²) weight linking in MicroDataFrame.copy() diff --git a/changelog.d/migrate-to-app-token.fixed.md b/changelog.d/migrate-to-app-token.fixed.md new file mode 100644 index 00000000..c0884319 --- /dev/null +++ b/changelog.d/migrate-to-app-token.fixed.md @@ -0,0 +1 @@ +Migrate versioning workflow from the expired POLICYENGINE_GITHUB PAT to a short-lived GitHub App token, matching the pattern already used by policyengine-us and policyengine-api. diff --git a/changelog.d/migrate-to-app-token.yaml b/changelog.d/migrate-to-app-token.yaml deleted file mode 100644 index d5a49bed..00000000 --- a/changelog.d/migrate-to-app-token.yaml +++ /dev/null @@ -1,4 +0,0 @@ -- bump: patch - changes: - fixed: - - Migrate versioning workflow from the expired POLICYENGINE_GITHUB PAT to a short-lived GitHub App token, matching the pattern already used by policyengine-us and policyengine-api. diff --git a/changelog.d/require-pr-fragment.fixed.md b/changelog.d/require-pr-fragment.fixed.md new file mode 100644 index 00000000..db00c7a5 --- /dev/null +++ b/changelog.d/require-pr-fragment.fixed.md @@ -0,0 +1 @@ +Require each pull request to add a changelog fragment with a configured type. diff --git a/tests/ci/test_bump_version.py b/tests/ci/test_bump_version.py new file mode 100644 index 00000000..d290b780 --- /dev/null +++ b/tests/ci/test_bump_version.py @@ -0,0 +1,40 @@ +from pathlib import Path +from runpy import run_path + +import pytest + +script = run_path(str(Path(__file__).resolve().parents[2] / ".github/bump_version.py")) +infer_bump = script["infer_bump"] +bump_version = script["bump_version"] + + +@pytest.mark.parametrize( + ("names", "expected"), + [ + (["change.fixed.md"], "patch"), + (["change.changed"], "patch"), + (["fix.fixed.md", "feature.added.md"], "minor"), + (["feature.removed.md"], "minor"), + (["feature.added.md", "api.breaking.md"], "major"), + ], +) +def test_infer_bump(tmp_path, names, expected): + for name in names: + (tmp_path / name).touch() + assert infer_bump(tmp_path) == expected + + +def test_infer_bump_without_fragments(tmp_path, capsys): + (tmp_path / ".gitkeep").touch() + with pytest.raises(SystemExit) as exc: + infer_bump(tmp_path) + assert exc.value.code == 1 + assert capsys.readouterr().err == "No changelog fragments found\n" + + +@pytest.mark.parametrize( + ("bump", "expected"), + [("major", "2.0.0"), ("minor", "1.3.0"), ("patch", "1.2.10")], +) +def test_bump_version(bump, expected): + assert bump_version("1.2.9", bump) == expected