-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean_up_packages.js
150 lines (135 loc) · 4.67 KB
/
clean_up_packages.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
function getLinkToNextPage(headerObj) {
/**
* Takes the Header object and returns the URL to the next page or null if no such page exists.
*
* Format of link_header: '<URL>; rel="next", <URL>; rel="last"' where URL is a URL to a certain page.
*/
if (!headerObj.has("link")) {
return null;
}
const linkHeader = headerObj.get("link");
const splittedLinkHeader = linkHeader.trim().split(",");
for (let i = 0; i < splittedLinkHeader.length; i++) {
const splittedHeaderParts = splittedLinkHeader[i].trim().split(";");
const refName = splittedHeaderParts[1].trim();
const refLink = splittedHeaderParts[0].trim();
if (refName === 'rel="next"') {
// The link is represented as '<url>'. This chops off the '<' and '>' characters.
return refLink.slice(1, refLink.length - 1);
}
}
return null;
}
async function getOldDevVersions() {
/**
* Return a list of development packages that is older than 30 days.
* The list of packages return by this method is not necessarily complete.
* This is intentional, because we don't want to execute too many requests at once.
*/
let queryUrl =
"https://api.github.com/orgs/inmanta/packages/npm/web-console/versions?per_page=100";
let result = [];
while (true) {
// This API endpoint sorts the packages from new to old.
const queryResults = await fetch(
queryUrl,
{
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
method: "GET",
},
(error) => console.log(error),
);
const packages = await queryResults.json();
const response_headers = queryResults.headers;
if (!packages || !packages.length) {
throw Error("Unable to find web-console packages");
}
// Take the dev versions only
const devVersions = packages.filter((pkg) => pkg.name.includes("dev"));
const devVersionsWithDate = devVersions.map((devPackageVersion) => {
return {
id: devPackageVersion.id,
version: devPackageVersion.name,
updatedAt: devPackageVersion.updated_at,
};
});
// Take the versions that are older than 30 days
const oldDevVersions = devVersionsWithDate.filter(
(devPackageVersion) =>
Math.floor(
(new Date() - new Date(devPackageVersion.updatedAt)) /
(1000 * 60 * 60 * 24),
) > 30,
);
if (result.length > 0 && oldDevVersions.length === 0) {
// We probably cleaned up until here during the previous run of this script.
// Don't go further back in history to limit the amount of requests.
return result;
}
result = result.concat(oldDevVersions);
if (result.length > 5) {
// Return as soon as we have at least five packages to not execute too many requests at once.
return result;
}
// Check to see if another page should be queried
const linkToNextPage = getLinkToNextPage(response_headers);
if (linkToNextPage != null) {
queryUrl = linkToNextPage;
console.log(`Fetching the next page from ${queryUrl}`);
} else {
if (result.length === 0) {
console.log("No dev version exist");
}
return result;
}
}
}
getOldDevVersions().then(
(oldDevVersions) => {
if (oldDevVersions.length == 0) {
console.log("No old versions found matching the criteria");
return;
}
// Make sure we're not starting too many requests at once
if (oldDevVersions.length > 5) {
oldDevVersions = oldDevVersions.slice(0, 5);
}
oldDevVersions.map((oldDevVersion) => {
const idToDelete = oldDevVersion.id;
console.log(
`Attempting to delete version ${oldDevVersion.version} with id ${idToDelete} from ${oldDevVersion.updatedAt}`,
);
fetch(
`https://api.github.com/orgs/inmanta/packages/npm/web-console/versions/${idToDelete}`,
{
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
method: "DELETE",
},
).then(
(response) => {
if (response.ok) {
console.log(`Successfully deleted package ${idToDelete}`);
} else {
const error_message = `Deleting package ${idToDelete} failed: ${response.statusText}`;
console.log(error_message);
throw Error(error_message);
}
},
(reason) => {
console.log(reason);
},
);
});
},
(reason) => {
console.log(`Failed to retrieve dev packages: ${reason}`);
},
);