forked from dev-drprasad/delete-older-releases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (138 loc) ยท 4.19 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
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");
}
let deletePattern = process.env.INPUT_DELETE_TAG_PATTERN || "";
if (deletePattern) {
console.log(`releases containing ${deletePattern} will be targeted`);
}
const commonOpts = {
host: "api.github.com",
port: 443,
protocol: "https:",
auth: `user:${GITHUB_TOKEN}`,
headers: {
"Content-Type": "application/json",
"User-Agent": "node.js",
},
};
async function deleteOlderReleases(keepLatest) {
let releaseIdsAndTags = [];
try {
let data = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/releases?per_page=100`,
method: "GET",
});
data = data || [];
// filter for delete_pattern
const activeMatchedReleases = data.filter(
({ draft, tag_name }) => !draft && tag_name.indexOf(deletePattern) !== -1
);
if (activeMatchedReleases.length === 0) {
console.log(`๐ no active releases found. exiting...`);
return;
}
const matchingLoggingAddition = deletePattern.length > 0 ? " matching" : "";
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(({ id, tag_name: tagName }) => ({ id, tagName }))
.slice(keepLatest);
} 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/${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);
}
run();