|
| 1 | +name: Reusable Update pre-commit |
| 2 | +# Update pre-commit config and create PR if changes are detected |
| 3 | +# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI] |
| 4 | +# UoE editor: Alejandro Bordallo <[email protected]> |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_call: |
| 8 | + inputs: |
| 9 | + ref_for_scheduled_build: |
| 10 | + description: | |
| 11 | + 'Reference on which the repo should be checkout for scheduled build. |
| 12 | + Usually is this name of a branch or a tag.' |
| 13 | + default: '' |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + secrets: |
| 17 | + precommit-pr-token: |
| 18 | + description: 'PAT from GreatAlexander for PR auto-approval' |
| 19 | + required: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + auto_update_and_create_pr: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + env: |
| 25 | + # this will be src/{repo-owner}/{repo-name} |
| 26 | + path: src/${{ github.repository }} |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + fetch-depth: 0 |
| 33 | + path: ${{ env.path }} |
| 34 | + ref: ${{ github.event.inputs.ref_for_scheduled_build }} |
| 35 | + |
| 36 | + - name: Install pre-commit |
| 37 | + run: | |
| 38 | + sudo apt-get install -qq python3-venv |
| 39 | + python3 -m venv .venv |
| 40 | + source .venv/bin/activate |
| 41 | + python3 -m pip install pre-commit |
| 42 | +
|
| 43 | + - name: Auto-update with pre-commit |
| 44 | + run: | |
| 45 | + source .venv/bin/activate |
| 46 | + cd ${{ env.path }} |
| 47 | + pre-commit autoupdate || true # Ignoring errors |
| 48 | +
|
| 49 | + - name: Check for changes |
| 50 | + id: git_status |
| 51 | + run: | |
| 52 | + cd ${{ env.path }} |
| 53 | + git diff --quiet && echo "changed=false" >> $GITHUB_OUTPUT || echo "changed=true" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + - name: There are changes |
| 56 | + if: steps.git_status.outputs.changed == 'true' |
| 57 | + run: | |
| 58 | + cd ${{ env.path }} |
| 59 | + git diff --exit-code || true |
| 60 | +
|
| 61 | + - name: No changes! |
| 62 | + if: steps.git_status.outputs.changed == 'false' |
| 63 | + run: | |
| 64 | + echo "No changes detected" |
| 65 | +
|
| 66 | + - name: Create Pull Request |
| 67 | + id: cpr |
| 68 | + if: steps.git_status.outputs.changed == 'true' |
| 69 | + uses: peter-evans/create-pull-request@v7 |
| 70 | + with: |
| 71 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + branch: auto-update-${{ github.event.inputs.ref_for_scheduled_build }} |
| 73 | + base: main |
| 74 | + commit-message: Bump version of pre-commit hooks |
| 75 | + title: Bump version of pre-commit hooks |
| 76 | + body: This pull request contains auto-updated files of the pre-commit config. |
| 77 | + delete-branch: true |
| 78 | + draft: false |
| 79 | + path: ${{ env.path }} |
| 80 | + |
| 81 | + - name: Enable Pull Request Automerge |
| 82 | + if: steps.cpr.outputs.pull-request-operation == 'created' |
| 83 | + run: | |
| 84 | + cd ${{ env.path }} |
| 85 | + gh pr merge --squash --auto "${{ steps.cpr.outputs.pull-request-number }}" |
| 86 | + env: |
| 87 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + |
| 89 | + - name: Auto approve |
| 90 | + if: steps.cpr.outputs.pull-request-operation == 'created' |
| 91 | + run: | |
| 92 | + cd ${{ env.path }} |
| 93 | + gh pr review --approve "${{ steps.cpr.outputs.pull-request-number }}" |
| 94 | + env: |
| 95 | + GH_TOKEN: ${{ secrets.precommit-pr-token }} |
0 commit comments