Skip to content

ci(pre-commit): Fix PR checks and cache #9531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions .github/workflows/pre-commit-status.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,64 @@
# This needs to be in a separate workflow because it requires higher permissions than the calling workflow
name: Report Pre-commit Check Status

on:
pull_request_target:
types: [opened, reopened, synchronize, labeled, unlabeled]
workflow_run:
workflows: [Pre-commit hooks]
types:
- completed

permissions:
statuses: write

jobs:
report-run:
name: Check if the PR has run the pre-commit checks
report-success:
name: Report pre-commit success
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Report pending
uses: conda/actions/[email protected]
with:
context: "Pre-commit checks"
state: pending
description: The pre-commit checks need to be successful before merging

- name: Wait for pre-commit checks to complete
uses: lucasssvaz/wait-on-workflow@v1
if: |
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge')
id: wait-on-workflow
- name: Report success
uses: actions/github-script@v7
with:
timeout: 10
interval: 30
workflow: pre-commit.yml
sha: ${{ github.event.pull_request.head.sha || github.sha }}
script: |
const owner = '${{ github.repository_owner }}';
const repo = '${{ github.repository }}'.split('/')[1];
const sha = '${{ github.event.workflow_run.head_sha }}';
core.debug(`owner: ${owner}`);
core.debug(`repo: ${repo}`);
core.debug(`sha: ${sha}`);
const { context: name, state } = (await github.rest.repos.createCommitStatus({
context: 'Pre-commit checks',
description: 'Pre-commit checks successful',
owner: owner,
repo: repo,
sha: sha,
state: 'success',
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
})).data;
core.info(`${name} is ${state}`);

- name: Report success
uses: conda/actions/[email protected]
if: |
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') &&
steps.wait-on-workflow.outputs.conclusion == 'success'
report-pending:
name: Report pre-commit pending
if: github.event.workflow_run.conclusion != 'success'
runs-on: ubuntu-latest
steps:
- name: Report pending
uses: actions/github-script@v7
with:
context: "Pre-commit checks"
state: success
description: All pre-commit checks passed
script: |
const owner = '${{ github.repository_owner }}';
const repo = '${{ github.repository }}'.split('/')[1];
const sha = '${{ github.event.workflow_run.head_sha }}';
core.debug(`owner: ${owner}`);
core.debug(`repo: ${repo}`);
core.debug(`sha: ${sha}`);
const { context: name, state } = (await github.rest.repos.createCommitStatus({
context: 'Pre-commit checks',
description: 'The pre-commit checks need to be successful before merging',
owner: owner,
repo: repo,
sha: sha,
state: 'pending',
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
})).data;
core.info(`${name} is ${state}`);
14 changes: 12 additions & 2 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
name: Pre-commit hooks

on:
workflow_dispatch:
push:
branches:
- master
pull_request:
types: [opened, reopened, synchronize, labeled, unlabeled]

concurrency:
group: pre-commit-${{github.event.pull_request.number || github.ref}}
cancel-in-progress: true

jobs:
lint:
if: |
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge')
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') ||
github.event_name != 'pull_request'
name: Check if fixes are needed
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -57,6 +66,7 @@ jobs:

- name: Push changes using pre-commit-ci-lite
uses: pre-commit-ci/[email protected]
if: always()
# Only push changes in PRs
if: ${{ always() && github.event_name == 'pull_request' }}
with:
msg: "ci(pre-commit): Apply automatic fixes"
Loading