-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
63 lines (51 loc) · 1.87 KB
/
app.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
'use strict';
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const extend = require('util')._extend;
const validUrl = require('valid-url');
const format = require('string-template');
const BADGE_URL = 'https://img.shields.io/badge/{subject}-{label}-{color}.svg?style={style}';
// default parameters
const DEF_BADGE_PARAMS = {
label: 'Unknown',
color: 'lightgrey',
style: 'flat'
};
// jenkins plugins
const plugins = {
cobertura: require(__dirname + '/plugins/cobertura'),
jacoco: require(__dirname + '/plugins/jacoco')
};
// Configure Express
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// list plugins and methods
app.get('/', function(req, res) {
const availablePlugins = extend({}, plugins);
Object.keys(plugins).forEach(function(key) {
availablePlugins[key] = Object.keys(plugins[key]).map(function(m) { return m;});
});
res.json({plugins: availablePlugins});
});
//cobertura
app.get('/:plugin/:fn', function(req, res) {
if (!plugins[req.params.plugin]) {
res.status(404).send('Jenkins plugin not found: ' + req.params.plugin);
return;
} else if (typeof(plugins[req.params.plugin][req.params.fn]) !== 'function') {
res.status(404).send('Unknown method: '+ req.params.fn +' for plugin: ' + req.params.plugin);
return;
} else if (!req.query.job && !validUrl.isWebUri(req.query.job)) {
res.status(400).send('Missing query parameter: job, it must be a valid HTTP/HTTPS url');
return;
}
// Call plugin#fn() and return the badge
plugins[req.params.plugin][req.params.fn](req.query, function(err, badge) {
// override default parameters with the result from: plugin.fn()
badge.subject = req.params.fn;
const badgeParams = extend(DEF_BADGE_PARAMS, badge);
return res.redirect(format(BADGE_URL, badgeParams));
});
});
module.exports = app;