replace pre-commit.ci by a local workflow (#61652)

* replace pre-commit.ci by a local workflow

* more commit history

* Update .github/workflows/pre-commit.yaml

Co-authored-by: Matthias Kuhn <matthias@opengis.ch>

* remove default

* use comment rather than label

---------

Co-authored-by: Matthias Kuhn <matthias@opengis.ch>
This commit is contained in:
Denis Rouzaud 2025-04-28 13:36:28 +02:00 committed by GitHub
parent a8dc05be67
commit 74f958a5f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 98 additions and 3 deletions

98
.github/workflows/pre-commit.yaml vendored Normal file
View File

@ -0,0 +1,98 @@
name: Pre-commit checks
on:
pull_request:
push:
issue_comment:
types: [created]
jobs:
pre-commit:
if: github.event_name != 'issue_comment' || github.event.comment.body == '/fix-precommit'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 200
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Install pre-commit
run: pip install pre-commit
- name: Run pre-commit checks
id: pre-commit
run: |
MODIFIED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | tr '\n' ' ')
if [ -n "$MODIFIED_FILES" ]; then
pre-commit run --files $MODIFIED_FILES || (echo "pre-commit failed. Attempting to auto-fix..." && exit 1)
else
pre-commit run --all-files
fi
- name: Auto-commit fixes
if: failure() && github.event.pull_request.head.repo.full_name == github.repository && github.event_name != 'issue_comment'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "auto-fix pre-commit issues" || echo "No changes to commit"
git push
- name: Create a new branch for pre-commit fixes
if: failure() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git checkout -b fix/pre-commit-fixes-${{ github.event.pull_request.number }}
git add .
git commit -m "auto-fix pre-commit issues"
git push -f --set-upstream origin fix/pre-commit-fixes-${{ github.event.pull_request.number }}
- name: Listen for `/fix-precommit` comment
if: failure() && github.event_name == 'issue_comment' && github.event.comment.body == '/fix-precommit' && github.event.issue.pull_request
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pullRequest = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const branchName = `fix/pre-commit-fixes-${context.issue.number}`;
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/heads/${branchName}`,
sha: pullRequest.data.head.sha
});
const { data: pr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
head: branchName,
base: pullRequest.data.base.ref,
title: 'Auto-fix pre-commit issues',
body: 'This PR fixes pre-commit errors automatically.',
maintainer_can_modify: true
});
console.log(`Created PR: ${pr.html_url}`);
- name: Comment on PR if pre-commit failed
if: failure() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "⚠️ Pre-commit checks failed. You can fix issues locally by running `pre-commit run --all-files`. Alternatively, comment `/fix-precommit` on this pull request, and I will attempt to auto-fix and open a new pull request for you."
});

View File

@ -46,6 +46,3 @@ repos:
always_run: true
pass_filenames: false
ci:
autofix_prs: true
autoupdate_schedule: quarterly