name: Pre-Commit Checks
on:
  pull_request:
jobs:
  pre_commit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Fully Checkout Repo
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          git checkout ${{ github.base_ref }}
          git reset --hard origin/${{ github.base_ref }}
          git checkout ${{ github.head_ref }}
          git reset --hard origin/${{ github.head_ref }}
      - name: Install requirements
        run: python3 -m pip install pre-commit
      - name: Run pre-commit
        id: pre-commit
        run: pre-commit run --from-ref origin/${{ github.base_ref }} --to-ref origin/${{ github.head_ref }}
      - name: Find Comment
        uses: peter-evans/find-comment@81e2da3af01c92f83cb927cf3ace0e085617c556  # v2.2.0 (pinned for security reasons)
        id: fc
        if: failure() && steps.pre-commit.outcome == 'failure'
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-regex: 'Pre-commit checks failed'
      - name: Create or update comment
        uses: peter-evans/create-or-update-comment@5adcb0bb0f9fb3f95ef05400558bdb3f329ee808  # v2.1.0 (pinned for security reasons)
        if: failure() && steps.fc.outcome == 'success'
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            **Pre-commit checks failed.**
            
            Please install pre-commit so that your changes are checked before committing and pushing to GitHub. Run the following in the root of this repo:
            ```
            pip3 install pre-commit
            pre-commit install
            pre-commit clean
            ```
            Pre-commit will check and make changes to files when you run `git commit`. If pre-commit makes changes and fails you will need to add those changes by running `git add` and `git commit`.

            As you have already made changes and committed them before installing pre-commit, please run the following:
            ```
            git fetch
            git commit -a -m "Save work"
            pre-commit run --from-ref origin/${{ github.base_ref }} --to-ref ${{ github.head_ref }}
            ```
            
            You can use `git commit --no-verify` to bypass pre-commit however your PR will fail the pre-commit CI checks.
          edit-mode: replace
