This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
152 lines (122 loc) · 4.54 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { inspect } = require("util");
const core = require("@actions/core");
const { Octokit } = require("@octokit/action");
main();
async function main() {
try {
const octokit = new Octokit();
let parameters = {};
parameters.per_page = 100;
parameters.page = 1;
const repository = core.getInput("repository");
const ownerAndRepo = repository.split("/");
if(ownerAndRepo.length !== 2){
throw new Error(`The repository input parameter '${repository}' is not in the format {owner}/{repo}.`);
}
parameters.owner = ownerAndRepo[0];
parameters.repo = ownerAndRepo[1];
if(!parameters.owner){
throw new Error(`Owner cannot be empty. Make sure the repository input parameter '${repository}' is in the format {owner}/{repo}.`);
}
if(!parameters.repo){
throw new Error(`Repository cannot be empty. Make sure the repository input parameter '${repository}' is in the format {owner}/{repo}.`);
}
const workflow = core.getInput("workflow");
const olderThanSeconds = core.getInput("older-than-seconds");
const createdBefore = core.getInput("created-before");
const actor = core.getInput("actor");
const branch = core.getInput("branch");
const event = core.getInput("event");
const status = core.getInput("status");
const whatIf = core.getInput("what-if");
core.info(`Applying filters:`);
if(!!workflow){
core.info(`workflow: ${workflow}`);
parameters.workflow_id = workflow;
}
if(!!olderThanSeconds){
core.info(`older-than-seconds: ${olderThanSeconds}`);
parameters.created = `<=${new Date(Date.now() - olderThanSeconds * 1000).toISOString()}`;
}
else if(!!createdBefore){
let createdBeforeDate = new Date(createdBefore);
core.info(`created-before: ${createdBeforeDate}`);
parameters.created = `<=${createdBeforeDate.toISOString()}`;
}
if(!!actor){
parameters.actor = actor;
core.info(`actor: ${actor}`);
}
if(!!branch){
parameters.branch = branch;
core.info(`branch: ${branch}`);
}
if(!!event){
parameters.event = event;
core.info(`event: ${event}`);
}
if(!!status){
parameters.status = status;
core.info(`status: ${status}`);
}
if(whatIf !== "false"){
if(whatIf !== "true"){
core.warning(`Invalid value "${whatIf}" for what-if. Should be either "true" or "false". Defaulting to "true".`);
}
core.info(`Running in what-if mode. The following workflow runs would be deleted if what-if was set to "false":`);
}
for(;;) {
let response;
if(!!workflow){
response = await octokit.actions.listWorkflowRuns(parameters);
}
else{
response = await octokit.actions.listWorkflowRunsForRepo(parameters);
}
if(response.data.workflow_runs <= 0){
break;
}
let deletedFromCurrentPage = false;
for (const workflowRun of response.data.workflow_runs) {
const title = workflowRun.head_commit.message.split("\n")[0]
const workflowRunLog = `${workflowRun.id} created at ${workflowRun.created_at}. Title: "${title}", Author: ${workflowRun.head_commit.author.name} - ${workflowRun.head_commit.author.email}, Branch: ${workflowRun.head_branch}, Workflow: ${workflowRun.name}`;
if(process.env.GITHUB_RUN_ID == workflowRun.id){
core.info(`Skipping current workflow run ${workflowRun.id}.`);
continue;
}
if(whatIf !== "false"){
core.info(`Workflow run ${workflowRunLog}`);
continue;
}
else{
core.info(`Deleting workflow run ${workflowRunLog}...`);
}
let deleteParameters = {
owner: parameters.owner,
repo: parameters.repo,
run_id: workflowRun.id
};
try {
let { status } = await octokit.actions.deleteWorkflowRun(deleteParameters);
if(status == 204){
core.info(`Deleted workflow run ${workflowRun.id}.`);
deletedFromCurrentPage = true;
} else{
core.warning(`Something went wrong while deleting workflow "${title}" with ID:${workflowRun.id}. Status code: ${status}`);
}
} catch (error) {
core.info(inspect(error));
}
}
if(deletedFromCurrentPage && response.data.total_count < parameters.per_page){
break;
}
if(whatIf !== "false" || !deletedFromCurrentPage){
parameters.page += 1;
}
}
} catch (error) {
core.info(inspect(error));
core.setFailed(error.message);
}
}