Skip to content

Commit 2c39877

Browse files
authored
ci(pre-commit): Fix PR checks and cache (#9531)
1 parent 4960d86 commit 2c39877

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

Diff for: .github/workflows/pre-commit-status.yml

+51-28
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,64 @@
1+
# This needs to be in a separate workflow because it requires higher permissions than the calling workflow
12
name: Report Pre-commit Check Status
23

34
on:
4-
pull_request_target:
5-
types: [opened, reopened, synchronize, labeled, unlabeled]
5+
workflow_run:
6+
workflows: [Pre-commit hooks]
7+
types:
8+
- completed
69

710
permissions:
811
statuses: write
912

1013
jobs:
11-
report-run:
12-
name: Check if the PR has run the pre-commit checks
14+
report-success:
15+
name: Report pre-commit success
16+
if: github.event.workflow_run.conclusion == 'success'
1317
runs-on: ubuntu-latest
1418
steps:
15-
- name: Report pending
16-
uses: conda/actions/set-commit-status@v24.2.0
17-
with:
18-
context: "Pre-commit checks"
19-
state: pending
20-
description: The pre-commit checks need to be successful before merging
21-
22-
- name: Wait for pre-commit checks to complete
23-
uses: lucasssvaz/wait-on-workflow@v1
24-
if: |
25-
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge')
26-
id: wait-on-workflow
19+
- name: Report success
20+
uses: actions/github-script@v7
2721
with:
28-
timeout: 10
29-
interval: 30
30-
workflow: pre-commit.yml
31-
sha: ${{ github.event.pull_request.head.sha || github.sha }}
22+
script: |
23+
const owner = '${{ github.repository_owner }}';
24+
const repo = '${{ github.repository }}'.split('/')[1];
25+
const sha = '${{ github.event.workflow_run.head_sha }}';
26+
core.debug(`owner: ${owner}`);
27+
core.debug(`repo: ${repo}`);
28+
core.debug(`sha: ${sha}`);
29+
const { context: name, state } = (await github.rest.repos.createCommitStatus({
30+
context: 'Pre-commit checks',
31+
description: 'Pre-commit checks successful',
32+
owner: owner,
33+
repo: repo,
34+
sha: sha,
35+
state: 'success',
36+
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
37+
})).data;
38+
core.info(`${name} is ${state}`);
3239
33-
- name: Report success
34-
uses: conda/actions/set-commit-status@v24.2.0
35-
if: |
36-
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') &&
37-
steps.wait-on-workflow.outputs.conclusion == 'success'
40+
report-pending:
41+
name: Report pre-commit pending
42+
if: github.event.workflow_run.conclusion != 'success'
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Report pending
46+
uses: actions/github-script@v7
3847
with:
39-
context: "Pre-commit checks"
40-
state: success
41-
description: All pre-commit checks passed
48+
script: |
49+
const owner = '${{ github.repository_owner }}';
50+
const repo = '${{ github.repository }}'.split('/')[1];
51+
const sha = '${{ github.event.workflow_run.head_sha }}';
52+
core.debug(`owner: ${owner}`);
53+
core.debug(`repo: ${repo}`);
54+
core.debug(`sha: ${sha}`);
55+
const { context: name, state } = (await github.rest.repos.createCommitStatus({
56+
context: 'Pre-commit checks',
57+
description: 'The pre-commit checks need to be successful before merging',
58+
owner: owner,
59+
repo: repo,
60+
sha: sha,
61+
state: 'pending',
62+
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
63+
})).data;
64+
core.info(`${name} is ${state}`);

Diff for: .github/workflows/pre-commit.yml

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
name: Pre-commit hooks
22

33
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
48
pull_request:
59
types: [opened, reopened, synchronize, labeled, unlabeled]
610

11+
concurrency:
12+
group: pre-commit-${{github.event.pull_request.number || github.ref}}
13+
cancel-in-progress: true
14+
715
jobs:
816
lint:
917
if: |
10-
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge')
18+
contains(github.event.pull_request.labels.*.name, 'Status: Pending Merge') ||
19+
github.event_name != 'pull_request'
1120
name: Check if fixes are needed
1221
runs-on: ubuntu-latest
1322
steps:
@@ -57,6 +66,7 @@ jobs:
5766

5867
- name: Push changes using pre-commit-ci-lite
5968
uses: pre-commit-ci/lite-action@v1.0.2
60-
if: always()
69+
# Only push changes in PRs
70+
if: ${{ always() && github.event_name == 'pull_request' }}
6171
with:
6272
msg: "ci(pre-commit): Apply automatic fixes"

0 commit comments

Comments
 (0)