-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (88 loc) · 3.62 KB
/
ci-tools-cancel_workflows_on_pr_closed.yml
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Leka - iOS Monorepo
# Copyright APF France handicap
# SPDX-License-Identifier: Apache-2.0
name: Cancel Workflows on PR Close or Command
on:
pull_request:
types: [closed]
issue_comment:
types: [created]
workflow_dispatch: # nothing to setup here, just to trigger the workflow manually
jobs:
cancel-workflows:
runs-on: ubuntu-latest
permissions:
actions: write
pull-requests: read # Needed to fetch PR details
steps:
- name: Cancel Running Workflows Based on Event
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
const eventName = context.eventName;
const currentRunId = context.runId;
let prNumber;
let prHeadSha;
if (eventName === 'pull_request') {
// Event triggered by PR closure
prNumber = context.payload.pull_request.number;
prHeadSha = context.payload.pull_request.head.sha;
console.log(`Triggered by pull_request event for PR #${prNumber}`);
} else if (eventName === 'issue_comment') {
// Event triggered by a new comment
prNumber = context.payload.issue.number;
// Check if the issue is a PR
if (!context.payload.issue.pull_request) {
console.log('The comment is not on a pull request. Exiting.');
return;
}
const commentBody = context.payload.comment.body.trim();
console.log(`Comment body: "${commentBody}"`);
// Check if the comment contains the "/cancel" command
if (commentBody !== '/cancel') {
console.log('Comment does not contain "/cancel". Exiting.');
return;
}
console.log(`Triggered by issue_comment event for PR #${prNumber}`);
// Fetch PR details to get the head SHA
const { data: pullRequest } = await github.rest.pulls.get({
owner,
repo,
pull_number: prNumber,
});
prHeadSha = pullRequest.head.sha;
} else {
console.log('Unsupported event type. Exiting.');
return;
}
console.log(`PR Number: ${prNumber}`);
console.log(`PR Head SHA: ${prHeadSha}`);
console.log(`Current Run ID: ${currentRunId}`);
// Fetch all in-progress and queued workflow runs
const statuses = ['in_progress', 'queued'];
let runs = [];
for (const status of statuses) {
const response = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
status,
per_page: 100, // Fetch up to 100 runs per status
});
runs = runs.concat(response.data.workflow_runs);
}
console.log(`Total in-progress or queued runs: ${runs.length}`);
runs.forEach(run => {
console.log(`Run ID: ${run.id}, Head SHA: ${run.head_sha}, Event: ${run.event}, Status: ${run.status}`);
});
// Cancel runs associated with the PR's head SHA, excluding the current run
for (const run of runs) {
if (run.id !== currentRunId && run.head_sha === prHeadSha) {
await github.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: run.id,
});
console.log(`Cancelled workflow run: ${run.id}`);
}
}