Skip to content

Commit 0e411c8

Browse files
MaxGhenisclaude
andauthored
Migrate to pyproject.toml and towncrier fragments (#546)
* Migrate to pyproject.toml and towncrier fragments Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Format bump_version.py with black Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix Makefile and dev dependencies after setup.py removal Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Delete old changelog files --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7b957c4 commit 0e411c8

9 files changed

Lines changed: 191 additions & 750 deletions

File tree

.github/bump_version.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Infer semver bump from towncrier fragment types and update version."""
2+
3+
import re
4+
import sys
5+
from pathlib import Path
6+
7+
8+
def get_current_version(pyproject_path: Path) -> str:
9+
text = pyproject_path.read_text()
10+
match = re.search(r'^version\s*=\s*"(\d+\.\d+\.\d+)"', text, re.MULTILINE)
11+
if not match:
12+
print(
13+
"Could not find version in pyproject.toml",
14+
file=sys.stderr,
15+
)
16+
sys.exit(1)
17+
return match.group(1)
18+
19+
20+
def infer_bump(changelog_dir: Path) -> str:
21+
fragments = [
22+
f
23+
for f in changelog_dir.iterdir()
24+
if f.is_file() and f.name != ".gitkeep"
25+
]
26+
if not fragments:
27+
print("No changelog fragments found", file=sys.stderr)
28+
sys.exit(1)
29+
30+
categories = {f.suffix.lstrip(".") for f in fragments}
31+
for f in fragments:
32+
parts = f.stem.split(".")
33+
if len(parts) >= 2:
34+
categories.add(parts[-1])
35+
36+
if "breaking" in categories:
37+
return "major"
38+
if "added" in categories or "removed" in categories:
39+
return "minor"
40+
return "patch"
41+
42+
43+
def bump_version(version: str, bump: str) -> str:
44+
major, minor, patch = (int(x) for x in version.split("."))
45+
if bump == "major":
46+
return f"{major + 1}.0.0"
47+
elif bump == "minor":
48+
return f"{major}.{minor + 1}.0"
49+
else:
50+
return f"{major}.{minor}.{patch + 1}"
51+
52+
53+
def update_file(path: Path, old_version: str, new_version: str):
54+
text = path.read_text()
55+
updated = text.replace(
56+
f'version = "{old_version}"',
57+
f'version = "{new_version}"',
58+
)
59+
if updated != text:
60+
path.write_text(updated)
61+
print(f" Updated {path}")
62+
63+
64+
def main():
65+
root = Path(__file__).resolve().parent.parent
66+
pyproject = root / "pyproject.toml"
67+
changelog_dir = root / "changelog.d"
68+
69+
current = get_current_version(pyproject)
70+
bump = infer_bump(changelog_dir)
71+
new = bump_version(current, bump)
72+
73+
print(f"Version: {current} -> {new} ({bump})")
74+
75+
update_file(pyproject, current, new)
76+
77+
78+
if __name__ == "__main__":
79+
main()

.github/workflows/pr.yaml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@ jobs:
1111
uses: "lgeiger/black-action@master"
1212
with:
1313
args: ". -l 79 --check"
14-
check-version:
15-
name: Check version
14+
check-changelog:
15+
name: Check changelog fragment
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: actions/checkout@v3
19-
with:
20-
fetch-depth: 0
21-
repository: ${{ github.event.pull_request.head.repo.full_name }}
22-
ref: ${{ github.event.pull_request.head.ref }}
23-
- name: Set up Python
24-
uses: actions/setup-python@v4
25-
with:
26-
python-version: 3.13
27-
- name: Build changelog
28-
run: pip install yaml-changelog==0.3.0 && make changelog
29-
- name: Preview changelog update
30-
run: ".github/get-changelog-diff.sh"
31-
- name: Check version number has been properly updated
32-
run: .github/is-version-number-acceptable.sh
18+
- uses: actions/checkout@v4
19+
- name: Check for changelog fragment
20+
run: |
21+
FRAGMENTS=$(find changelog.d -type f ! -name '.gitkeep' | wc -l)
22+
if [ "$FRAGMENTS" -eq 0 ]; then
23+
echo "::error::No changelog fragment found in changelog.d/"
24+
echo "Add one with: echo 'Description.' > changelog.d/\$(git branch --show-current).<type>.md"
25+
echo "Types: added, changed, fixed, removed, breaking"
26+
exit 1
27+
fi
3328
Test:
3429
runs-on: ${{ matrix.os }}
3530
strategy:

.github/workflows/push.yaml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ jobs:
2323
steps:
2424
- name: Checkout repo
2525
uses: actions/checkout@v3
26-
with:
27-
repository: ${{ github.event.pull_request.head.repo.full_name }}
28-
ref: ${{ github.event.pull_request.head.ref }}
29-
token: ${{ secrets.POLICYENGINE_GITHUB }}
26+
with: token: ${{ secrets.POLICYENGINE_GITHUB }}
3027
- name: Setup Python
3128
uses: actions/setup-python@v4
3229
with:
3330
python-version: 3.13
3431
- name: Build changelog
35-
run: pip install yaml-changelog==0.3.0 && make changelog
3632
- name: Preview changelog update
3733
run: ".github/get-changelog-diff.sh"
3834
- name: Update changelog
@@ -107,10 +103,7 @@ jobs:
107103
steps:
108104
- name: Checkout repo
109105
uses: actions/checkout@v2
110-
with:
111-
repository: ${{ github.event.pull_request.head.repo.full_name }}
112-
ref: ${{ github.event.pull_request.head.ref }}
113-
token: ${{ secrets.POLICYENGINE_GITHUB }}
106+
with: token: ${{ secrets.POLICYENGINE_GITHUB }}
114107
- name: Setup Python
115108
uses: actions/setup-python@v2
116109
with:

Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ test:
1515
policyengine-core test -c policyengine_canada policyengine_canada/tests
1616

1717
build:
18-
python setup.py sdist bdist_wheel
18+
python -m build
1919

2020
changelog:
21-
build-changelog changelog.yaml --output changelog.yaml --update-last-date --start-from 0.0.0 --append-file changelog_entry.yaml
22-
build-changelog changelog.yaml --org PolicyEngine --repo policyengine-canada --output CHANGELOG.md --template .github/changelog_template.md
23-
bump-version changelog.yaml setup.py
24-
rm changelog_entry.yaml || true
25-
touch changelog_entry.yaml
21+
python .github/bump_version.py
22+
towncrier build --yes --version $$(python -c "import re; print(re.search(r'version = \"(.+?)\"', open('pyproject.toml').read()).group(1))")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Migrated from changelog_entry.yaml to towncrier fragments to eliminate merge conflicts.

0 commit comments

Comments
 (0)