diff --git a/.github/workflows/pre-commit-update.yml b/.github/workflows/pre-commit-update.yml new file mode 100644 index 00000000..ad8c5448 --- /dev/null +++ b/.github/workflows/pre-commit-update.yml @@ -0,0 +1,96 @@ +name: Update pre-commit + +####### +# Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR. +####### + +on: + workflow_call: + inputs: + pre-commit-source: + description: "The arguments for `pip install` to install pre-commit; use ./path/to/package[dev] for the repo's pinned version." + default: '.[dev]' + type: string + create-changenote: + description: "If 'true', creates a misc changenote in the './changes' directory." + default: true + type: boolean + +permissions: + pull-requests: write + +env: + BRANCH_NAME: "autoupdates/pre-commit" + CHANGENOTE_DIR: "./changes" + +jobs: + pre-commit-update: + name: Update pre-commit + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 1 + token: ${{ secrets.BRUTUS_PAT_TOKEN }} + + - name: Configure git + run: | + git config user.email "brutus@beeware.org" + git config user.name "Brutus (robot)" + + - name: Set up Python + uses: actions/setup-python@v4.5.0 + with: + python-version: 3.X + + - name: Install pre-commit + run: python -m pip install ${{ inputs.pre-commit-source }} + + - name: Update pre-commit hooks + run: | + pre-commit autoupdate + pre-commit install-hooks + + - name: Run pre-commit + # pre-commit returns non-zero when files are changed and fails the job + continue-on-error: true + run: pre-commit run --all-files + + - name: PR Needed? + id: pr + run: | + if [[ $(git status --porcelain) ]]; then + echo "needed=true" >> ${GITHUB_OUTPUT} + else + echo "needed=false" >> ${GITHUB_OUTPUT} + fi + + - name: Create Pull Request + id: created-pr + if: steps.pr.outputs.needed == 'true' + uses: peter-evans/create-pull-request@v4.2.3 + with: + token: ${{ secrets.BRUTUS_PAT_TOKEN }} + title: "Auto-update `pre-commit` hooks" + branch: ${{ env.BRANCH_NAME }} + commit-message: "Auto-updated `pre-commit` hooks." + committer: "Brutus (robot) " + author: "Brutus (robot) " + body: "Updated `pre-commit` hooks to their latest versions and ran updated hooks against the repo." + labels: "dependencies" + + - name: Add changenote + if: (inputs.create-changenote == true) && (steps.created-pr.outputs.pull-request-number != '') + run: | + git fetch origin + git checkout ${{ env.BRANCH_NAME }} + + FILENAME="${{ env.CHANGENOTE_DIR }}/${{ steps.created-pr.outputs.pull-request-number }}.misc.rst" + printf 'Updated ``pre-commit`` hooks to the latest version.\n' > "${FILENAME}" + + git add "${FILENAME}" + git commit -m "Add changenote." + git push --set-upstream origin ${{ env.BRANCH_NAME }}