-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (48 loc) · 1.52 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
const core = require('@actions/core');
const { Octokit } = require("@octokit/rest");
const accessToken = core.getInput('access-token');
const packageName = core.getInput('package-name');
const versionsToKeep = core.getInput('versions-to-keep');
try {
const octokit = new Octokit({
auth: accessToken
});
console.info("find versions to delete");
const versionsToDelete = findVersionsToDelete(octokit)
.catch(e => {
console.error(e);
throw new Error(e.message);
});
versionsToDelete.then(versions => {
console.info('going to delete the follwing versions: ', versions);
versions.forEach(version => {
deleteOldVersion(version, octokit);
});
});
} catch (error) {
core.setFailed(error.message);
}
async function findVersionsToDelete(octokit) {
let response = await octokit.rest.packages.getAllPackageVersionsForPackageOwnedByAuthenticatedUser({
package_type: 'container',
package_name: packageName,
});
const data = response.data;
let versions = new Map();
data.forEach(version => {
const updated = new Date(version.created_at);
versions.set(updated, version.id);
});
const sortedVersions = [...versions]
.sort((o1, o2) => o2[0].getTime() - o1[0].getTime())
.map(tuple => tuple[1]);
return sortedVersions.splice(versionsToKeep);
}
async function deleteOldVersion(version, octokit) {
console.info("... deleting version ", version);
await octokit.rest.packages.deletePackageVersionForAuthenticatedUser({
package_type: 'container',
package_name: packageName,
package_version_id: version
});
}