This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
forked from bots-house/ghcr-delete-image-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
132 lines (113 loc) · 3.11 KB
/
utils.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
const core = require("@actions/core");
/**
* Parse input from env.
* @returns Config
*/
let getConfig = function () {
const config = {
owner: core.getInput("owner", { required: true }),
name: core.getInput("name", { required: true }),
token: core.getInput("token", { required: true }),
// optional, mutual exclusive options
tag: core.getInput("tag") || null,
untaggedKeepLatest: core.getInput("untagged-keep-latest") || null,
untaggedOlderThan: core.getInput("untagged-older-than") || null,
};
const definedOptionsCount = [
config.tag,
config.untaggedKeepLatest,
config.untaggedOlderThan,
].filter((x) => x !== null).length;
if (definedOptionsCount == 0) {
throw new Error("no any required options defined");
} else if (definedOptionsCount > 1) {
throw new Error("too many selectors defined, use only one");
}
if (config.untaggedKeepLatest) {
if (
isNaN((config.untaggedKeepLatest = parseInt(config.untaggedKeepLatest)))
) {
throw new Error("untagged-keep-latest is not number");
}
}
if (config.untaggedOlderThan) {
if (
isNaN((config.untaggedOlderThan = parseInt(config.untaggedOlderThan)))
) {
throw new Error("untagged-older-than is not number");
}
}
return config;
};
let findPackageVersionByTag = async function (octokit, owner, name, tag) {
const tags = new Set();
for await (const pkgVer of iteratePackageVersions(octokit, owner, name)) {
const versionTags = pkgVer.metadata.container.tags;
if (versionTags.includes(tag)) {
return pkgVer;
} else {
versionTags.map((item) => {
tags.add(item);
});
}
}
throw new Error(
`package with tag '${tag}' does not exits, available tags: ${Array.from(
tags
).join(", ")}`
);
};
let findPackageVersionsUntaggedOrderGreaterThan = async function (
octokit,
owner,
name,
n
) {
const pkgs = [];
for await (const pkgVer of iteratePackageVersions(octokit, owner, name)) {
const versionTags = pkgVer.metadata.container.tags;
if (versionTags.length == 0) {
pkgs.push(pkgVer);
}
}
pkgs.sort((a, b) => {
return new Date(b.updated_at) - new Date(a.updated_at);
});
return pkgs.slice(n);
};
let iteratePackageVersions = async function* (octokit, owner, name) {
for await (const response of octokit.paginate.iterator(
octokit.rest.packages.getAllPackageVersionsForPackageOwnedByOrg,
{
package_type: "container",
package_name: name,
org: owner,
state: "active",
per_page: 100,
}
)) {
for (let packageVersion of response.data) {
yield packageVersion;
}
}
};
let deletePackageVersion = async (octokit, owner, name, versionId) => {
await octokit.rest.packages.deletePackageVersionForOrg({
package_type: "container",
package_name: name,
org: owner,
package_version_id: versionId,
});
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
module.exports = {
getConfig,
findPackageVersionByTag,
deletePackageVersion,
findPackageVersionsUntaggedOrderGreaterThan,
sleep,
};