-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-versions.js
142 lines (134 loc) · 3.09 KB
/
delete-versions.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
const { info } = require("@actions/core");
const graphql = require("./graphql");
const queries = require("./queries");
async function findAll(
token,
query,
params,
findObject,
elements = [],
cursor = null
) {
while (true) {
info("a new page grab");
let rsp = await graphql(token, query, {
...params,
after: cursor,
});
const obj = findObject(rsp);
elements.push(...obj.edges.map((edge) => edge.node));
const pageInfo = obj.pageInfo;
if (!pageInfo.hasNextPage) {
break;
}
cursor = pageInfo.endCursor;
}
return elements;
}
async function findAllPackageVersions(token, package, owner, repo) {
info(
"Complete package versions data JSON: " + JSON.stringify(package.versions)
);
let versions = package.versions.edges.map((edge) => edge.node);
const pageInfo = package.versions.pageInfo;
if (pageInfo.hasNextPage) {
info("starts finding packages by pages");
await findAll(
token,
queries.packageVersions,
{
owner,
repo,
package: package.name,
},
(rsp) => rsp.repository.packages.edges[0].node.versions,
versions,
pageInfo.endCursor
);
}
info(
"Log all found versions in findAllPackageVersions: " +
JSON.stringify(versions)
);
return versions;
}
async function deletePackageVersions(
token,
package,
owner,
repo,
keepCnt,
dryRun
) {
const versions = await findAllPackageVersions(token, package, owner, repo);
const majorVersions = {};
versions.forEach((v) => {
const mv = v.version.split(".")[0];
if (!majorVersions[mv]) {
majorVersions[mv] = [];
}
majorVersions[mv].push(v);
});
const toDelete = [];
for (const key in majorVersions) {
let vs = majorVersions[key];
vs = vs.reverse();
if (vs.length <= keepCnt) {
continue;
}
vs = vs.slice(0, vs.length - keepCnt);
vs.forEach((v) => {
toDelete.push(v);
});
}
const deleted = [];
for (let i = 0; i < toDelete.length; i++) {
// on dryRun just add as deleted
if (dryRun) {
deleted.push(toDelete[i]);
continue;
}
const rsp = await graphql(token, queries.deletePackageVersion, {
packageVersionId: toDelete[i].id,
headers: {
Accept: "application/vnd.github.package-deletes-preview+json",
},
});
if (rsp.deletePackageVersion.success) {
deleted.push(toDelete[i]);
}
}
return deleted;
}
module.exports = async function (inputs) {
let packages = await findAll(
inputs.token,
queries.packages,
{
owner: inputs.owner,
repo: inputs.repo,
},
(rsp) => rsp.repository.packages
);
packages = packages.filter((p) => {
return !p.name.startsWith("deleted_");
});
const deleted = [];
for (let i = 0; i < packages.length; i++) {
const vs = await deletePackageVersions(
inputs.token,
packages[i],
inputs.owner,
inputs.repo,
inputs.keepCnt,
inputs.dryRun
);
if (vs.length > 0) {
deleted.push({
name: packages[i].name,
versions: vs,
});
}
}
return deleted;
};