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
74 changes: 74 additions & 0 deletions .github/bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""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()
30 changes: 18 additions & 12 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ jobs:

- name: Run tests
run: |
uv run pytest policyengine_au/tests -v --cov=policyengine_au --cov-report=xml

- name: Upload coverage to Codecov
if: matrix.python-version == '3.13'
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
uv run pytest policyengine_au/tests -v

documentation:
name: Build Documentation
Expand All @@ -95,7 +86,7 @@ jobs:

- name: Build documentation
run: |
myst build docs -o docs/_build
myst build docs --html

yaml-tests:
name: YAML Policy Tests
Expand All @@ -120,4 +111,19 @@ jobs:

- name: Run YAML tests
run: |
uv run pytest policyengine_au/tests/policy -v
uv run pytest policyengine_au/tests/policy -v

check-changelog:
name: Check changelog fragment
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check for changelog fragment
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).<type>.md"
echo "Types: added, changed, fixed, removed, breaking"
exit 1
fi
55 changes: 39 additions & 16 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ jobs:
black . -l 79 --check
linecheck . --check

- name: Run all tests with coverage
- name: Run tests
run: |
uv run pytest policyengine_au/tests -v --cov=policyengine_au --cov-report=xml --cov-report=term-missing

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
uv run pytest policyengine_au/tests -v

- name: Build package
run: |
Expand All @@ -52,34 +44,65 @@ jobs:
pip install twine
twine check dist/*

versioning:
name: Update version and changelog
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
needs: lint-test-build

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.13"

- name: Build changelog
run: |
pip install towncrier
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: Commit and push changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git diff --staged --quiet || git commit -m "Update changelog and version [skip ci]"
git push

deploy-docs:
name: Deploy Documentation
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
needs: lint-test-build

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.13"

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"

- name: Install package with docs dependencies
run: |
uv pip install --system -e .[dev]

- name: Build documentation
run: |
myst build docs

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: github.ref == 'refs/heads/main'
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ build:
python -m build

changelog:
build-changelog changelog.yaml --output CHANGELOG.md --start-from 0.1.0
python .github/bump_version.py
towncrier build --yes --version $$(python -c "import re; print(re.search(r'version = \"(.+?)\"', open('pyproject.toml').read()).group(1))")

clean:
find . -type f -name "*.pyc" -delete
Expand Down
Empty file added changelog.d/.gitkeep
Empty file.
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 @@
Migrate from changelog.yaml to towncrier fragments.
22 changes: 0 additions & 22 deletions changelog.yaml

This file was deleted.

34 changes: 33 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dev = [
"plotly>=5.19.0",
"ipykernel>=6.29.5",
"ipywidgets>=8.1.5",
"towncrier>=24.0.0",
]

[project.urls]
Expand Down Expand Up @@ -95,4 +96,35 @@ exclude = [
"**/*.pyo",
"**/.DS_Store",
"**/Thumbs.db",
]
]

[tool.towncrier]
directory = "changelog.d"
filename = "CHANGELOG.md"
package_dir = "."
title_format = "## {version}"

[[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
Loading