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 36
/
index.ts
293 lines (267 loc) Β· 7.73 KB
/
index.ts
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
282
283
284
285
286
287
288
289
290
291
292
293
import { context, getOctokit } from "@actions/github";
import {
error,
getBooleanInput,
getInput,
info,
setSecret,
warning,
} from "@actions/core";
export type Octokit = ReturnType<typeof getOctokit>;
interface QualifiedRepo {
owner: string;
repo: string;
}
export interface WorkflowInput {
githubToken: string;
shouldDeleteReleases: boolean;
repo: QualifiedRepo;
tagName: string;
octokit: Octokit;
}
/**
* Logs a message.
*
* @param header A header (preferably an emoji representing the "feel" of the message) that should be displayed before
* the message. This is expected to be a single character.
* @param message The message to display.
* @param level The severity level for this message.
*/
function log(
header: string,
message: string,
level: "default" | "error" | "warn" = "default"
): void {
const loggableMessage = `${header.padEnd(3)}${message}`;
switch (level) {
case "default":
info(loggableMessage);
break;
case "error":
error(loggableMessage);
break;
case "warn":
warning(loggableMessage);
break;
}
}
/**
* Deletes a single tag with the name of {@link tagName}.
* @param octokit The Octokit instance to use for making API calls to GitHub.
* @param owner The owner of the repo with the releases to delete.
* @param repo The repo with the releases to delete.
* @param tagName The name of the tag to delete. This is expected to be solely the tag name, not the name of a
* git reference.
*/
async function deleteTag(
octokit: Octokit,
{ owner, repo }: QualifiedRepo,
tagName: string
): Promise<void> {
try {
await octokit.rest.git.deleteRef({
owner,
repo,
ref: `tags/${tagName}`,
});
log("β
", `"${tagName}" deleted successfully!`);
} catch (error) {
if (error instanceof Error) {
log(
"π",
`failed to delete tag "${tagName}" <- ${error.message}`,
"error"
);
if (error.message === "Reference does not exist") {
log(
"π",
"Proceeding anyway, because tag not existing is the goal",
"warn"
);
} else {
log(
"π",
`An error occurred while deleting the tag "${tagName}"`,
"error"
);
process.exit(1);
}
} else {
log(
"π",
`An error occurred while deleting the tag "${tagName}"`,
"error"
);
process.exit(1);
}
}
}
/**
* Deletes all releases that are pointed to {@link tagName}.
*
* @param octokit The Octokit instance to use for making API calls to GitHub.
* @param qualifiedRepo The fully qualified repo to delete releases for.
* @param tagName The tag name to delete releases that are based on this tag.
*/
async function deleteReleases(
octokit: Octokit,
qualifiedRepo: QualifiedRepo,
tagName: string
): Promise<void> {
let releaseIds: number[] = [];
try {
const releases = await octokit.rest.repos.listReleases({
repo: qualifiedRepo.repo,
owner: qualifiedRepo.owner,
});
releaseIds = (releases.data ?? [])
.filter(({ tag_name, draft }) => tag_name === tagName && !draft)
.map(({ id }) => id);
} catch (error) {
if (error instanceof Error) {
log("π", `failed to get list of releases <- ${error.message}`, "error");
} else {
log("π", `failed to get list of releases <- ${error}`, "error");
}
process.exit(1);
return;
}
if (releaseIds.length === 0) {
log("π", `no releases found associated to tag "${tagName}"`, "warn");
return;
}
log("π»", `found ${releaseIds.length} releases to delete`);
for (const release_id of releaseIds) {
try {
await octokit.rest.repos.deleteRelease({
release_id,
...qualifiedRepo,
});
} catch (error) {
if (error instanceof Error) {
log(
"π",
`failed to delete release with id "${release_id}" <- ${error.message}`,
"error"
);
} else {
log(
"π",
`failed to delete release with id "${release_id}" <- ${error}`,
"error"
);
}
process.exit(1);
}
}
log("ππΌ", "all releases deleted successfully!");
}
/**
* Gets the repo information for the repo that this action should operate on. Defaults to the repo running this action
* if the repo isn't explicitly set via this action's input.
*/
function getRepo(): QualifiedRepo {
const inputRepoData = getInput("repo");
const [inputOwner, inputRepo] = inputRepoData?.split("/");
if (inputRepo && inputOwner) {
return {
repo: inputRepo,
owner: inputOwner,
};
} else if (inputRepo || inputOwner) {
log(
"π",
`a valid repo was not given. Expected "${inputRepoData}" to be in the form of "owner/repo"`
);
process.exit(1);
} else {
// This default should only happen when no input repo at all is provided.
return context.repo;
}
}
function getGitHubToken(): string {
const tokenFromEnv = process.env.GITHUB_TOKEN;
const inputToken = getInput("github_token");
if (inputToken) {
return inputToken;
}
if (tokenFromEnv != null) {
log(
"β οΈ",
'Providing the GitHub token from the environment variable is deprecated. Provide it as an input with the name "github_token" instead.',
"warn"
);
return tokenFromEnv;
}
log(
"π",
'A valid GitHub token was not provided. Provide it as an input with the name "github_token"',
"error"
);
process.exit(1);
}
function getShouldDeleteReleases(): boolean {
const deleteReleaseInputKey = "delete_release";
const hasDeleteReleaseInput = !!getInput(deleteReleaseInputKey);
if (hasDeleteReleaseInput) {
// This will throw if it's not provided, so we have to wrap it in a check to make
// sure it exists first, since it's an optional field.
return getBooleanInput(deleteReleaseInputKey);
}
return false;
}
/**
* Gets the inputs for this action.
*
* @return {Promise<{shouldDeleteReleases: boolean,
* githubToken: string,
* repo: {repo: string, owner: string},
* tagName: string,
* octokit: import("@octokit/core").Octokit & import("@octokit/plugin-rest-endpoint-methods").restEndpointMethods}>}
*/
export function getInputs(): WorkflowInput {
const tagName = getInput("tag_name");
const githubToken = getGitHubToken();
const shouldDeleteReleases = getShouldDeleteReleases();
const repo = getRepo();
const octokit = getOctokit(githubToken);
return {
octokit,
tagName,
githubToken,
shouldDeleteReleases,
repo,
};
}
function validateInputField(isValid: any, invalidMessage: string): void {
if (!isValid) {
log("π", invalidMessage, "error");
process.exit(1);
}
}
/**
* Runs this action using the provided inputs.
*/
export async function run(inputs: WorkflowInput): Promise<void> {
const { tagName, githubToken, shouldDeleteReleases, repo, octokit } = inputs;
setSecret(githubToken);
// Purposefully perform these checks even though the types match because it's possible the inputs were provided
// directly as environment variables
validateInputField(tagName, "no tag name provided as an input.");
validateInputField(githubToken, "no Github token provided");
validateInputField(
typeof shouldDeleteReleases === "boolean",
`an invalid value for shouldDeleteReleases was provided: ${shouldDeleteReleases}`
);
validateInputField(
repo?.owner && repo?.repo,
"An invalid repo was provided!"
);
log("π·", `given tag is "${tagName}"`);
log("π", `given repo is "${repo.owner}/${repo.repo}"`);
log("π", `delete releases is set to "${shouldDeleteReleases}"`);
if (shouldDeleteReleases) {
await deleteReleases(octokit, repo, tagName);
}
await deleteTag(octokit, repo, tagName);
}