From e00d3592494e73f1fb3c2604e8836a9a778c69a0 Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 22 Nov 2013 14:03:35 +0400 Subject: [PATCH] Major improvements --- BuildServer/app.js | 2 +- BuildServer/lib/builder.js | 25 +++++-- BuildServer/routes/status.js | 107 ++++++++++++++++++++++++++-- BuildServer/views/status-image.jade | 15 ++-- 4 files changed, 132 insertions(+), 17 deletions(-) diff --git a/BuildServer/app.js b/BuildServer/app.js index e319430..7713fff 100644 --- a/BuildServer/app.js +++ b/BuildServer/app.js @@ -40,7 +40,7 @@ app.get('/', routes.index); app.post('/github/postreceive', routes.postreceive); app.get('/manual', routes.manual.get); app.post('/manual', routes.manual.post); -app.get('/status', routes.status.image); +app.get('/status/:owner/:reponame/:branch/:rev', routes.status.image); app.get('/status.svg', routes.status.image); http.createServer(app).listen(app.get('port'), function(){ diff --git a/BuildServer/lib/builder.js b/BuildServer/lib/builder.js index 7fdc555..6ffb88e 100644 --- a/BuildServer/lib/builder.js +++ b/BuildServer/lib/builder.js @@ -18,6 +18,19 @@ var build = function (options, callback) { fse.mkdirsSync(release); + fs.writeFileSync(options.app.get('releasepath') + "/" + owner + "/" + reponame + "/" + branch + "/latest.id", rev); + fse.mkdirsSync(options.app.get('releasepath') + "/" + owner + "/" + reponame + "/$revs"); + fs.writeFileSync(options.app.get('releasepath') + "/" + owner + "/" + reponame + "/$revs/" + rev + ".branch", branch); + + var done = function (err, result) { + fs.writeFile(release + "/report.json", JSON.stringify({err: err, result: result}), function (writeErr) { + if (writeErr) { + return callback(writeErr); + } + return callback(err, result); + }); + }; + gitLoader({ remote: url + ".git", local: local, @@ -31,11 +44,11 @@ var build = function (options, callback) { console.log("Done loading from git"); fs.exists(exported + "/mbs.json", function (exists) { if (!exists) { - return callback(null, "MBSNotFound"); + return done(null, "MBSNotFound"); } fs.readFile(exported + "/mbs.json", function (err, data) { if (err) { - return callback(err, "MBSUnableToRead"); + return done(err, "MBSUnableToRead"); } var task; @@ -43,17 +56,15 @@ var build = function (options, callback) { task = JSON.parse(data); } catch(err) { console.log("Malformed data: " + data); - return callback(err, "MBSMalformed"); + return done(err, "MBSMalformed"); } processor.processTask(task, function (err, result) { if (err) { - return callback(err, result); + return done(err, result); } - fs.writeFile(release + "/report.json", JSON.stringify(result), function (err) { - return callback(err, result); - }); + return done(err, result); }); }); }); diff --git a/BuildServer/routes/status.js b/BuildServer/routes/status.js index 11fd9dd..dc0aaf9 100644 --- a/BuildServer/routes/status.js +++ b/BuildServer/routes/status.js @@ -1,5 +1,104 @@ -exports.image = function(req, res){ - console.log(req.headers); - res.setHeader('Content-Type', 'image/svg+xml'); - res.render('status-image', { title: 'Express' + req + "qq", status: "Error" }); +"use strict"; + +var fs = require('fs'), + url = require('url'); + +var parseOptionsFromReferer = function (req, callback) { + var pathParts = (url.parse(req.headers.referer || "").pathname || "").split("/"); + var result = {}; + if (pathParts.length < 3) { + return callback("BadRequest", result); + } + + result.owner = pathParts[1]; + result.reponame = pathParts[2]; + if (pathParts[3] && /^[\da-f]{40}$/i.test(pathParts[4])) { + result.rev = pathParts[4]; + var branchFile = req.app.get('releasepath') + "/" + result.owner + "/" + result.reponame + "/$revs/" + result.rev + ".branch"; + fs.exists(branchFile, function (exists) { + if (!exists) { + return callback("BranchFileNotFound", result); + } + fs.readFile(branchFile, function (err, data) { + if (err) { + return callback(err, result); + } + result.branch = data.toString(); + result.branchName = result.branch.split("/").pop(); + return callback(null, result); + }); + }); + } else { + result.branchName = pathParts[4] || "master"; + result.branch = "refs/heads/" + result.branchName; +// console.log(result); + var revFile = req.app.get('releasepath') + "/" + result.owner + "/" + result.reponame + "/" + result.branch + "/latest.id"; + fs.exists(revFile, function (exists) { + if (!exists) { + return callback("RevFileNotFound", result); + } + fs.readFile(revFile, function (err, data) { + if (err) { + return callback(err, result); + } + result.rev = data.toString(); + return callback(null, result); + }); + }); + } +}; + +var loadReport = function (app, options, callback) { + var reportFile = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/" + options.rev + "/report.json"; + fs.exists(reportFile, function (exists) { + if (!exists) { + return callback("ReportFileNotFound", options); + } + + fs.readFile(reportFile, function (err, data) { + if (err) { + return callback(err, options); + } + options.report = JSON.parse(data); + return callback(null, options); + }); + }); +}; + +exports.image = function(req, res) { +// console.log(req.headers); + var handle = function (err, options) { + if (err === "ReportFileNotFound") { + options.status = "Building"; + } else if (err) { + options.status = "StatusError"; + options.message = err; + } else if (options.report.result === "MBSNotFound") { + options.status = "MBSNotUsed"; + } else if (options.report.err) { + options.status = "Error"; + options.message = options.report.err; + } else if ((options.report.result.warns.$allMessages || []).length > 0) { + options.status = "Warning"; + options.message = options.report.result.warns.$allMessages[0]; + } else { + options.status = "OK"; + } + console.log(options); + res.setHeader('Content-Type', 'image/svg+xml'); + res.render('status-image', options); + }; + + parseOptionsFromReferer(req, function (err, options) { + if (err) { + return handle(err, options); + } + + loadReport(req.app, options, function (err, options) { + handle(err, options); + }); + }); +}; + +exports.page = function(req, res) { }; diff --git a/BuildServer/views/status-image.jade b/BuildServer/views/status-image.jade index a1f250a..e65f30f 100644 --- a/BuildServer/views/status-image.jade +++ b/BuildServer/views/status-image.jade @@ -1,8 +1,13 @@ doctype xml --var fills = {Error: "red", Warn: "yellow", OK: "green" } --var colors = {Error: "white", Warn: "black", OK: "black" } +-var fills = {Warn: "yellow", OK: "green" } +-var colors = {Warn: "black", OK: "black" } +-var fill = fills[status] || "red" +-var color = colors[status] || "white" -svg(xmlns="http://www.w3.org/2000/svg", version="1.1", width="800px", height="100px") - rect(width="100%", height="100%", fill=fills[status]) - text(x=20, y=50, font-size=36, font-weight="bold", fill=colors[status]) Build status: #{status} +svg(xmlns="http://www.w3.org/2000/svg", xmlns:xlink="http://www.w3.org/1999/xlink", version="1.1", width="800px", height=(message?"100px":"80px")) + a(xlink:href="/status/" + [owner, reponame, branchName, rev].join("/")) + rect(width="100%", height="100%", fill=fill) + text(x=20, y=50, font-size=36, font-weight="bold", fill=color) Build status: #{status} + if (message) + text(x=20, y=80, font-size=18, fill=color) #{message}