From efb94b585bac0fa32d34b8b4a9e82372561bff25 Mon Sep 17 00:00:00 2001 From: Gotier Date: Tue, 20 Feb 2018 10:04:17 +0100 Subject: [PATCH] use jenkins-api for jenkinsDeployInfo fix MIME type problem with sonar fix healthreport reporting in JenkinsInfo.class --- .../js/core/plugin/JenkinsInfo.class.js | 43 ++++--- .../assets/js/core/plugin/SonarInfo.class.js | 15 ++- server/package.json | 2 +- server/server.js | 111 ++++++++---------- 4 files changed, 82 insertions(+), 89 deletions(-) diff --git a/front/assets/js/core/plugin/JenkinsInfo.class.js b/front/assets/js/core/plugin/JenkinsInfo.class.js index 14e56fc..d183807 100644 --- a/front/assets/js/core/plugin/JenkinsInfo.class.js +++ b/front/assets/js/core/plugin/JenkinsInfo.class.js @@ -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"); @@ -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); + } } } diff --git a/front/assets/js/core/plugin/SonarInfo.class.js b/front/assets/js/core/plugin/SonarInfo.class.js index f12602e..38ce9f6 100644 --- a/front/assets/js/core/plugin/SonarInfo.class.js +++ b/front/assets/js/core/plugin/SonarInfo.class.js @@ -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"; @@ -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" : @@ -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]; diff --git a/server/package.json b/server/package.json index e0311d6..de35282 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "server", - "version": "1.3.0", + "version": "1.3.1", "main": "server.js", "dependencies": { "async": "^2.6.0", diff --git a/server/server.js b/server/server.js index 6a14235..283522c 100644 --- a/server/server.js +++ b/server/server.js @@ -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', '*'); @@ -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) { @@ -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']; @@ -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']; @@ -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']; @@ -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']; @@ -269,4 +250,4 @@ app.get("/codeReviewStats", function (req, res) { }); }).end(); -}) +});