-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (40 loc) · 1.73 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
var path = require('path');
var express = require('express');
var app = express();
var request = require('request');
var config = require('./config/config');
//TODO Send the error in the response
var fetchFromGocd = function(resFromServer){
request({url: config.gocd_url + "/go/dashboard.json", json: true}, function (error, response, dashboard) {
if (error || response.statusCode !== 200) {
return {error: error};
}
var pipelines = dashboard.map(pg => pg.pipelines).reduce((acc, p) => acc.concat(p));
var interestedPipelines = pipelines.filter(p => config.interests.indexOf(p.name) > -1);
if (interestedPipelines.length == 0) {
return {error: "No matching pipelines found, check config.js"};
}
return resFromServer.send({pipelines: interestedPipelines});
});
};
var fetchJobsFromGocd = function(resFromServer, pipeline, stage, pipelineCounter, stageCounter) {
var stageDetailsUrl = [config.gocd_url, "go/api/stages", pipeline, stage, "instance", pipelineCounter, stageCounter ].join("/");
request({url: stageDetailsUrl, json: true}, function (error, response, stageDetails) {
if (error || response.statusCode !== 200) {
return {error: error};
}
return resFromServer.send({jobs: stageDetails.jobs});
});
}
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/dashboard.json', function(req, res){
fetchFromGocd(res);
});
app.get('/stageDetails.json', function (req, res) {
fetchJobsFromGocd(res, req.query.pipeline, req.query.stage, req.query.pipelineCounter, req.query.stageCounter);
});
var server = app.listen(config.port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});