Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Infer semver bump from towncrier fragment types and update version."""

import re
import sys
from pathlib import Path


def get_current_version(pyproject_path: Path) -> str:
text = pyproject_path.read_text()
match = re.search(r'^version\s*=\s*"(\d+\.\d+\.\d+)"', text, re.MULTILINE)
if not match:
print(
"Could not find version in pyproject.toml",
file=sys.stderr,
)
sys.exit(1)
return match.group(1)


def infer_bump(changelog_dir: Path) -> str:
fragments = [
f
for f in changelog_dir.iterdir()
if f.is_file() and f.name != ".gitkeep"
]
if not fragments:
print("No changelog fragments found", file=sys.stderr)
sys.exit(1)

categories = {f.suffix.lstrip(".") for f in fragments}
for f in fragments:
parts = f.stem.split(".")
if len(parts) >= 2:
categories.add(parts[-1])

if "breaking" in categories:
return "major"
if "added" in categories or "removed" in categories:
return "minor"
return "patch"


def bump_version(version: str, bump: str) -> str:
major, minor, patch = (int(x) for x in version.split("."))
if bump == "major":
return f"{major + 1}.0.0"
elif bump == "minor":
return f"{major}.{minor + 1}.0"
else:
return f"{major}.{minor}.{patch + 1}"


def update_file(path: Path, old_version: str, new_version: str):
text = path.read_text()
updated = text.replace(
f'version = "{old_version}"',
f'version = "{new_version}"',
)
if updated != text:
path.write_text(updated)
print(f" Updated {path}")


def main():
root = Path(__file__).resolve().parent.parent
pyproject = root / "pyproject.toml"
changelog_dir = root / "changelog.d"

current = get_current_version(pyproject)
bump = infer_bump(changelog_dir)
new = bump_version(current, bump)

print(f"Version: {current} -> {new} ({bump})")

update_file(pyproject, current, new)


if __name__ == "__main__":
main()
7 changes: 0 additions & 7 deletions .github/check-changelog-entry.sh

This file was deleted.

29 changes: 10 additions & 19 deletions .github/workflows/pr_changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
name: Changelog entry

on:
pull_request:
branches: [main]

jobs:
check-fork:
check-changelog:
name: Check changelog fragment
runs-on: ubuntu-latest
steps:
- name: Check if PR is from fork
- uses: actions/checkout@v4
- name: Check for changelog fragment
run: |
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
echo "❌ ERROR: This PR is from a fork repository."
echo "PRs must be created from branches in the main PolicyEngine/policyengine-us-data repository."
echo "Please close this PR and create a new one following these steps:"
echo "1. git checkout main"
echo "2. git pull upstream main"
echo "3. git checkout -b your-branch-name"
echo "4. git push -u upstream your-branch-name"
echo "5. Create PR from the upstream branch"
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).<type>.md"
echo "Types: added, changed, fixed, removed, breaking"
exit 1
fi
echo "✅ PR is from the correct repository"

require-entry:
needs: check-fork
uses: ./.github/workflows/reusable_changelog_check.yaml
with:
require_entry: true
validate_format: true
45 changes: 0 additions & 45 deletions .github/workflows/reusable_changelog_check.yaml

This file was deleted.

15 changes: 9 additions & 6 deletions .github/workflows/versioning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- main

paths:
- changelog_entry.yaml
- "changelog.d/**"
- "!pyproject.toml"

jobs:
Expand All @@ -19,20 +19,23 @@ jobs:
uses: actions/checkout@v4
with:
token: ${{ secrets.POLICYENGINE_GITHUB }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Build changelog
run: pip install yaml-changelog && make changelog
- name: Install towncrier
run: pip install towncrier
- name: Bump version and build changelog
run: |
python .github/bump_version.py
towncrier build --yes --version $(python -c "import re; print(re.search(r'version = \"(.+?)\"', open('pyproject.toml').read()).group(1))")
- name: Update lockfile
run: uv lock
- name: Preview changelog update
run: ".github/get-changelog-diff.sh"
- name: Update changelog
uses: EndBug/add-and-commit@v9
with:
add: "."
message: Update package version
message: Update package version
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ install:
pip install -e ".[dev]" --config-settings editable_mode=compat

changelog:
build-changelog changelog.yaml --output changelog.yaml --update-last-date --start-from 1.0.0 --append-file changelog_entry.yaml
build-changelog changelog.yaml --org PolicyEngine --repo policyengine-us-data --output CHANGELOG.md --template .github/changelog_template.md
bump-version changelog.yaml pyproject.toml
rm changelog_entry.yaml || true
touch changelog_entry.yaml

python .github/bump_version.py
towncrier build --yes --version $$(python -c "import re; print(re.search(r'version = \"(.+?)\"', open('pyproject.toml').read()).group(1))")
download:
python policyengine_us_data/storage/download_private_prerequisites.py

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions changelog.d/migrate-to-towncrier.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrated from changelog_entry.yaml to towncrier fragments to eliminate merge conflicts.
36 changes: 35 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ dev = [
"yaml-changelog>=0.1.7",
"build",
"tomli",
"itables",
"itables", "towncrier>=24.8.0",

]

[tool.setuptools]
Expand Down Expand Up @@ -98,3 +99,36 @@ extend-exclude = '''
| dist
)/
'''

[tool.towncrier]
package = "policyengine_us_data"
directory = "changelog.d"
filename = "CHANGELOG.md"
title_format = "## [{version}] - {project_date}"
issue_format = ""
underlines = ["", "", ""]

[[tool.towncrier.type]]
directory = "breaking"
name = "Breaking changes"
showcontent = true

[[tool.towncrier.type]]
directory = "added"
name = "Added"
showcontent = true

[[tool.towncrier.type]]
directory = "changed"
name = "Changed"
showcontent = true

[[tool.towncrier.type]]
directory = "fixed"
name = "Fixed"
showcontent = true

[[tool.towncrier.type]]
directory = "removed"
name = "Removed"
showcontent = true
15 changes: 15 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.