|
| 1 | +name: Update Pre-commit Hooks |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run every Sunday at 11:00 AM UTC (4:00 AM Pacific) |
| 6 | + - cron: 0 11 * * 0 |
| 7 | + workflow_dispatch: # Allow manual trigger |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + update-hooks: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + timeout-minutes: 30 |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v6 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v6 |
| 26 | + with: |
| 27 | + python-version: '3.12' |
| 28 | + |
| 29 | + - name: Install uv |
| 30 | + uses: astral-sh/setup-uv@v6 |
| 31 | + |
| 32 | + - name: Update pre-commit hooks |
| 33 | + run: | |
| 34 | + # Store original config for comparison |
| 35 | + cp .pre-commit-config.yaml .pre-commit-config.yaml.bak |
| 36 | +
|
| 37 | + # Update hooks to latest versions |
| 38 | + make update-hooks |
| 39 | +
|
| 40 | + # Check if there are any changes |
| 41 | + if ! diff -q .pre-commit-config.yaml .pre-commit-config.yaml.bak > /dev/null; then |
| 42 | + echo "HOOKS_UPDATED=true" >> "$GITHUB_ENV" |
| 43 | + echo "Pre-commit hooks have been updated" |
| 44 | + else |
| 45 | + echo "HOOKS_UPDATED=false" >> "$GITHUB_ENV" |
| 46 | + echo "No pre-commit hook updates available" |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Validate hook updates |
| 50 | + if: env.HOOKS_UPDATED == 'true' |
| 51 | + run: | |
| 52 | + echo "Running pre-commit hooks to validate updates..." |
| 53 | +
|
| 54 | + # First run - often fails but makes auto-fixes |
| 55 | + if make run-hooks-all-files; then |
| 56 | + echo "HOOKS_VALID=true" >> "$GITHUB_ENV" |
| 57 | + echo "All pre-commit hooks passed validation on first run" |
| 58 | + else |
| 59 | + echo "Pre-commit hooks failed on first run (expected - likely auto-fixing)" |
| 60 | +
|
| 61 | + # Stage the pre-commit config |
| 62 | + git add .pre-commit-config.yaml |
| 63 | +
|
| 64 | + # Second run - should pass after auto-fixes |
| 65 | + if make run-hooks-all-files; then |
| 66 | + echo "HOOKS_VALID=true" >> "$GITHUB_ENV" |
| 67 | + echo "All pre-commit hooks passed validation on second run" |
| 68 | + else |
| 69 | + echo "HOOKS_VALID=false" >> "$GITHUB_ENV" |
| 70 | + echo "Pre-commit hooks failed validation - manual intervention required" |
| 71 | + fi |
| 72 | + fi |
| 73 | +
|
| 74 | + - name: Create Pull Request |
| 75 | + if: env.HOOKS_UPDATED == 'true' && env.HOOKS_VALID == 'true' |
| 76 | + run: | |
| 77 | + # Configure git |
| 78 | + git config user.name "github-actions[bot]" |
| 79 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 80 | +
|
| 81 | + # Create a new branch |
| 82 | + BRANCH_NAME="chore/update-precommit-hooks-$(date +%Y%m%d)" |
| 83 | + git checkout -b "$BRANCH_NAME" |
| 84 | +
|
| 85 | + # Stage the pre-commit config file |
| 86 | + git add .pre-commit-config.yaml |
| 87 | +
|
| 88 | + # Commit the changes |
| 89 | + git commit -m "$(cat <<'EOF' |
| 90 | + chore: update pre-commit hooks to latest versions |
| 91 | +
|
| 92 | + 🤖 Generated with GitHub Actions |
| 93 | + EOF |
| 94 | + )" |
| 95 | +
|
| 96 | + # Push the branch |
| 97 | + git push origin "$BRANCH_NAME" |
| 98 | +
|
| 99 | + # Create PR using gh CLI |
| 100 | + gh pr create \ |
| 101 | + --title "chore: update pre-commit hooks to latest versions" \ |
| 102 | + --body "$(cat <<'EOF' |
| 103 | + ## Summary |
| 104 | + Weekly automated update of pre-commit hooks to their latest versions. |
| 105 | +
|
| 106 | + ## Changes |
| 107 | + - Updated pre-commit hook versions in `.pre-commit-config.yaml` |
| 108 | +
|
| 109 | + ## Test plan |
| 110 | + - [ ] Verify hooks pass locally: `make run-hooks-all-files` |
| 111 | + EOF |
| 112 | + )" |
| 113 | + env: |
| 114 | + GH_TOKEN: ${{ github.token }} |
| 115 | + |
| 116 | + - name: Summary |
| 117 | + if: always() |
| 118 | + run: | |
| 119 | + { |
| 120 | + echo "## Pre-commit Hook Update Summary" |
| 121 | + echo "- **Date**: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" |
| 122 | + echo "- **Hooks Updated**: ${{ env.HOOKS_UPDATED }}" |
| 123 | +
|
| 124 | + if [ "${{ env.HOOKS_UPDATED }}" = "true" ]; then |
| 125 | + echo "- **Hooks Valid**: ${{ env.HOOKS_VALID }}" |
| 126 | + if [ "${{ env.HOOKS_VALID }}" = "true" ]; then |
| 127 | + echo "- **Status**: PR created with hook updates" |
| 128 | + else |
| 129 | + echo "- **Status**: Hook updates failed validation" |
| 130 | + fi |
| 131 | + else |
| 132 | + echo "- **Status**: All hooks already up to date" |
| 133 | + fi |
| 134 | + } >> "$GITHUB_STEP_SUMMARY" |
| 135 | +
|
| 136 | + - name: Clean up |
| 137 | + if: always() |
| 138 | + run: rm -f .pre-commit-config.yaml.bak |
0 commit comments