This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (55 loc) · 1.8 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const context = github.context;
var empty = require('is-empty');
// most @actions toolkit packages have async methods
async function run() {
try {
// Do nothing if event is not a pull request.
if (context.eventName !== 'pull_request') {
return;
}
// Read secret access token.
const myToken = core.getInput('githubToken');
const owner = context.repo.owner;
const repo = context.repo.repo;
let branch = context.payload.pull_request.head.ref;
if(empty(myToken)) {
core.setFailed('Action failed with error, please set githubToken token');
return;
}
const octokit = new github.GitHub(myToken);
// Get all running action on the same branch.
let listRunJob = await octokit.actions.listRepoWorkflowRuns({
owner,
repo,
branch: branch,
per_page: 100
});
// Get max run id by workflow.
var maxJobByWrokflow = [];
listRunJob.data.workflow_runs.forEach(function(value) {
if(maxJobByWrokflow[value.workflow_url] === undefined || value.run_number > maxJobByWrokflow[value.workflow_url]) {
maxJobByWrokflow[value.workflow_url] = value.run_number;
}
});
// Canceled all job with a less run number by workflow.
listRunJob.data.workflow_runs.forEach(function(value) {
if(maxJobByWrokflow[value.workflow_url] !== undefined
&& value.status != 'completed'
&& value.run_number < maxJobByWrokflow[value.workflow_url]) {
var run_id = value.id;
octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id
})
core.info(`Kill job ${value.run_number} to ${value.id}`);
}
});
}
catch (error) {
core.setFailed(error.message);
}
}
run()