Skip to content

ci(pre-commit): Enforce that the pre-commit checks are run before merge #264

ci(pre-commit): Enforce that the pre-commit checks are run before merge

ci(pre-commit): Enforce that the pre-commit checks are run before merge #264

Workflow file for this run

name: Pre-commit check
on:
pull_request:
types: [opened, reopened, synchronize, labeled, unlabeled]
permissions:
checks: write
jobs:
lint:
if: |
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge')
name: Checking if any fixes are needed
runs-on: ubuntu-latest
steps:
- name: Checkout latest commit
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Set up Python 3
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Get Python version hash
run: |
echo "Using $(python -VV)"
echo "PY_HASH=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- name: Restore pre-commit cache
uses: actions/cache/restore@v4
id: restore-cache
with:
path: |
~/.cache/pre-commit
~/.cache/pip
key: pre-commit|${{ env.PY_HASH }}|${{ hashFiles('.pre-commit-config.yaml', '.github/workflows/pre-commit.yml') }}
- name: Install python dependencies
run: python -m pip install pre-commit docutils
- name: Get changed files
id: changed-files
uses: tj-actions/[email protected]
- name: Run pre-commit hooks in changed files
run: pre-commit run --color=always --show-diff-on-failure --files ${{ steps.changed-files.outputs.all_changed_files }}
- name: Save pre-commit cache
uses: actions/cache/save@v4
if: ${{ always() && steps.restore-cache.outputs.cache-hit != 'true' }}
continue-on-error: true
with:
path: |
~/.cache/pre-commit
~/.cache/pip
key: ${{ steps.restore-cache.outputs.cache-primary-key }}
- name: Push changes using pre-commit-ci-lite
uses: pre-commit-ci/[email protected]
if: always()
with:
msg: "ci(pre-commit): Apply automatic fixes"
report-run:
name: Check if the PR has run the pre-commit checks
needs: lint
if: always()
runs-on: ubuntu-latest
steps:
- name: Report success
if: |
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') &&
needs.lint.result == 'success'
uses: actions/github-script@v7
with:
script: |
github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: ${{ github.event.pull_request.head.sha }},
state: 'success',
context: 'pre-commit-result',
description: 'All pre-commit checks passed',
});
- name: Report pending
env:
GH_TOKEN: ${{ github.token }}
if: |
!contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') ||
needs.lint.result == 'failure'
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/statuses/${{ github.sha }} \
-f state=pending -f context=pre-commit-result -f description="Pre-commit checks need to pass before merging"