-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.55 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
47
48
49
50
51
52
53
54
55
56
57
const fs = require('fs');
const express = require('express');
const multer = require('multer');
const path = require('path');
const rimraf = require('rimraf');
const unzip = require('unzip');
const PORT = 9012;
const app = express();
if (!fs.existsSync('./uploads')) fs.mkdirSync('./uploads');
if (!fs.existsSync('./coverage')) fs.mkdirSync('./coverage');
const zip = multer({dest:'./uploads/'});
app.get('/', (req, res) => {
const folders = fs.readdirSync('./coverage');
const links = folders.map((folderName) => {
return `<a href="/${folderName}">${folderName}</a><br />`;
});
res.send(
`<html>
<head>
<title>GPMDP Coverage Reports</title>
</head>
<body>
${links}
</body>
</html>`
);
});
app.post('/submit', zip.single('zip'), (req, res) => {
if (req.body.key !== (process.env.COVERAGE_ARTIFACT_KEY || '')) {
res.status(403);
res.json({error: 'Not authorized to upload coverage file'});
return;
}
const file = req.file;
const branch = (req.body.branch || 'Unknown Branch').replace(/\//g, '-');
// fs.rename(path.resolve(file.path), path.resolve(`${__dirname}/coverage/${branch}.zip`));
const branchDir = path.resolve(__dirname, 'coverage', branch);
rimraf(branchDir, (err) => {
if (err) return res.status(500).send();
const readStream = fs.createReadStream(file.path);
readStream.pipe(unzip.Extract({ path: branchDir }))
.on('close', () => res.status(200).send());
});
});
app.use('/', express.static('./coverage'))
app.listen(PORT, () => {
console.log('Listening on:', PORT)
})