-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (40 loc) · 1.4 KB
/
index.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
const fs = require('fs');
const core = require('@actions/core');
const glob = require('glob');
const fetch = require('node-fetch');
const { reportAction } = require('@gh-stats/reporter');
const host = core.getInput('host');
process.on('unhandledRejection', up => {
core.setFailed(`Action failed ${up}`);
});
if (!fs.existsSync('monitoring_templates')) {
core.setFailed('"monitoring_templates" directory not found!');
}
glob('monitoring_templates/**/*.y*ml', async (er, files) => {
if (er) throw new er;
const results = await Promise.all(files.map((file) => {
if (file.endsWith('/properties.yaml')) {
return verifyFile(file, true);
} else {
return verifyFile(file);
}
}));
results.forEach(result => {
if (!result.response.valid) {
core.setFailed(`Configuration invalid: ${result.file}\n${JSON.stringify(result.response, null, 2)}`);
}
});
});
function verifyFile(file, isProperties = false) {
const url = isProperties ? `https://${host}/api/monitoring-templates/validate-properties` : `https://${host}/api/monitoring-templates/validate`;
return fetch(url, {
body: fs.readFileSync(file),
headers: {
'Content-Type': 'application/yaml'
},
method: 'POST'
})
.then(response => response.json())
.then(response => ({ response, file }));
}
reportAction();