-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
128 lines (110 loc) · 3.65 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
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
const express = require('express');
const Badge = require('badgeit');
const parse = require('git-url-parse');
const path = require('path');
const serveStatic = require('serve-static');
const compression = require('compression');
const bodyParser = require('body-parser');
const Coverage = require('./lib/coverage');
const port = process.env.PORT || 8080;
const asyncMiddleware = (fn) => {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
};
const app = express();
app.use(bodyParser.json());
app.use(compression());
app.use(serveStatic(path.resolve(__dirname, 'dist'), {
maxAge: '1d',
etag: false
}));
app.post('/api/upload', asyncMiddleware(async (req, res) => {
let { git, source_files, service_job_id, service_pull_request, service_name } = req.body;
// Make sure the remote url is set correctly
git.remotes.url = parse(parse(git.remotes.url).toString("ssh")).toString("https");
try {
const results = await Coverage.save({
source_files,
git,
service_job_id,
service_pull_request,
service_name
});
res.send({ results });
} catch(error) {
res.status(500);
res.send({ error });
}
}));
app.get('/api/repos', asyncMiddleware(async (req, res) => {
try {
const repos = await Coverage.repos();
res.send(repos);
} catch(error) {
res.status(500);
res.send({ error });
}
}));
app.get('/api/feed/:page?', asyncMiddleware(async (req, res) => {
const { page } = req.params;
try {
const feed = await Coverage.feed(page);
res.send(feed);
} catch(error) {
res.status(500);
res.send({ error });
}
}));
app.get('/api/repos/:service/:owner/', asyncMiddleware(async (req, res) => {
const { service, owner } = req.params;
try {
const coverages = await Coverage.repos(`${service}/${owner}`);
res.send(coverages);
} catch(error) {
console.log(error);
res.status(500);
res.send({ error });
}
}));
app.get('/api/coverage/:service/:owner/:repo/', asyncMiddleware(async (req, res) => {
const { limit } = req.query;
const { service, owner, repo } = req.params;
try {
const coverages = await Coverage.get(`${service.replace(/%2E/g, '.')}/${owner}/${repo}`, limit);
res.send(coverages);
} catch(error) {
console.log(error)
res.status(500);
res.send({ error });
}
}));
app.get('/badge/:service/:owner/:repo.svg', asyncMiddleware(async (req, res) => {
const { service, owner, repo } = req.params;
try {
const { history } = await Coverage.get(`${service.replace(/%2E/g, '.')}/${owner}/${repo}`, 1);
const { source_files } = history[0];
let found = 0;
let hit = 0;
source_files.forEach((file) => {
const { lines={hit: 0, found: 0}, branches={hit: 0, found: 0}, functions={hit: 0, found: 0} } = file;
found += lines.found + branches.found + functions.found;
hit += lines.hit + branches.hit + functions.hit;
});
const percentage = parseInt((hit / found) * 100);
const color = percentage >= 85 ? '#3DB712' : percentage <= 85 && percentage >= 70 ? '#caa300' : '#cc5338';
const badge = await Badge({ color: { right: color }, text: ['coverage', `${percentage}%`] });
res.set('Content-Type', 'image/svg+xml; charset=utf-8');
res.send(badge);
} catch(error) {
const badge = await Badge({ color: { right: "#b63b3b" }, text: ['coverage', 'not found'] });
res.set('Content-Type', 'image/svg+xml; charset=utf-8');
res.send(badge);
}
}));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
app.listen(port, function () {
console.log(`lcov-server is listening on http://localhost:${port}`); // eslint-disable-line
});