|
| 1 | +--- |
| 2 | +name: Autoupdate pre-commit config |
| 3 | + |
| 4 | +on: |
| 5 | + schedule: |
| 6 | + - cron: "0 0 * * 0" |
| 7 | + |
| 8 | +jobs: |
| 9 | + pre_commit_autoupdate: |
| 10 | + env: |
| 11 | + config_path: .pre-commit-config.yaml |
| 12 | + update_message_path: /tmp/message.txt |
| 13 | + update_branch: chore/pre-commit-autoupdate |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Generate a token |
| 17 | + id: generate-token |
| 18 | + uses: actions/create-github-app-token@v1 |
| 19 | + with: |
| 20 | + app-id: ${{ vars.WRITE_CONTENTS_PR_APP }} |
| 21 | + private-key: ${{ secrets.WRITE_CONTENTS_PR_KEY }} |
| 22 | + - uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + ref: main |
| 25 | + - name: Create or checkout update branch |
| 26 | + id: create_branch |
| 27 | + env: |
| 28 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 29 | + run: | |
| 30 | + export pr_number=$( gh pr view ${{ env.update_branch }} --json number --jq '.number' ) |
| 31 | + export pr_state=$( gh pr view ${{ env.update_branch }} --json state --jq '.state' ) |
| 32 | + echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" |
| 33 | + echo "pr_state=$pr_state" >> "$GITHUB_OUTPUT" |
| 34 | +
|
| 35 | + if git fetch origin ${{ env.update_branch }}; then |
| 36 | + # If branch wasn't deleted after merge, do so here |
| 37 | + if [ "$pr_state" = "MERGED" ]; then |
| 38 | + git push -d origin ${{ env.update_branch }} |
| 39 | + git checkout -b ${{ env.update_branch }} |
| 40 | + git push origin ${{ env.update_branch }} |
| 41 | + fi |
| 42 | + git checkout ${{ env.update_branch }} |
| 43 | + else |
| 44 | + git checkout -b ${{ env.update_branch }} |
| 45 | + git push origin ${{ env.update_branch }} |
| 46 | + fi |
| 47 | +
|
| 48 | + # Pre-commit setup and autoupdate steps |
| 49 | + - name: Set up Python |
| 50 | + uses: actions/setup-python@v5 |
| 51 | + with: |
| 52 | + python-version: 3.13 |
| 53 | + cache: pip |
| 54 | + - name: Upgrade Pip |
| 55 | + run: python -m pip install --upgrade pip |
| 56 | + - name: Install pre-commit |
| 57 | + run: pip install pre-commit |
| 58 | + - name: Store hash of baseline pre-commit config for comparison |
| 59 | + id: old_file |
| 60 | + run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT |
| 61 | + - name: Overwrite config on branch with version from main |
| 62 | + run: git checkout main ${{ env.config_path }} |
| 63 | + - name: Store hash of main pre-commit config for comparison |
| 64 | + id: main_file |
| 65 | + run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT |
| 66 | + - name: Run pre-commit autoupdate on main pre-commit config |
| 67 | + id: autoupdate |
| 68 | + run: | |
| 69 | + pre-commit autoupdate > ${{ env.update_message_path }} |
| 70 | + sed -i "/updating/!d" ${{ env.update_message_path }} |
| 71 | + - name: Store hash of new pre-commit config for comparison |
| 72 | + id: new_file |
| 73 | + run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT |
| 74 | + # Commit authoring and pull-request creation/updating |
| 75 | + - name: Commit (with signature) pre-commit config |
| 76 | + id: commit |
| 77 | + if: steps.old_file.outputs.hash != steps.new_file.outputs.hash |
| 78 | + env: |
| 79 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 80 | + run: | |
| 81 | + export message="chore(deps): autoupdate pre-commit hooks" |
| 82 | + export sha=$( git rev-parse ${{ env.update_branch }}:${{ env.config_path }} ) |
| 83 | + export content=$( base64 -i ${{ env.config_path }} ) |
| 84 | + gh api --method PUT /repos/:owner/:repo/contents/${{ env.config_path }} \ |
| 85 | + --field message="$message" \ |
| 86 | + --field content="$content" \ |
| 87 | + --field branch=${{ env.update_branch }} \ |
| 88 | + --field sha="$sha" |
| 89 | + - name: Create or update pre-commit-autoupdate pull-request |
| 90 | + if: steps.commit.conclusion == 'success' || ( steps.main_file.outputs.hash != steps.new_file.outputs.hash ) |
| 91 | + env: |
| 92 | + GH_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 93 | + run : | |
| 94 | + export title="chore(deps): autoupdate pre-commit hooks" |
| 95 | + export body=$( cat ${{ env.update_message_path }} ) |
| 96 | + export pr_number=${{ steps.create_branch.outputs.pr_number }} |
| 97 | + export pr_state=${{ steps.create_branch.outputs.pr_state }} |
| 98 | +
|
| 99 | + # If the PR is closed, can it be reopened, or is the PR already open? |
| 100 | + if ( [ "$pr_state" = "CLOSED" ] && gh pr reopen $pr_number ) || [ "$pr_state" = "OPEN" ]; then |
| 101 | + gh api --method PATCH /repos/:owner/:repo/pulls/$pr_number \ |
| 102 | + --field title="$title" \ |
| 103 | + --field body="$body" |
| 104 | + else |
| 105 | + # If a PR doesn't already exist, and no previous PR can be reopened, create a new PR. |
| 106 | + gh pr create -t "$title" -b "$body" -l dependencies -B main -H ${{ env.update_branch }} |
| 107 | + fi |
0 commit comments