This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.js
281 lines (233 loc) · 7.9 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const fetch = require("./fetch");
if (!process.env.GITHUB_TOKEN) {
console.error("🔴 no GITHUB_TOKEN found. pass `GITHUB_TOKEN` as env");
process.exitCode = 1;
return;
}
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
if (!process.env.GITHUB_REPOSITORY) {
console.error(
"🔴 no GITHUB_REPOSITORY found. pass `GITHUB_REPOSITORY` as env"
);
process.exitCode = 1;
return;
}
if (!process.env.INPUT_REPO) {
console.warn("💬 no `repo` name given. fall-ing back to this repo");
}
const [owner, repo] = (
process.env.INPUT_REPO || process.env.GITHUB_REPOSITORY
).split("/");
if (!owner || !repo) {
console.error("☠️ either owner or repo name is empty. exiting...");
process.exitCode = 1;
return;
}
if (!process.env.INPUT_KEEP_LATEST) {
console.error("✋🏼 no `keep_latest` given. exiting...");
process.exitCode = 1;
return;
}
const keepLatest = Number(process.env.INPUT_KEEP_LATEST);
if (Number.isNaN(keepLatest) || keepLatest < 0) {
console.error("🤮 invalid `keep_latest` given. exiting...");
process.exitCode = 1;
return;
}
if (keepLatest === 0) {
console.error("🌶 given `keep_latest` is 0, this will wipe out all releases");
}
const shouldDeleteTags = process.env.INPUT_DELETE_TAGS === "true";
if (shouldDeleteTags) {
console.log("🔖 corresponding tags also will be deleted");
}
const deletePrereleaseOnly = process.env.INPUT_DELETE_PRERELEASE_ONLY === "true";
if (deletePrereleaseOnly) {
console.log("🔖 Remove only prerelease");
}
let deletePatternStr = process.env.INPUT_DELETE_TAG_PATTERN || "";
let deletePattern = new RegExp("");
if (deletePatternStr) {
console.log(`releases matching ${deletePatternStr} will be targeted`);
deletePattern = new RegExp(deletePatternStr);
}
let keepMinDownloadCount = Number(process.env.INPUT_KEEP_MIN_DOWNLOAD_COUNTS);
if (Number.isNaN(keepMinDownloadCount) || keepMinDownloadCount < 0) {
keepMinDownloadCount = 0
}
if (keepMinDownloadCount === 0) {
console.error("🌶 given `keep_min_download_counts` is 0, this will not enable the download count removal rule");
}else {
console.log("🌶 given `keep_min_download_counts` is ",keepMinDownloadCount,", this will continue to add the download count deletion rule to the original deletion rule");
}
let deleteExpiredData = Number(process.env.INPUT_DELETE_EXPIRED_DATA);
if (Number.isNaN(deleteExpiredData) || deleteExpiredData < 0) {
deleteExpiredData = 0
}
console.log("🌶 given `delete_expired_data` is ",deleteExpiredData);
let gitHubRestApi = process.env.INPUT_GITHUB_REST_API_URL || "api.github.com";
const commonOpts = {
host: gitHubRestApi,
port: 443,
protocol: "https:",
auth: `user:${GITHUB_TOKEN}`,
headers: {
"Content-Type": "application/json",
"User-Agent": "node.js",
},
};
async function deleteOlderReleases(keepLatest, keepMinDownloadCount, deleteExpiredData) {
let releaseIdsAndTags = [];
try {
const releasesData = [];
let page = 1;
let hasMorePages = true;
while (hasMorePages) {
let pageData = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/releases?per_page=100&page=${page}`,
method: "GET",
});
if (pageData.length === 0) {
hasMorePages = false;
} else {
releasesData.push(...pageData);
page++;
}
}
data = releasesData || [];
const activeMatchedReleases = data.filter((item) => {
if (deletePrereleaseOnly) {
if (deletePatternStr) {
return !item.draft && item.prerelease && item.tag_name.match(deletePattern);
} else {
return !item.draft && item.prerelease;
}
} else {
if (deletePatternStr) {
return !item.draft && item.tag_name.match(deletePattern);
} else {
return !item.draft;
}
}
})
// const activeMatchedReleases = data.filter((item) => {
// const shouldDelete = deletePrereleaseOnly && deletePatternStr;
// const isDraft = item.draft;
// const hasAssets = item.assets.length > 0;
// const isPrerelease = item.prerelease;
// const isTagMatching = deletePatternStr ? item.tag_name.match(deletePattern) : true;
// return !isDraft && hasAssets && (shouldDelete ? (isTagMatching && isPrerelease) : true);
// });
if (activeMatchedReleases.length === 0) {
console.log(`😕 no active releases found. exiting...`);
return;
}
const matchingLoggingAddition = deletePattern.length > 0 ? " matching" : "";
if (deletePrereleaseOnly) {
console.log(
`💬 found total of ${activeMatchedReleases.length}${matchingLoggingAddition} active prerelease(s)`
);
} else {
console.log(
`💬 found total of ${activeMatchedReleases.length}${matchingLoggingAddition} active release(s)`
);
}
releaseIdsAndTags = activeMatchedReleases
.sort((a,b)=> Date.parse(b.published_at) - Date.parse(a.published_at))
.map(item=> {
const totalDownloads = item.assets.reduce((sum, asset) => sum + asset.download_count, 0);
return {
id: item.id,
tagName: item.tag_name,
published_at: item.published_at,
download_counts: totalDownloads
}
})
.slice(keepLatest);
if (keepMinDownloadCount !== 0)
{
if (deleteExpiredData !== 0)
{
const currentDate = new Date();
releaseIdsAndTags = releaseIdsAndTags.filter(item => {
const publishedDate = new Date(item.published_at);
const timeDifference = currentDate - publishedDate;
const daysDifference = Math.floor(timeDifference / (1000 * 3600 * 24));
return item.download_counts < keepMinDownloadCount || daysDifference > deleteExpiredData;
});
}
else
{
releaseIdsAndTags=releaseIdsAndTags.filter(item => item.download_counts < keepMinDownloadCount);
}
}
else
{
if (deleteExpiredData !== 0)
{
const currentDate = new Date();
releaseIdsAndTags = releaseIdsAndTags.filter(item => {
const publishedDate = new Date(item.published_at);
const timeDifference = currentDate - publishedDate;
const daysDifference = Math.floor(timeDifference / (1000 * 3600 * 24));
return daysDifference > deleteExpiredData;
});
}
}
}catch (error) {
console.error(`🌶 failed to get list of releases <- ${error.message}`);
console.error(`exiting...`);
process.exitCode = 1;
return;
}
if (releaseIdsAndTags.length === 0) {
console.error(`😕 no older releases found. exiting...`);
return;
}
console.log(`🍻 found ${releaseIdsAndTags.length} older release(s)`);
let hasError = false;
for (let i = 0; i < releaseIdsAndTags.length; i++) {
const { id: releaseId, tagName } = releaseIdsAndTags[i];
try {
console.log(`starting to delete ${tagName} with id ${releaseId}`);
const _ = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/releases/${releaseId}`,
method: "DELETE",
});
if (shouldDeleteTags) {
try {
const _ = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/git/refs/tags/${encodeURI(tagName)}`,
method: "DELETE",
});
} catch (error) {
console.error(
`🌶 failed to delete tag "${tagName}" <- ${error.message}`
);
hasError = true;
break;
}
}
} catch (error) {
console.error(
`🌶 failed to delete release with id "${releaseId}" <- ${error.message}`
);
hasError = true;
break;
}
}
if (hasError) {
process.exitCode = 1;
return;
}
console.log(
`👍🏼 ${releaseIdsAndTags.length} older release(s) deleted successfully!`
);
}
async function run() {
await deleteOlderReleases(keepLatest, keepMinDownloadCount, deleteExpiredData);
}
run();