-
Notifications
You must be signed in to change notification settings - Fork 1.7k
55 lines (50 loc) · 1.88 KB
/
pr-cancel.yml
File metadata and controls
55 lines (50 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
name: Cancel PR Workflows on Close
on:
pull_request:
types: [ closed ]
permissions:
actions: write
jobs:
cancel:
name: Cancel In-Progress Workflows
if: github.event.pull_request.merged == false
runs-on: ubuntu-latest
steps:
- name: Cancel PR Build and System Test
uses: actions/github-script@v8
with:
script: |
const workflows = ['pr-build.yml', 'system-test.yml', 'codeql.yml'];
const headSha = context.payload.pull_request.head.sha;
const prNumber = context.payload.pull_request.number;
for (const workflowId of workflows) {
for (const status of ['in_progress', 'queued']) {
const runs = await github.paginate(
github.rest.actions.listWorkflowRuns,
{
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId,
status,
event: 'pull_request',
per_page: 100,
},
(response) => response.data.workflow_runs
);
for (const run of runs) {
if (!run) {
continue;
}
const prs = Array.isArray(run.pull_requests) ? run.pull_requests : [];
const isTargetPr = prs.length === 0 || prs.some((pr) => pr.number === prNumber);
if (run.head_sha === headSha && isTargetPr) {
await github.rest.actions.cancelWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id,
});
console.log(`Cancelled ${workflowId} run #${run.id} (${status})`);
}
}
}
}