From 74f958a5f14ac0637e5457eba5e963103980103d Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Mon, 28 Apr 2025 13:36:28 +0200 Subject: [PATCH] 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 * remove default * use comment rather than label --------- Co-authored-by: Matthias Kuhn --- .github/workflows/pre-commit.yaml | 98 +++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 3 - 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pre-commit.yaml diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml new file mode 100644 index 00000000000..18f905ebd6e --- /dev/null +++ b/.github/workflows/pre-commit.yaml @@ -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." + }); diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e90dea0b0ea..3dc24f327ce 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,6 +46,3 @@ repos: always_run: true pass_filenames: false -ci: - autofix_prs: true - autoupdate_schedule: quarterly