forked from c-hive/gha-remove-artifacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (150 loc) · 4.74 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const core = require("@actions/core");
const { Octokit } = require("@octokit/action");
const { throttling } = require("@octokit/plugin-throttling");
const moment = require("moment");
const yn = require("yn");
const devEnv = process.env.NODE_ENV === "dev";
if (devEnv) {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
require("dotenv-safe").config();
}
function getConfigs() {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const [age, units] = devEnv
? process.env.AGE.split(" ")
: core.getInput("age", { required: true }).split(" ");
const maxAge = moment().subtract(age, units);
console.log(
"Maximum artifact age:",
age,
units,
"( created before",
maxAge.format(),
")"
);
return {
repo: {
owner,
repo,
},
pagination: {
perPage: 100,
},
maxAge: moment().subtract(age, units),
skipTags: devEnv
? yn(process.env.SKIP_TAGS)
: yn(core.getInput("skip-tags")),
retriesEnabled: true,
};
}
const ThrottledOctokit = Octokit.plugin(throttling);
async function run() {
const configs = getConfigs();
const octokit = new ThrottledOctokit({
throttle: {
onRateLimit: (retryAfter, options) => {
console.error(
`Request quota exhausted for request ${options.method} ${options.url}, number of total global retries: ${options.request.retryCount}`
);
console.log(`Retrying after ${retryAfter} seconds!`);
return configs.retriesEnabled;
},
onAbuseLimit: (retryAfter, options) => {
console.error(
`Abuse detected for request ${options.method} ${options.url}, retry count: ${options.request.retryCount}`
);
console.log(`Retrying after ${retryAfter} seconds!`);
return configs.retriesEnabled;
},
},
});
async function getTaggedCommits() {
const listTagsRequest = octokit.repos.listTags.endpoint.merge({
...configs.repo,
per_page: configs.pagination.perPage,
ref: "tags",
});
const tags = await octokit.paginate(listTagsRequest);
return tags.map(tag => tag.commit.sha);
}
let taggedCommits;
if (configs.skipTags) {
try {
taggedCommits = await getTaggedCommits(octokit);
} catch (err) {
console.error("Error while requesting tags: ", err);
throw err;
}
}
const workflowRunsRequest = octokit.actions.listRepoWorkflowRuns.endpoint.merge(
{
...configs.repo,
per_page: configs.pagination.perPage,
}
);
return octokit
.paginate(workflowRunsRequest, ({ data }, done) => {
const stopPagination = data.find(workflowRun => {
const createdAt = moment(workflowRun.created_at);
return createdAt.isBefore(moment.utc().subtract(90, "days"));
});
if (stopPagination) {
done();
}
return data;
})
.then(workflowRuns => {
const artifactPromises = workflowRuns
.filter(workflowRun => {
const skipWorkflow =
configs.skipTags && taggedCommits.includes(workflowRun.head_sha);
if (skipWorkflow) {
console.log(`Skipping tagged run ${workflowRun.head_sha}`);
return false;
}
return true;
})
.map(workflowRun => {
const workflowRunArtifactsRequest = octokit.actions.listWorkflowRunArtifacts.endpoint.merge(
{
...configs.repo,
per_page: configs.pagination.perPage,
run_id: workflowRun.id,
}
);
return octokit.paginate(workflowRunArtifactsRequest).then(artifacts =>
artifacts
.filter(artifact => {
const createdAt = moment(artifact.created_at);
return createdAt.isBefore(configs.maxAge);
})
.map(artifact => {
if (devEnv) {
return new Promise(resolve => {
console.log(
`Recognized development environment, preventing ${artifact.id} from being removed.`
);
resolve();
});
}
return octokit.actions
.deleteArtifact({
...configs.repo,
artifact_id: artifact.id,
})
.then(() => {
console.log(
`Successfully removed artifact with id ${artifact.id}.`
);
});
})
);
});
return Promise.all(artifactPromises).then(artifactDeletePromises =>
Promise.all([].concat(...artifactDeletePromises))
);
});
}
run().catch(err => {
core.setFailed(err.toString());
});