This repository was archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean-data.js
171 lines (155 loc) · 4.25 KB
/
clean-data.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
"use strict";
/***************************************************************************
*
* (C) Copyright IBM Corp. 2018
*
* This program and the accompanying materials are made available
* under the terms of the Apache License v2.0 which accompanies
* this distribution.
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* Contributors:
* Multiple authors (IBM Corp.) - initial implementation and documentation
***************************************************************************/
const request = require("request-promise");
const HOST = process.env.HOST;
const TOKEN = process.env.TOKEN;
const LOOKUP = require("./lookup.json");
function getReq (url, filterList) {
return request.get({
uri: url,
headers: {
filter: JSON.stringify(filterList) || null
},
followAllRedirects: true
}, (err, res, body) => {
if (err || res.statusCode !== 200) {
process.exit(1);
} else {
return body;
}
});
}
function deleteReq(url) {
return request.del({
uri: url,
headers: {
Authorization: TOKEN
},
followAllRedirects: true,
json: true
}, (err, res) => {
if (err) {
return process.exit(1);
} else {
return res.statusCode;
}
});
}
// ID's to delete
let modules = new Set();
let moduleVersions = new Set();
let moduleVersionTests = new Set();
let moduleRecommendedLTSs = new Set();
function runDeletes () {
console.log(`Deleting ${moduleVersions.size} modules`);
modules.forEach((id) => {
deleteReq(`http://${HOST}/api/Modules/${id}`);
});
console.log(`Deleting ${moduleVersions.size} orphan module versions`);
moduleVersions.forEach((id) => {
deleteReq(`http://${HOST}/api/ModuleVersions/${id}`);
});
console.log(`Deleting ${moduleVersionTests.size} ` +
"orphan module version tests");
moduleVersionTests.forEach((id) => {
deleteReq(`http://${HOST}/api/ModuleVersionTests/${id}`);
});
console.log(`Deleting ${moduleRecommendedLTSs.size} ` +
"orphan module recommended LTSs");
moduleRecommendedLTSs.forEach((id) => {
deleteReq(`http://${HOST}/api/ModuleRecommendedLTSs/${id}`);
});
}
const filter = {
"include": [
{"ModuleVersion": "Module"},
"Architecture",
"Distribution",
"OperatingSystem"
]
};
async function cleanup () {
const MVTS = JSON.parse(await getReq(`http://${HOST}/api/ModuleVersionTests`,
filter));
for (let mvt of MVTS) {
// Check ModuleVersion and Module
if (!mvt.ModuleVersion) {
if (!moduleVersionTests.has(mvt.id)) {
moduleVersionTests.add(mvt.id);
}
} else {
if (!mvt.ModuleVersion.Module) {
if (!moduleVersionTests.has(mvt.id)) {
moduleVersionTests.add(mvt.id);
}
if (!moduleVersions.has(mvt.ModuleVersion.id)) {
moduleVersions.add(mvt.ModuleVersion.id);
}
}
}
// Check Architecture
if (!mvt.Architecture) {
if (!moduleVersionTests.has(mvt.id)) {
moduleVersionTests.add(mvt.id);
}
}
// Check Distribution
if (!mvt.Distribution) {
if (!moduleVersionTests.has(mvt.id)) {
moduleVersionTests.add(mvt.id);
}
}
// Check OperatingSystem
if (!mvt.OperatingSystem) {
if (!moduleVersionTests.has(mvt.id)) {
moduleVersionTests.add(mvt.id);
}
}
}
// Find orphan ModuleVersions
const MODULE_VERSIONS = JSON.parse(
await getReq(`http://${HOST}/api/ModuleVersions`, {"include": "Module"})
);
for (let moduleVersion of MODULE_VERSIONS) {
if (!moduleVersion.Module) {
if (!moduleVersions.has(moduleVersion.id)) {
moduleVersions.add(moduleVersion.id);
}
}
}
const MODULE_REC_LTS = JSON.parse(
await getReq(`http://${HOST}/api/ModuleRecommendedLTSs`,
{"include":"Module"}));
for (let mrlts of MODULE_REC_LTS) {
// Check Module
if (!mrlts.Module) {
if (!moduleRecommendedLTSs.has(mrlts.id)) {
moduleRecommendedLTSs.add(mrlts.id);
}
}
}
const MODULES = JSON.parse(
await getReq(`http://${HOST}/api/Modules`));
MODULES.forEach(module => {
if (!LOOKUP[module.name]) {
if (!modules.has(module.id)) {
modules.add(module.id);
}
}
});
runDeletes();
}
cleanup();