-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
executable file
·50 lines (46 loc) · 1.64 KB
/
server.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
#! /usr/bin/env node
var request = require('request')
var express = require('express')
var app = express()
app.get('/jenkins/c/http/*', function(req,res) {
var jurl = req.params[0]
var url = 'http://' + jurl + '/lastSuccessfulBuild/cobertura/api/json/?depth=2'
request(url, function(err, response, body) {
if (!err && response.statusCode == 200) {
var elements = JSON.parse(body)['results']['elements']
for (var i in elements) {
if (elements[i]['name'] == 'Lines') {
var cov = elements[i]['ratio'].toFixed(2)
var color = function(cov) {
if (cov < 20) {
return 'red'
} else if (cov < 80) {
return 'yellow'
} else {
return 'brightgreen'
}
}(cov)
var badge_url = 'https://img.shields.io/badge/coverage-' + cov.toString() + '%-' + color + '.svg'
var style = req.param("style")
if (typeof style != 'undefined') {
badge_url += '?style=' + style
}
console.log('[GET] ' + '/jenkins/c/http/' + jurl)
console.log(' generating badge(' + badge_url + ')')
res.redirect(badge_url)
}
}
} else {
console.log(err)
var badge_url = 'https://img.shields.io/badge/coverage-none-lightgrey.svg'
console.log('[GET] ' + '/jenkins/c/http/' + jurl)
console.log(' generating badge(' + badge_url + ')')
res.redirect(badge_url)
}
})
})
var port = process.argv.slice(2)[0];
if (!port) port = 9913
var server = app.listen(port, function() {
console.log('Listening on port %d...', server.address().port)
})