This repository was archived by the owner on Jul 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (142 loc) · 4.8 KB
/
Copy pathindex.js
File metadata and controls
168 lines (142 loc) · 4.8 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var EXPRESS = require('express');
var PATH = require('path');
var LESS_MIDDLEWARE = require('less-middleware');
var NODE_REST_CLIENT = require('node-rest-client').Client;
var JENKINS = require('jenkins-api');
var QUERY_STRING = require('querystring');
var UNDERSCORE = require('underscore');
var CRYPTO = require('crypto');
var _ = UNDERSCORE;
var SonarQubeUrl = 'http://nvi-prd-jen01.nvisionit.co.za:9000';
var JenkinsUrl = 'http://nvi-prd-jen01.nvisionit.co.za:8080';
var ViewName = "All";
var DataCache = {};
var _express = EXPRESS();
var _jenkins = new NODE_REST_CLIENT();
_jenkins.registerMethod("List", JenkinsUrl + '/view/${viewName}/api/json?depth=0', 'GET');
_jenkins.registerMethod("Overview", JenkinsUrl + '/job/${resource}/api/json?depth=0', 'GET');
_jenkins.registerMethod("Scm", JenkinsUrl + '/job/${resource}/scm/api/json?depth=0', 'GET');
_jenkins.registerMethod("Build", JenkinsUrl + '/job/${resource}/lastBuild/api/json?depth=1', 'GET');
_jenkins = _jenkins.methods;
var _sonarqube = new NODE_REST_CLIENT();
_sonarqube.registerMethod("getMetrics", SonarQubeUrl + '/api/resources', 'GET');
_sonarqube = _sonarqube.methods;
_express.use(LESS_MIDDLEWARE(__dirname + '/public', {
dest: __dirname + '/public',
enable: ['less'],
force: true,
debug: true
}));
_express.use(EXPRESS.static(__dirname + '/public'));
_express.use('/bower_components', EXPRESS.static(__dirname + '/bower_components'));
_express.get('/api/list', function (req, res) {
_jenkins.List({
path: {
viewName: ViewName
}
}, function (data) {
_.each(data.jobs, function (item) {
var job_name = item.name;
var obj = DataCache[job_name];
if (!obj) {
obj = {
status: item.color,
name: item.name
};
DataCache[job_name] = obj;
}
});
res.json(DataCache);
}).on('error', function (err) {
console.log(err);
});
});
_express.get('/api/details', function (req, res) {
var job_name = req.query.name;
var obj = DataCache[job_name];
if (!obj) {
obj = {};
DataCache[job_name] = obj;
}
_jenkins.Overview({ path: { resource: job_name } },
function (data) {
obj.status = data.color;
obj.name = data.displayName;
obj.reports = _.pluck(data.healthReport, "description");
}).on('error', function (err) {
console.log(err);
});
_jenkins.Scm({ path: { resource: job_name } },
function (data) {
obj.scm = data.type;
}).on('error', function (err) {
console.log(err);
});
_jenkins.Build({ path: { resource: job_name } },
function (data) {
obj.busy = data.building;
obj.build_number = data.displayName;
obj.progress = (data.building) ? data.executor.progress : 100;
obj.result = data.result;
obj.timestamp = data.timestamp;
obj.gravatar = "http://www.gravatar.com/avatar/" + CRYPTO.createHash('md5').update(job_name).digest("hex") + "?s=72&d=identicon&r=PG";
obj.culprits = _.pluck(data.culprits, "fullName");
if (obj.culprits.length === 0) {
obj.culprits = _.chain(data.actions)
.pluck("causes")
.flatten()
.compact().pluck("userName").compact().value();
}
// Find recorded unit tests from the last build
{
obj.tests = _.find(data.actions, function (action) {
return action.urlName === "testReport";
});
}
// Jenkins (or the SonarQube plugin) makes it hard to guess what the resource name is.
// Its in the Actions list when it runs, and it has one field, a URL...
// So we cannot guess for sure, we need the SonarQube server URL defined,
// and a marker to start looking for the resource name.
{
var server = SonarQubeUrl.toLowerCase();
var idx = 'index/';
var sonarqube_url = _.chain(data.actions)
.pluck("url") // Get all URL fields
.compact() // Clear out the nulls
.find(function (url) { // Try and find the SonarQube URL
url = url.toLowerCase();
return url.indexOf(server) >= 0 && url.lastIndexOf(idx) > 0;
})
.value(); // Get the eventual value, if any.
if (sonarqube_url) {
var resource = sonarqube_url.substr(sonarqube_url.lastIndexOf(idx) + idx.length);
_sonarqube.getMetrics({
parameters: {
resource: resource,
metrics: 'ncloc,coverage,new_coverage,sqale_index,sqale_rating,sqale_debt_ratio'
},
headers: { "Accept": "application/json" }
}, function (data) {
data = data[0];
obj.sonarqube = _.chain(data.msr)
.map(function (item) {
var msr = [item.key, item.frmt_val];
return msr;
})
.object()
.value();
}).on('error', function (err) {
console.log(err);
});
}
}
}).on('error', function (err) {
console.log(err);
});
res.json(obj);
});
var server = _express.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});