Skip to content

Update pre-commit

Update pre-commit #286

name: Update pre-commit
#######
# Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR.
#######
on:
schedule:
- cron: "16 8 1 * *" # 0816 UTC on the 1st
workflow_dispatch:
workflow_call:
permissions:
pull-requests: write
env:
BRANCH_PREFIX: "autoupdates/pre-commit"
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}"
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.PAT }}
- name: Configure git
run: |
git config user.email "nobody@example.org"
git config user.name "pre-commit updates"
- name: Set up Python
uses: actions/setup-python@v5.3.0
with:
python-version: 3.X
- name: Install pre-commit
run: python -m pip install .[dev]
- 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}
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.PAT }}
title: "Bump pre-commit hook for ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
branch: "${{ env.BRANCH_PREFIX }}/${{ steps.pr.outputs.hook-name }}"
commit-message: "Bumped pre-commit hook for ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
committer: "pre-commit updater <noreply@example.org>"
author: "pre-commit updater <noreply@example.org>"
body: "Automatically bumped `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"