forked from bots-house/ghcr-delete-image-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
181 lines (157 loc) · 4.47 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
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
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,
taggedKeepLatest: core.getInput("tagged-keep-latest") || null,
tagRegex: core.getInput("tag-regex") || null,
};
const definedOptionsCount = [
config.tag,
config.untaggedKeepLatest,
config.untaggedOlderThan,
config.taggedKeepLatest,
config.tagRegex
].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.taggedKeepLatest) {
if (
isNaN((config.taggedKeepLatest = parseInt(config.taggedKeepLatest)))
) {
throw new Error("tagged-keep-latest is not number");
}
if (!config.tagRegex)
throw new Error("regex must be provided when tagged-keep-latest set");
}
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 findPackageVersionsTagRegexMatchOrderGreaterThan = async function (
octokit,
owner,
name,
n,
regex
) {
const pkgs = [];
// const pkgVers = await iteratePackageVersions(octokit, owner, name);
for await (const pkgVer of iteratePackageVersions(octokit, owner, name)) {
core.info(`🔎 found pkgVer ${pkgVer.metadata.container.tags}...`);
const versionTags = pkgVer.metadata.container.tags;
if (regex && versionTags.length > 0) {
for (tag in versionTags) {
core.info(`🔎 found tag ${tag}...`);
if (!regex.test(tag))
continue;
core.info(`🔎 tag ${tag} matches. Deleting...`);
pkgs.push(pkgVer);
break;
}
}
}
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) {
core.info('found package!');
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,
findPackageVersionsTagRegexMatchOrderGreaterThan,
sleep,
};