forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
67 lines (62 loc) · 2.39 KB
/
Copy pathpr-cancel.yml
File metadata and controls
67 lines (62 loc) · 2.39 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
56
57
58
59
60
61
62
63
64
65
66
67
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 Integration Tests
uses: actions/github-script@v8
with:
script: |
const workflows = [
'pr-build.yml',
'codeql.yml',
'integration-test-single-node.yml',
'integration-test-multinode.yml',
];
const headSha = context.payload.pull_request.head.sha;
const prNumber = context.payload.pull_request.number;
for (const workflowId of workflows) {
// Wrap each workflow iteration so a missing / renamed file
// doesn't take down the whole cancel job — other workflows
// in the list still get processed.
try {
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})`);
}
}
}
} catch (err) {
console.log(`Skipping ${workflowId}: ${err.message}`);
}
}