-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
283 lines (263 loc) Β· 8.18 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
import { context, getOctokit } from "@actions/github";
import { RequestError } from "@octokit/request-error";
import { error, getInput, info, setSecret, warning } from "@actions/core";
export type Octokit = ReturnType<typeof getOctokit>;
export interface WorkflowInput {
githubToken: string;
owner: string;
packageType: string;
packages: string[];
versions: 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 packageType The type of package to delete.
* @param packages The packages to delete.
* @param versions The versions to delete.
*/
async function deletePackageVersions(
octokit: Octokit,
owner: string,
packageType: string,
packages: string[],
versions: string[]
): Promise<void> {
const pkgType = packageType as
| "npm"
| "maven"
| "rubygems"
| "docker"
| "nuget"
| "container";
const userInfo = await octokit.rest.users.getByUsername({
username: owner,
});
const userType = userInfo.data.type;
log("βΉοΈ", `Owner type is ${userType}`);
for (const pkg of packages) {
let allVersions;
try {
if (userType === "Organization") {
allVersions = await octokit.paginate(
octokit.rest.packages.getAllPackageVersionsForPackageOwnedByOrg,
{
org: owner,
package_type: pkgType,
package_name: pkg,
per_page: 100,
}
);
} else {
allVersions = await octokit.paginate(
octokit.rest.packages.getAllPackageVersionsForPackageOwnedByUser,
{
username: owner,
package_type: pkgType,
package_name: pkg,
per_page: 100,
}
);
}
} catch (error) {
if (error instanceof RequestError && error.status === 404) {
log("π", `Package ${pkg} not found. Skipping...`, "warn");
} else {
log(
"π",
`An error occurred while listing package versions for "${pkg}"`,
"error"
);
throw error;
}
}
if (allVersions === undefined) {
continue;
}
const versionsToDelete = allVersions.filter((version) =>
versions.includes(version.name)
);
if (versionsToDelete.length === 0) {
log("βΉοΈ", `Package ${pkg} has no matching versions. Skipping...`);
continue;
}
let shouldDeletePackage = false;
for (const version of versionsToDelete) {
log(
"π",
`Deleting package version id=${version.id} name=${version.name} for package ${pkg}`
);
try {
if (userType === "Organization") {
await octokit.rest.packages.deletePackageVersionForOrg({
package_type: pkgType,
package_name: pkg,
package_version_id: version.id,
org: owner,
});
} else {
await octokit.rest.packages.deletePackageVersionForUser({
package_type: pkgType,
package_name: pkg,
package_version_id: version.id,
username: owner,
});
}
log(
"β
",
`Deleted package version id=${version.id} name=${version.name} for package ${pkg}`
);
} catch (error) {
if (error instanceof RequestError) {
if (error.status === 404) {
log(
"π",
`Package version ${version.id} does not exist for package ${pkg}. Skipping...`
);
} else if (error.message.includes("last version of a package")) {
log(
"βΉοΈ",
`Last version of package ${pkg} cannot be deleted, will delete the package later.`
);
shouldDeletePackage = true;
} else {
log(
"π",
`An error occurred while deleting package version id=${version.id} name=${version.name} for package ${pkg}`,
"error"
);
throw error;
}
} else {
log(
"π",
`An error occurred while deleting package version id=${version.id} name=${version.name} for package ${pkg}`,
"error"
);
throw error;
}
}
}
if (shouldDeletePackage) {
log(
"π",
`Deleting package ${pkg} since there's only one version left to delete`
);
try {
if (userType === "Organization") {
await octokit.rest.packages.deletePackageForOrg({
package_type: pkgType,
package_name: pkg,
org: owner,
});
} else {
await octokit.rest.packages.deletePackageForUser({
package_type: pkgType,
package_name: pkg,
username: owner,
});
}
log("β
", `Deleted package ${pkg}`);
} catch (error) {
if (error instanceof RequestError && error.status === 404) {
log("π", `Package ${pkg} does not exist. Skipping...`);
} else {
log("π", `An error occurred while deleting package ${pkg}`, "error");
throw error;
}
}
}
}
}
/**
* 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 getOwner(): string {
const inputOwner = getInput("owner");
if (inputOwner) {
return inputOwner;
} else {
// This default should only happen when no input repo at all is provided.
return context.repo.owner;
}
}
/**
* 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 githubToken = getInput("github_token");
const packageType = getInput("package_type");
const packages = getInput("packages").split(",");
const versions = getInput("versions").split(",");
const owner = getOwner();
const octokit = getOctokit(githubToken);
return {
octokit,
githubToken,
owner,
packageType,
packages,
versions,
};
}
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 { githubToken, octokit, owner, packageType, packages, versions } =
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(packageType, "no tag name provided as an input.");
validateInputField(githubToken, "no Github token provided");
validateInputField(owner, "An invalid owner was provided!");
validateInputField(packages != null, "Invalid packages provided!");
validateInputField(versions != null, "Invalid versions provided!");
log("π", `given owner is "${owner}"`);
log("π", `given packageType is "${packageType}"`);
log("π", `given packages are "${packages}"`);
log("π·", `given versions are "${versions}"`);
await deletePackageVersions(octokit, owner, packageType, packages, versions);
}