Status page refactored

dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 9 years ago
parent 9cd602d8fc
commit 5cc1338744
  1. 101
      BuildServer/lib/status-processor.js
  2. 100
      BuildServer/routes/status.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);
});
}

@ -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);
});
};

Loading…
Cancel
Save