Skip to content

Commit b571850

Browse files
committed
fix: sync release draft version to pyproject
1 parent e34878a commit b571850

2 files changed

Lines changed: 65 additions & 9 deletions

File tree

.github/workflows/publish-pypi.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: publish-pypi
22

33
on:
4+
release:
5+
types:
6+
- published
47
workflow_dispatch:
58
inputs:
69
upload:
@@ -22,19 +25,33 @@ jobs:
2225

2326
- name: Check package version
2427
run: |
25-
# Ensure the version in pyproject.toml is equivalent to the latest git tag
28+
set -euo pipefail
29+
30+
# Ensure the version in pyproject.toml matches the release tag being
31+
# published. For manual runs, fall back to the latest published
32+
# release tag.
2633
# Read version from pyproject.toml
2734
PACKAGE_VERSION=$(python -c "import tomllib; f = open('pyproject.toml', 'rb'); data = tomllib.load(f); f.close(); print(data['project']['version'])")
2835
echo "Package version: $PACKAGE_VERSION"
2936
30-
# Go to github releases to get latest tag
31-
LATEST_TAG=$(curl -s https://api.github.com/repos/httpdss/structkit/releases/latest | grep 'tag_name' | cut -d\" -f4)
32-
# Remove leading 'v' if present
33-
LATEST_TAG=${LATEST_TAG#v}
34-
echo "Latest tag: $LATEST_TAG"
37+
if [ "${GITHUB_EVENT_NAME}" = "release" ]; then
38+
RELEASE_TAG="${GITHUB_REF_NAME}"
39+
else
40+
RELEASE_TAG=$(python - <<'PY'
41+
import json
42+
import urllib.request
43+
44+
with urllib.request.urlopen("https://api.github.com/repos/httpdss/structkit/releases/latest") as response:
45+
print(json.load(response)["tag_name"])
46+
PY
47+
)
48+
fi
49+
50+
RELEASE_VERSION=${RELEASE_TAG#v}
51+
echo "Release version: $RELEASE_VERSION"
3552
36-
if [ "$PACKAGE_VERSION" != "$LATEST_TAG" ]; then
37-
echo "Error: Package version ($PACKAGE_VERSION) does not match latest git tag ($LATEST_TAG)"
53+
if [ "$PACKAGE_VERSION" != "$RELEASE_VERSION" ]; then
54+
echo "Error: Package version ($PACKAGE_VERSION) does not match release version ($RELEASE_VERSION)"
3855
exit 1
3956
fi
4057

.github/workflows/release-drafter.yaml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,50 @@ jobs:
1616
uses: actions/checkout@v5
1717
with:
1818
fetch-depth: 0
19-
- uses: release-drafter/release-drafter@v6.1.0
19+
- uses: release-drafter/release-drafter@v7.5.1
2020
id: release-drafter
2121
with:
2222
config-name: release-drafter.yml
2323
publish: false
2424
prerelease: false
2525
env:
2626
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
- name: Sync pyproject version to release draft
28+
env:
29+
RESOLVED_VERSION: ${{ steps.release-drafter.outputs.resolved_version }}
30+
run: |
31+
set -euo pipefail
32+
if [ -z "${RESOLVED_VERSION}" ]; then
33+
echo "Release Drafter did not resolve a version"
34+
exit 1
35+
fi
36+
37+
python - <<'PY'
38+
import os
39+
import re
40+
from pathlib import Path
41+
42+
version = os.environ["RESOLVED_VERSION"].removeprefix("v")
43+
path = Path("pyproject.toml")
44+
text = path.read_text()
45+
updated, count = re.subn(
46+
r'(?m)^(version\s*=\s*")[^"]+(")$',
47+
rf'\g<1>{version}\2',
48+
text,
49+
count=1,
50+
)
51+
if count == 0:
52+
raise SystemExit("pyproject.toml version field was not found")
53+
path.write_text(updated)
54+
PY
55+
56+
if git diff --quiet -- pyproject.toml; then
57+
echo "pyproject.toml already matches ${RESOLVED_VERSION}"
58+
exit 0
59+
fi
60+
61+
git config user.name "github-actions[bot]"
62+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
63+
git add pyproject.toml
64+
git commit -m "chore: bump pyproject version to ${RESOLVED_VERSION}"
65+
git push

0 commit comments

Comments
 (0)