Skip to content

Commit

Permalink
use jenkins-api for jenkinsDeployInfo
Browse files Browse the repository at this point in the history
fix MIME type problem with sonar
fix healthreport reporting in JenkinsInfo.class
  • Loading branch information
Gotier committed Feb 20, 2018
1 parent 8f6105e commit efb94b5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 89 deletions.
43 changes: 26 additions & 17 deletions front/assets/js/core/plugin/JenkinsInfo.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class JenkinsInfo extends GetAndFillInfo {
if(info.lastBuild != null) {
$(this.projectSelector + " [name='buildQuality']").text("OK");
$(this.projectSelector + " [name='buildQuality']").addClass("nice");
$(this.projectSelector + " [name='linkLastBuild']").attr("href", info.lastBuild.url);
}
else {
$(this.projectSelector + " [name='buildQuality']").text("NA");
Expand All @@ -38,26 +39,34 @@ class JenkinsInfo extends GetAndFillInfo {
else {
$(this.projectSelector + " [name='buildQuality']").text(info.lastBuild.number===info.lastFailedBuild.number ? "KO" : "OK");
$(this.projectSelector + " [name='buildQuality']").addClass(info.lastBuild.number===info.lastFailedBuild.number ? "bad" : "nice");
$(this.projectSelector + " [name='linkLastBuild']").attr("href", info.lastBuild.url);
}

if(info.healthReport.length >= 2)
$(this.projectSelector + " [name='detailBuild']").text(info.healthReport[1].description);
if(info.healthReport.length >= 1) {
let jenkinsHealthDescription = info.healthReport[0].description;
if (~jenkinsHealthDescription.indexOf("out of a total of")) {
let arrHealth = jenkinsHealthDescription.split("out of a total of");

let arrNbFailingTests = arrHealth[0].split("tests");
let arrNbTotalTests = arrHealth[1].split("tests");

let desc = arrNbFailingTests[0] + "/" + arrNbTotalTests[0] + " tests failed";
$(this.projectSelector + " [name='testQuality']").text(desc);

} else {
$(this.projectSelector + " [name='testQuality']").text(jenkinsHealthDescription);
if(info.healthReport != null) {
for (var i = 0; i < info.healthReport.length; i++) {
var desc = info.healthReport[i].description;

if (desc.startsWith("Test Result")) {
if (~desc.indexOf("out of a total of")) {
let arr = desc.split("out of a total of");
let arrNbFailed = arr[0].split("tests");
let arrNbTotal = arr[1].split("tests");

let testQuality = arrNbFailed[0] + "/" + arrNbTotal[0] + " tests failed";
$(this.projectSelector + " [name='testQuality']").text(testQuality);
} else {
$(this.projectSelector + " [name='testQuality']").text(desc);
}

} else if (desc.startsWith("Build stability")) {
if (~desc.indexOf("out of the last")) {
$(this.projectSelector + " [name='detailBuild']").text(desc.replace("out of the last", "/"));
} else {
$(this.projectSelector + " [name='detailBuild']").text(desc);
}
}
}
}
$(this.projectSelector + " [name='linkLastBuild']").attr("href", info.lastBuild.url);
}
}

}
Expand Down
15 changes: 9 additions & 6 deletions front/assets/js/core/plugin/SonarInfo.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SonarInfo extends GetAndFillInfo {

getResult(msg) {
let sonarResult = new Object();

if(msg[0].cells.length==0) {
sonarResult.numberLines = "No Data";
sonarResult.numberLinesTrend = "No Data";
Expand All @@ -37,18 +38,19 @@ class SonarInfo extends GetAndFillInfo {
sonarResult.blockerViolation = "No Data";
sonarResult.criticalViolation = "No Data";
let this_=this;

// call last figures
let url = sonarUrl + "/api/resources?resource="+this.sonarName+"&metrics="+this.metrics+"&format=json&fromDateTime="+this.dateDebut+"&toDateTime=" + this.dateFin;
$.ajax({
$.ajax({
type: 'GET',
dataType: 'jsonp',
contentType: "text/plain",
data: {},
url: url+"&callback=?",
url: serverUrl + "/sonarResources?resource="+this.sonarName+"&metrics="+this.metrics+"&format=json&callback=?",
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR)
console.log(textStatus + " : " + errorThrown);
console.log(jqXHR);
},
success: function (msg) {

msg[0].msr.forEach(function(msr) {
switch(msr.key) {
case "blocker_violations" :
Expand All @@ -72,7 +74,8 @@ class SonarInfo extends GetAndFillInfo {
sonarResult.lastSonarCalculation = new Date(msg[0].date);
this_.fillInfo(sonarResult);
}
});
});

} else {
let firstValue = msg[0].cells[0];
let lastValue = msg[0].cells[msg[0].cells.length-1];
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "server",
"version": "1.3.0",
"version": "1.3.1",
"main": "server.js",
"dependencies": {
"async": "^2.6.0",
Expand Down
111 changes: 46 additions & 65 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ var server = app.listen(8085, function () {
var port = server.address().port
console.log("App listening at http://%s:%s", host, port)

})
});

//error handling to prevent server is kill by an error
process.on('uncaughtException', function(err) {
// handle the error safely
console.log(err);
})

});

app.get("/version", function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
Expand All @@ -41,15 +40,16 @@ app.get("/version", function (req, res) {
res.send(result);
});


var credential = '';
if(applicationConf.jenkins.user !== undefined) {
credential = applicationConf.jenkins.user + ':' + applicationConf.jenkins.userToken + '@';
}

app.get("/jenkinsinfo", function (req, res) {
let projectName = req.query['project_name'];
let callback = req.query['callback'];

let credential = '';
if(applicationConf.jenkins.user !== undefined) {
credential = applicationConf.jenkins.user + ':' + applicationConf.jenkins.userToken + '@';
}


var jenkins = jenkinsapi.init('http://' + credential + applicationConf.jenkins.host+':'+applicationConf.jenkins.port+'');

jenkins.job_info(projectName, {token: 'jenkins-token'}, function(err, data) {
Expand All @@ -63,6 +63,40 @@ app.get("/jenkinsinfo", function (req, res) {
});
});

app.get("/jenkinsDeployInfo", function (req, res) {
let endDate = new Date(req.query['endDate']);
let jobName = req.query['jobName'];
let callback = req.query['callback'];

let path = '/job/' + jobName;

var jenkins = jenkinsapi.init('http://' + credential + applicationConf.jenkins.host+':'+applicationConf.jenkins.port+'');

jenkins.job_info(jobName, {tree : 'builds[timestamp,id,result]', token: 'jenkins-token'}, function(err, data) {
var jsonData = JSON.parse(data);
var jenkinsBuilds = jsonData.builds;
var cpt = 0;

for (var i = 0, len = jenkinsBuilds.length; i < len; ++i) {
var jenkinsBuild = jenkinsBuilds[i];

if (jenkinsBuild.timestamp > endDate.getTime()
&& jenkinsBuild.result == "SUCCESS") {
cpt++;
}
}

var result = new Object();
result.numberOfDeploy = cpt;
result = JSON.stringify(result);

if(callback != null) {
result = callback + "([" + result.toString() + "])";
}

res.end(result.toString());
});
});

app.get("/sonarTimeMachine", function (req, res) {
let sonarName = req.query['resource'];
Expand Down Expand Up @@ -111,59 +145,6 @@ app.get("/sonarResources", function (req, res) {
}).end();
});



app.get("/jenkinsDeployInfo", function (req, res) {
let endDate = new Date(req.query['endDate']);
let jobName = req.query['jobName'];
let callback = req.query['callback'];

let path = '/job/' + jobName;

let options = {
host: applicationConf.jenkins.host,
port: applicationConf.jenkins.port,
path: path + "/api/json?tree=builds[number,status,timestamp,id,result]",
method: 'GET'
};

let url = "http://"+ options.host + ":" + options.port + "/" + options.path;

http.request(options, function(resRequest) {
resRequest.setEncoding('utf8');
var data = '';

resRequest.on('data', function (chunk){
data += chunk;
});

resRequest.on('end', function (body) {
var jsonData = JSON.parse(data);
var jenkinsBuilds = jsonData.builds;
var cpt = 0;

for (var i = 0, len = jenkinsBuilds.length; i < len; ++i) {
var jenkinsBuild = jenkinsBuilds[i];

if (jenkinsBuild.timestamp > endDate.getTime()
&& jenkinsBuild.result == "SUCCESS") {
cpt++;
}
}

var result = new Object();
result.numberOfDeploy = cpt;
result = JSON.stringify(result);

if (callback != null) {
result = callback + "([" + result + "])";
}
res.send(result);
});
}).end();

})

app.get("/cerberusinfo", function (req, res) {
let projectName = req.query['project_name'];
let tag = req.query['tag'];
Expand All @@ -189,7 +170,7 @@ app.get("/cerberusinfo", function (req, res) {
res.end(result);
});
}).end();
})
});

app.get("/getLastTagCerberus", function (req, res) {
let prefixTag = req.query['prefixTag'];
Expand Down Expand Up @@ -221,7 +202,7 @@ app.get("/getLastTagCerberus", function (req, res) {
res.end(result);
});
}).end();
})
});

app.get("/codeReviewStats", function (req, res) {
let team = req.query['teamName'];
Expand Down Expand Up @@ -269,4 +250,4 @@ app.get("/codeReviewStats", function (req, res) {
});

}).end();
})
});

0 comments on commit efb94b5

Please sign in to comment.