From 5cc1338744a60c60bf2437c1a607e3ba410f7091 Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 6 Nov 2015 14:15:49 +0300 Subject: [PATCH] Status page refactored --- BuildServer/lib/status-processor.js | 101 ++++++++++++++++++++++++++++ BuildServer/routes/status.js | 100 +++------------------------ 2 files changed, 112 insertions(+), 89 deletions(-) create mode 100644 BuildServer/lib/status-processor.js diff --git a/BuildServer/lib/status-processor.js b/BuildServer/lib/status-processor.js new file mode 100644 index 0000000..3b6354a --- /dev/null +++ b/BuildServer/lib/status-processor.js @@ -0,0 +1,101 @@ +"use strict"; + +var fs = require('fs'), + url = require('url'), + glob = require('glob'); + +var addBranchInfo = function (app, options, callback) { + var branchFile = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/$revs/" + options.rev + ".branch"; + fs.exists(branchFile, function (exists) { + if (!exists) { + return callback("BranchFileNotFound", options); + } + fs.readFile(branchFile, function (err, data) { + if (err) { + return callback(err, result); + } + options.branch = data.toString(); + options.branchName = options.branch.split("/").pop(); + return callback(null, options); + }); + }); +}; + +var addRevInfo = function (app, options, callback) { + var revFile = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/latest.id"; + fs.exists(revFile, function (exists) { + if (!exists) { + return callback("RevFileNotFound", options); + } + fs.readFile(revFile, function (err, data) { + if (err) { + return callback(err, options); + } + options.rev = data.toString(); + return callback(null, options); + }); + }); +}; + +var parseOptions = function (app, options, callback) { + var result = {}; + + result.owner = options.owner; + result.reponame = options.reponame; + + if (options.rev && !/^[\da-f]{40}$/i.test(options.rev)) { + return callback("Wrong rev format: " + options.rev, options); + } + + if (options.rev) { + result.rev = options.rev; + return addBranchInfo(app, result, callback); + } else if (/^[\da-f]{40}$/i.test(options.branchName)) { + result.rev = options.branchName; + return addBranchInfo(app, result, callback); + } else { + result.branchName = options.branchName || "master"; + result.branch = "refs/heads/" + result.branchName; + return addRevInfo(app, result, callback); + } +}; + +var loadReport = function (app, options, callback) { + var releaseDir = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/" + options.rev; + + glob("**", {cwd: releaseDir, mark: true}, function (err, files) { + if (err) { + return callback(err, options); + } + + var reportFile = releaseDir + "/report.json"; + options.files = files; + fs.exists(reportFile, function (exists) { + if (!exists) { + return callback("ReportFileNotFound", options); + } + + fs.readFile(reportFile, function (err, dataBuffer) { + if (err) { + return callback(err, options); + } + var data = dataBuffer.toString(); + if (!data) { + return callback("ReportFileNotFound", options); + } + options.report = JSON.parse(data); + return callback(null, options); + }); + }); + }); +}; + +exports.getReport = function (app, options, callback) { + parseOptions(app, options, function (err, result) { + if (err) { + return callback(err); + } + + return loadReport(app, result, callback); + }); +} diff --git a/BuildServer/routes/status.js b/BuildServer/routes/status.js index 616a236..4c731a6 100644 --- a/BuildServer/routes/status.js +++ b/BuildServer/routes/status.js @@ -2,40 +2,8 @@ var fs = require('fs'), url = require('url'), - glob = require('glob'); - -var addBranchInfo = function (app, options, callback) { - var branchFile = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/$revs/" + options.rev + ".branch"; - fs.exists(branchFile, function (exists) { - if (!exists) { - return callback("BranchFileNotFound", options); - } - fs.readFile(branchFile, function (err, data) { - if (err) { - return callback(err, result); - } - options.branch = data.toString(); - options.branchName = options.branch.split("/").pop(); - return callback(null, options); - }); - }); -}; - -var addRevInfo = function (app, options, callback) { - var revFile = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/latest.id"; - fs.exists(revFile, function (exists) { - if (!exists) { - return callback("RevFileNotFound", options); - } - fs.readFile(revFile, function (err, data) { - if (err) { - return callback(err, options); - } - options.rev = data.toString(); - return callback(null, options); - }); - }); -}; + glob = require('glob'), + statusProcessor = require('../lib/status-processor'); var parseOptionsFromReferer = function (req, callback) { var pathParts = (url.parse(req.headers.referer || "").pathname || "").split("/"); @@ -46,44 +14,9 @@ var parseOptionsFromReferer = function (req, callback) { result.owner = pathParts[1]; result.reponame = pathParts[2]; - if (pathParts[3] && /^[\da-f]{40}$/i.test(pathParts[4])) { - result.rev = pathParts[4]; - return addBranchInfo(req.app, result, callback); - } else { - result.branchName = pathParts[4] || "master"; - result.branch = "refs/heads/" + result.branchName; - return addRevInfo(req.app, result, callback); - } -}; - -var loadReport = function (app, options, callback) { - var releaseDir = app.get('releasepath') + "/" + options.owner + "/" + options.reponame + "/" + options.branch + "/" + options.rev; - - glob("**", {cwd: releaseDir, mark: true}, function (err, files) { - if (err) { - return callback(err, options); - } - - var reportFile = releaseDir + "/report.json"; - options.files = files; - fs.exists(reportFile, function (exists) { - if (!exists) { - return callback("ReportFileNotFound", options); - } - - fs.readFile(reportFile, function (err, dataBuffer) { - if (err) { - return callback(err, options); - } - var data = dataBuffer.toString(); - if (!data) { - return callback("ReportFileNotFound", options); - } - options.report = JSON.parse(data); - return callback(null, options); - }); - }); - }); + result.branchName = pathParts[3]; + result.rev = pathParts[4]; + return callback(null, result); }; exports.image = function(req, res) { @@ -117,14 +50,13 @@ exports.image = function(req, res) { return handle(err, options); } - loadReport(req.app, options, function (err, options) { + statusProcessor.getReport(req.app, options, function (err, options) { handle(err, options); }); }); }; exports.page = function(req, res) { -// console.log(req); var options = { owner: req.params.owner, reponame: req.params.reponame, @@ -132,20 +64,10 @@ exports.page = function(req, res) { branch: "/refs/heads/" + req.params.branch, rev: req.params.rev }; - var finish = function (err, options) { - loadReport(req.app, options, function (err, options) { - options.err = err; - console.log(options); - res.render('status', options); - }); - }; - if (/^[\da-f]{40}$/i.test(options.branchName)) { - options.rev = options.branchName; - options.branchName = undefined; - options.branch = undefined; - return addBranchInfo(req.app, options, finish); - } else { - return finish(undefined, options); - } + statusProcessor.getReport(req.app, options, function (err, options) { + options = options || {}; + options.err = err; + res.render('status', options); + }); };