Skip to content

Update pre-commit config #103

Update pre-commit config

Update pre-commit config #103

name: "Update pre-commit config"
on:
schedule:
- cron: "48 3 * * 1"
workflow_dispatch:
permissions:
pull-requests: write
contents: write
jobs:
update-pre-commit-config:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: python3 -m pip install pre-commit
- name: Hash existing config file
run: sha256sum .pre-commit-config.yaml > ${{ runner.temp }}/config_file_hashes
- name: Update pre-commit config
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git switch -c "pre-commit-update"
pre-commit autoupdate --freeze
- name: Generate GitHub App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.AUTH_APP_ID }}
private-key: ${{ secrets.AUTH_APP_PRIVATE_KEY }}
- name: Create pull requests
run: |
if $(sha256sum --status -c ${{ runner.temp }}/config_file_hashes); then
echo "Config file unchanged. Skipping pull request..."
exit 0
fi
git add .pre-commit-config.yaml
git commit -m "[CI] Update .pre-commit-config.yaml"
remote_branch_name="pre-commit-$(sha256sum .pre-commit-config.yaml | head -c 8)"
git push --set-upstream origin "pre-commit-update:${remote_branch_name}"
# Create PR on GitHub using GitHub REST API.
request_body='{"title":"[CI] Update pre-commit hooks","body":"Created automatically by GitHub Actions.","base":"main","head":"'"${remote_branch_name}"'"}'
curl -LsS --fail-with-body \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ steps.app-token.outputs.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/pulls \
-d "$request_body"