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
77 changes: 77 additions & 0 deletions .github/bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""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()
10 changes: 10 additions & 0 deletions .github/fetch_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Print the package version from pyproject.toml (used to tag releases)."""

import re
from pathlib import Path

text = (Path(__file__).resolve().parent.parent / "pyproject.toml").read_text()
match = re.search(r'^version\s*=\s*"(.+?)"', text, re.MULTILINE)
if match is None:
raise SystemExit("Could not find version in pyproject.toml")
print(match.group(1))
4 changes: 4 additions & 0 deletions .github/publish-git-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/env bash

git tag `python .github/fetch_version.py` # create a new tag
git push --tags || true # update the repository version
21 changes: 21 additions & 0 deletions .github/workflows/changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Changelog

on:
pull_request:
branches: [ main ]

jobs:
check-changelog:
name: Check changelog fragment
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- 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
30 changes: 30 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI
on:
push:
branches: [ main ]

jobs:
Test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.13"]
steps:
- name: Checkout repo
uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v8.1.0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv pip install -e ".[dev]" --system
- name: Run tests with coverage
run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v6
with:
files: ./coverage.xml
fail_ci_if_error: false
verbose: true
62 changes: 62 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Pull request
on:
pull_request:
branches: [ main ]

jobs:
Lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: "3.13"
- name: Install uv
uses: astral-sh/setup-uv@v8.1.0
- name: Install ruff
run: uv pip install ruff --system
- name: Check formatting and lint
run: make check-format

Test:
strategy:
matrix:
python-version: ["3.11", "3.13", "3.14"]
fail-fast: false
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v8.1.0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: uv pip install -e ".[dev]" --system
- name: Run tests with coverage
run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v6
with:
files: ./coverage.xml
fail_ci_if_error: false
verbose: true

Build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v8.1.0
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: "3.13"
- name: Install dependencies
run: uv pip install -e ".[dev]" --system
- name: Build package
run: make build
70 changes: 70 additions & 0 deletions .github/workflows/versioning.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Workflow that runs on versioning metadata updates.

name: Versioning updates
on:
push:
branches:
- main

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

jobs:
Versioning:
runs-on: ubuntu-latest
if: |
(!(github.event.head_commit.message == 'Update package version'))
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@v3
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Checkout repo
uses: actions/checkout@v6
with:
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v6
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: Update changelog
uses: EndBug/add-and-commit@v10
with:
add: "."
message: Update package version
github_token: ${{ steps.app-token.outputs.token }}
fetch: false
publish-to-pypi:
name: Publish to PyPI
if: (github.event.head_commit.message == 'Update package version')
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # Fetch all history for all tags and branches
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: 3.13
- name: Install package
run: make install
- name: Build package
run: python -m build
- name: Publish a git tag
run: ".github/publish-git-tag.sh || true"
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI }}
skip-existing: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ __pycache__/
dist/
build/
*.egg-info/
.coverage
coverage.xml
htmlcov/
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- towncrier release notes start -->
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 PolicyEngine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
install:
pip install -e ".[dev]"

test:
pytest tests/ --cov=microunit --cov-report=xml --maxfail=0 -v

check-format:
ruff check .
ruff format --check .

format:
ruff check --fix .
ruff format .

build:
pip install build
python -m build

changelog:
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:
rm -rf dist/ build/ *.egg-info/
Loading
Loading