-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathindex.ts
92 lines (79 loc) · 2.97 KB
/
index.ts
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
import * as core from '@actions/core';
import * as github from '@actions/github';
if (!github) {
throw new Error('Module not found: github');
}
if (!core) {
throw new Error('Module not found: core');
}
async function main() {
const { eventName, sha, ref, repo: { owner, repo }, payload } = github.context;
const { GITHUB_RUN_ID } = process.env;
let branch = ref.slice(11);
let headSha = sha;
if (payload.pull_request) {
branch = payload.pull_request.head.ref;
headSha = payload.pull_request.head.sha;
} else if (payload.workflow_run) {
branch = payload.workflow_run.head_branch;
headSha = payload.workflow_run.head_sha;
}
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID });
const token = core.getInput('access_token', { required: true });
const workflow_id = core.getInput('workflow_id', { required: false });
const ignore_sha = core.getInput('ignore_sha', { required: false }) === 'true';
console.log(`Found token: ${token ? 'yes' : 'no'}`);
const workflow_ids: string[] = [];
const octokit = github.getOctokit(token);
const { data: current_run } = await octokit.actions.getWorkflowRun({
owner,
repo,
run_id: Number(GITHUB_RUN_ID)
});
if (workflow_id) {
// The user provided one or more workflow id
workflow_id.replace(/\s/g, '')
.split(',')
.forEach(n => workflow_ids.push(n));
} else {
// The user did not provide workflow id so derive from current run
workflow_ids.push(String(current_run.workflow_id));
}
console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`);
await Promise.all(workflow_ids.map(async (workflow_id) => {
try {
const { data } = await octokit.actions.listWorkflowRuns({
owner,
repo,
// @ts-ignore
workflow_id,
branch,
});
const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch);
console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`);
console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n'));
const runningWorkflows = branchWorkflows.filter(run =>
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at)
);
console.log(`with ${runningWorkflows.length} runs to cancel.`);
for (const {id, head_sha, status, html_url} of runningWorkflows) {
console.log('Canceling run: ', {id, head_sha, status, html_url});
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
} catch (e) {
const msg = e.message || e;
console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`);
}
console.log('')
}));
}
main()
.then(() => core.info('Cancel Complete.'))
.catch(e => core.setFailed(e.message));