Skip to content

Update pre-commit

Update pre-commit #95

name: Update pre-commit
#######
# Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR.
#######
on:
schedule:
- cron: "0 20 * * SUN" # Sunday @ 2000 UTC
workflow_dispatch:
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: "Defaults 'true' to create a misc changenote in the './changes' directory."
default: true
type: boolean
secrets:
BRUTUS_PAT_TOKEN:
required: true
permissions:
pull-requests: write
env:
BRANCH_PREFIX: "autoupdates/pre-commit"
CHANGENOTE_DIR: "./changes"
FORCE_COLOR: "1"
defaults:
run:
shell: bash
jobs:
pre-commit-hooks:
name: Pre-commit hooks
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
hook-repos: ${{ steps.hooks.outputs.repos }}
steps:
- name: Checkout Repo
uses: actions/checkout@v4.2.2
- name: Get Pre-commit Hooks
id: hooks
run: |
REPOS=$(yq .repos[].repo < .pre-commit-config.yaml | jq -R -s -c 'split("\n")[:-1]')
echo "repos=${REPOS}" >> ${GITHUB_OUTPUT}
pre-commit-update:
name: Update Pre-commit hook
needs: pre-commit-hooks
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
hook-repo: ${{ fromJson(needs.pre-commit-hooks.outputs.hook-repos) }}
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
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@v5.3.0
with:
python-version: "3.x"
cache: pip
cache-dependency-path: |
**/setup.cfg
**/pyproject.toml
.pre-commit-config.yaml
- name: Install pre-commit
run: python -m pip install ${{ inputs.pre-commit-source || 'pre-commit' }}
- name: Update pre-commit hooks
id: update
run: |
pre-commit autoupdate --repo ${{ matrix.hook-repo }} | tee update.log
echo "vers-bump-str=$(grep -ohE '([^ ]+ -> [^ ]+)' update.log | sed 's/->/to/')" >> ${GITHUB_OUTPUT}
rm -f update.log
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
REPO="${{ matrix.hook-repo }}"
echo "hook-name=${REPO##*/}" >> ${GITHUB_OUTPUT}
- name: Create Pull Request
id: created-pr
if: steps.pr.outputs.needed == 'true'
uses: peter-evans/create-pull-request@v7.0.5
with:
token: ${{ secrets.BRUTUS_PAT_TOKEN }}
title: "Bump ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
branch: "${{ env.BRANCH_PREFIX }}/${{ steps.pr.outputs.hook-name }}"
commit-message: "Bump ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
committer: "Brutus (robot) <brutus@beeware.org>"
author: "Brutus (robot) <brutus@beeware.org>"
body: "Bumps `pre-commit` hook for `${{ steps.pr.outputs.hook-name }}` from ${{ steps.update.outputs.vers-bump-str }} and ran the update against the repo."
labels: "dependencies"
- name: Add changenote
if: (inputs.create-changenote == true) && (steps.created-pr.outputs.pull-request-number != '')
run: |
BRANCH_NAME="${{ env.BRANCH_PREFIX }}/${{ steps.pr.outputs.hook-name }}"
git fetch origin
git checkout "${BRANCH_NAME}"
FILENAME="${{ env.CHANGENOTE_DIR }}/${{ steps.created-pr.outputs.pull-request-number }}.misc.rst"
printf 'The ``pre-commit`` hook for ``${{ steps.pr.outputs.hook-name }}`` was updated to its latest version.\n' > "${FILENAME}"
git add "${FILENAME}"
git commit -m "Add changenote."
git push --set-upstream origin "${BRANCH_NAME}"