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 pathupdate-test.js
121 lines (112 loc) · 3.11 KB
/
update-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
"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 [OS, ARCH, DISTRO] = [process.env.OS, process.env.ARCH,
process.env.DISTRO];
const [moduleName, moduleVersion] = [process.env.MODULE,
process.env.MODULE_VERSION];
const nodeVersion = process.env.NODE_VERSION;
const HOST = process.env.HOST;
const TOKEN = process.env.TOKEN;
const passResult = (parseInt(process.env.EXIT_STATUS) === 0);
if (TOKEN === null || TOKEN === "Incorrect Credentials") {
console.error("TOKEN is not defined");
process.exit(1);
}
console.log(`\nUpdating HOST: ${HOST}` +
`\nModule: ${moduleName},` +
`Module Verison: ${moduleVersion},` +
`Node Version: ${nodeVersion}` +
`\nPass: ${passResult}`
);
/*
Catch an error if coverage-summary.json
doesn't exist and set codeCoverage to 0
*/
let codeCoverage;
try {
const coverageSummary = require(`${process.env.WORKSPACE}/` +
`${moduleName}/coverage-summary.json`);
codeCoverage = parseInt(
coverageSummary.total.lines.pct.toString().split(".")[0]
);
console.log("Coverage summary:", coverageSummary);
} catch (e) {
codeCoverage = 0;
}
function getModuleVersionTestID() {
return request.get({
url: `http://${HOST}/api/ModuleVersionTests`,
followAllRedirects: true,
headers: {
filter: JSON.stringify({
"where": {
"module": moduleName,
"module_version": moduleVersion,
"node_version": nodeVersion,
"os": OS,
"arch": ARCH,
"distro": DISTRO
}
})
},
json: true
}, (err, res, body) => {
if (err || res.statusCode !== 200) {
console.error(body.error);
process.exit(1);
} else {
return body;
}
});
}
async function patchModuleVersionTest () {
const id = await getModuleVersionTestID();
let data = {
"module": moduleName,
"module_version": moduleVersion,
"node_version": nodeVersion,
"os": OS,
"arch": ARCH,
"distro": DISTRO,
"passed": passResult,
"code_coverage": codeCoverage || 0
};
if (id[0] && id[0].id) {
data.id = id[0].id;
}
return request.patch({
url: `http://${HOST}/api/ModuleVersionTests`,
followAllRedirects: true,
headers: {
"Authorization": TOKEN
},
body: data,
json: true // Automatically stringifies the body to JSON
}, (err, res, body) => {
if (err || res.statusCode !== 200) {
console.error(body.error);
process.exit(1);
} else {
console.log("Adding Module Version Test :", body);
return body;
}
});
}
async function updateTest() {
await patchModuleVersionTest();
}
updateTest();