This repository has been 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
/
missing-test.js
330 lines (304 loc) · 9.17 KB
/
missing-test.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"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 {
spawnSync
} = require("child_process");
const request = require("request-promise");
const fs = require("fs");
const path = require("path");
const lookup = JSON.parse(fs.readFileSync(
path.join(__dirname, "/lookup.json")
));
const HOST = process.env.HOST;
const TOKEN = process.env.TOKEN;
const TIMESTAMP = new Date();
let params = [];
if (TOKEN === null || TOKEN === "Incorrect Credentials") {
console.error("TOKEN is not defined");
process.exit(1);
}
function check(e, r, b) {
if (e || r.statusCode !== 200) {
console.error(b.error);
process.exit(1);
} else {
return b;
}
}
/*
Find missing test results for the latest module_version and
latest node_version (per LTS).
*/
function getLatestNodeVersion() {
return request.get({
url: `http://${HOST}/api/LTSVersions/latestNodeVersion`,
followAllRedirects: true
}, (err, res, body) => {
check(err, res, body);
});
}
// Fetch the list of valid platforms
function getValidPlatforms() {
return request.get({
url: `http://${HOST}/api/ValidPlatforms`,
followAllRedirects: true
}, (err, res, body) => {
check(err, res, body);
});
}
// Adds a new node version to the DB
function addNodeVersion(nodeVersion) {
let LTSVersion = nodeVersion.split(".")[0].replace("v", "");
request.post({
url: `http://${HOST}/api/NodeVersions`,
followAllRedirects: true,
headers: {
"Authorization": TOKEN
},
body: {
"node_version": nodeVersion,
"lts_version": LTSVersion
},
json: true // Automatically stringifies the body to JSON
}, (err, res, body) => {
check(err, res, body);
});
}
function getModule(moduleName) {
return request.get({
url: `http://${HOST}/api/Modules`,
headers: {
filter: JSON.stringify({
"where": {
"name": moduleName
}
})
},
followAllRedirects: true
}, (err, res, body) => {
check(err, res, body);
});
}
// Adds a new module version to the DB
function addModule(moduleName) {
return request.post({
url: `http://${HOST}/api/Modules`,
followAllRedirects: true,
headers: {
"Authorization": TOKEN
},
body: {
"name": moduleName
},
json: true // Automatically stringifies the body to JSON
}, (err, res, body) => {
check(err, res, body);
});
}
async function getModuleVersion(moduleName, moduleVersion) {
const module = await getModule(moduleName);
const moduleID = (module) ? JSON.parse(module)[0].id : null;
return request.get({
url: `http://${HOST}/api/ModuleVersions`,
headers: {
filter: JSON.stringify({
"where": {
"module_id": moduleID,
"module_version": moduleVersion
}
})
},
followAllRedirects: true,
json: true
}, (err, res, body) => {
check(err, res, body);
});
}
function addModuleVersion(moduleName, moduleVersion) {
let license = spawnSync("npm", ["view", moduleName,
"license"
]).stdout.toString().trim();
return request.post({
url: `http://${HOST}/api/ModuleVersions`,
followAllRedirects: true,
headers: {
"Authorization": TOKEN
},
body: {
"module": moduleName,
"module_version": moduleVersion,
"license": license || null,
"min_dep_license": license || null,
"timestamp": TIMESTAMP
},
json: true // Automatically stringifies the body to JSON
}, (err, res, body) => {
check(err, res, body);
});
}
function addRecommendedVersions(moduleName) {
let recMajorVersions = lookup[moduleName]["recommended-major-versions"];
for (let recVersion in recMajorVersions) {
let ltsVersion = recVersion.split(".")[0];
request.post({
url: `http://${HOST}/api/ModuleRecommendedLTSs`,
followAllRedirects: true,
headers: {
"Authorization": TOKEN
},
body: {
"lts_version": ltsVersion,
"module": moduleName,
"recommended_version": recMajorVersions[recVersion]
},
json: true // Automatically stringifies the body to JSON
}, (err) => {
if (err) {
return console.error(err);
}
});
}
}
// Fetch all tests for each module
function getModuleVersionTests(moduleName, moduleVersion, ltsVersion) {
let filter = {
"where": {
"module": moduleName,
"module_version": moduleVersion,
"node_version": ltsVersion.latestNodeVersion
},
"include": ["Architecture", "Distribution", "OperatingSystem"]
};
return request.get({
url: `http://${HOST}/api/ModuleVersionTests`,
followAllRedirects: true,
headers: {
filter: JSON.stringify(filter)
},
json: true
}, (err, res, body) => {
check(err, res, body);
});
}
async function missingTests() {
const ltsVersions = JSON.parse(await getLatestNodeVersion());
const validPlatforms = JSON.parse(await getValidPlatforms());
// Add a new Node Versions
if (ltsVersions[0].latestNodeVersion !== process.env.node6) {
await addNodeVersion(process.env.node6);
}
if (ltsVersions[1].latestNodeVersion !== process.env.node8) {
await addNodeVersion(process.env.node8);
}
if (ltsVersions[2].latestNodeVersion !== process.env.node10) {
await addNodeVersion(process.env.node10);
}
async function checkModuleExists(moduleName, moduleVersion) {
let moduleExists = await getModule(moduleName);
if (JSON.parse(moduleExists).length === 0) {
await addModule(moduleName);
await addModuleVersion(moduleName, moduleVersion);
await addRecommendedVersions(moduleName);
}
}
// Add a new Module Versions
let modules = [];
for (let i in Object.keys(lookup)) {
const moduleName = Object.keys(lookup)[i];
const moduleData = lookup[moduleName];
const moduleVersions = new Set();
// Add Latest Module Version
const latestModuleVersion =
spawnSync("npm", ["view", moduleName, "version"])
.stdout.toString().trim();
moduleVersions.add(latestModuleVersion);
// Add Latest Module Version for each recommended major version
const recMajorVersions = moduleData["recommended-major-versions"];
Object.keys(recMajorVersions).forEach(lts => {
const recMajVersion = recMajorVersions[lts];
const allVersionsForMajor =
spawnSync("npm", ["view", `${moduleName}@${recMajVersion}`, "version"])
.stdout.toString().trim().split(" ");
// Latest Module Version for Major
const moduleVersionForMajor =
allVersionsForMajor[allVersionsForMajor.length - 1].replace(/'/g, "");
// Add required module version is it isnt a duplicate
if (!moduleVersions.has(moduleVersionForMajor)) {
moduleVersions.add(moduleVersionForMajor);
}
});
// Array.from() converts a set to an Array
modules.push({
name: moduleName,
versions: Array.from(moduleVersions)
});
//Add ModuleVersions
await checkModuleExists(moduleName, latestModuleVersion);
moduleVersions.forEach(async moduleVersion => {
const resp = await getModuleVersion(moduleName, moduleVersion);
if (resp.length === 0) {
await addModuleVersion(moduleName, moduleVersion);
}
});
}
await findMissingTests();
console.log(params.join("%"));
async function findMissingTests() {
for (let i in modules) {
let module = modules[i];
for (let j in ltsVersions) {
let ltsVersion = ltsVersions[j];
for (let k in module.versions) {
let moduleVersion = module.versions[k];
const mvt =
await getModuleVersionTests(module.name, moduleVersion, ltsVersion);
if (mvt.length === 0) {
// Run on all platforms
params.push([module.name, moduleVersion,
ltsVersion.latestNodeVersion, "all", "all", "all"
]);
} else if (mvt.length !== validPlatforms.length) {
/*
Don't run if no missing files.
IF there are => for each valid platform check if
there is an existing test result
*/
validPlatforms.forEach(plat => {
let exist = false;
try {
mvt.forEach(test => {
if (test.OperatingSystem.os === plat.os &&
test.Architecture.arch === plat.arch &&
test.Distribution.distro === plat.distro) {
exist = true;
}
});
} catch (e) {
console.error(e);
}
if (!exist) {
params.push([module.name, moduleVersion,
ltsVersion.latestNodeVersion, plat.os, plat.arch, plat.distro
]);
}
});
}
}
}
}
}
}
missingTests();