Skip to content

Commit e21fbfe

Browse files
authored
Merge pull request #136 from PolicyEngine/feat/country-updater
feat: automated country package update workflow
2 parents e37f1d5 + 00ef515 commit e21fbfe

5 files changed

Lines changed: 118 additions & 9 deletions

File tree

.github/scripts/update-package.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Check if a Python package has a newer version on PyPI than what's
4+
# locked in uv.lock. If so, upgrade the lockfile and open a PR.
5+
#
6+
# Usage: ./update-package.sh <package-name>
7+
#
8+
# Requires: uv, gh (GitHub CLI), curl, jq, git
9+
# Environment: GH_TOKEN must be set for gh CLI
10+
11+
set -euo pipefail
12+
13+
PACKAGE="${1:?Usage: update-package.sh <package-name>}"
14+
15+
# 1. Get current locked version from uv.lock
16+
CURRENT=$(grep -A1 "^name = \"${PACKAGE}\"$" uv.lock | grep 'version' | head -1 | sed 's/.*"\(.*\)"/\1/')
17+
if [[ -z "$CURRENT" ]]; then
18+
echo "ERROR: Package '${PACKAGE}' not found in uv.lock"
19+
exit 1
20+
fi
21+
echo "Current locked version: ${PACKAGE}==${CURRENT}"
22+
23+
# 2. Get latest version from PyPI
24+
LATEST=$(curl -sf "https://pypi.org/pypi/${PACKAGE}/json" | jq -r .info.version)
25+
if [[ -z "$LATEST" ]]; then
26+
echo "ERROR: Could not fetch latest version for '${PACKAGE}' from PyPI"
27+
exit 1
28+
fi
29+
echo "Latest PyPI version: ${PACKAGE}==${LATEST}"
30+
31+
# 3. Compare
32+
if [[ "$CURRENT" == "$LATEST" ]]; then
33+
echo "Already up to date. Nothing to do."
34+
exit 0
35+
fi
36+
echo "Update available: ${CURRENT} -> ${LATEST}"
37+
38+
# 4. Check if a PR already exists for this package+version
39+
EXISTING_PR=$(gh pr list --search "in:title update ${PACKAGE} to ${LATEST}" --state open --json number --jq '.[0].number' 2>/dev/null || true)
40+
if [[ -n "$EXISTING_PR" ]]; then
41+
echo "PR #${EXISTING_PR} already exists for this update. Skipping."
42+
exit 0
43+
fi
44+
45+
# 5. Configure git author
46+
git config user.name "github-actions[bot]"
47+
git config user.email "github-actions[bot]@users.noreply.github.com"
48+
49+
# 6. Update pinned version in pyproject.toml and upgrade lockfile
50+
echo "Updating ${PACKAGE} pin in pyproject.toml: ==${CURRENT} -> ==${LATEST}"
51+
sed -i "s/\"${PACKAGE}==${CURRENT}\"/\"${PACKAGE}==${LATEST}\"/" pyproject.toml
52+
53+
echo "Running: uv lock --upgrade-package ${PACKAGE}"
54+
uv lock --upgrade-package "${PACKAGE}"
55+
56+
if git diff --quiet uv.lock pyproject.toml; then
57+
echo "No changes after upgrade. Nothing to do."
58+
exit 0
59+
fi
60+
61+
# 7. Create branch, commit, push, open PR
62+
BRANCH="auto/update-${PACKAGE}-${LATEST}"
63+
git checkout -b "$BRANCH"
64+
git add uv.lock pyproject.toml
65+
git commit -m "chore(deps): update ${PACKAGE} to ${LATEST}"
66+
git push -u origin "$BRANCH"
67+
68+
gh pr create \
69+
--title "chore(deps): update ${PACKAGE} to ${LATEST}" \
70+
--body "Update ${PACKAGE} to ${LATEST}"
71+
72+
echo "PR created for ${PACKAGE} ${CURRENT} -> ${LATEST}"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Update country packages
2+
3+
on:
4+
schedule:
5+
- cron: "*/30 * * * *" # Every 30 minutes
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
update:
14+
name: Update ${{ matrix.package }}
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
package: [policyengine-us, policyengine-uk]
19+
fail-fast: false
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Install uv
26+
uses: astral-sh/setup-uv@v5
27+
28+
- name: Setup Python
29+
run: uv python install 3.13
30+
31+
- name: Check for update and open PR
32+
env:
33+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
run: |
35+
chmod +x .github/scripts/update-package.sh
36+
.github/scripts/update-package.sh ${{ matrix.package }}

changelog.d/135.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Automated GitHub Actions workflow to check for country package updates and open PRs.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ dependencies = [
1111
"psycopg2-binary>=2.9.10",
1212
"supabase>=2.10.0",
1313
"storage3>=0.8.1",
14-
"policyengine>=3.2.1",
15-
"policyengine-uk>=2.75.1",
16-
"policyengine-us>=1.592.4",
14+
"policyengine>=3.2.3",
15+
"policyengine-uk==2.75.1",
16+
"policyengine-us==1.592.4",
1717
"pydantic>=2.9.2",
1818
"pydantic-settings>=2.6.0",
1919
"rich>=13.9.4",

uv.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)