2015-04-10 19:23:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 19:53:19 +00:00
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
2015-10-03 14:14:41 +00:00
|
|
|
db = require('../db'),
|
2015-11-24 22:41:20 +00:00
|
|
|
utils = require('../lib/utils'),
|
|
|
|
logger = require('../lib/logger')('builds resource');
|
2015-05-09 19:53:19 +00:00
|
|
|
|
2015-05-01 11:11:29 +00:00
|
|
|
module.exports = function(app) {
|
2015-11-24 22:41:20 +00:00
|
|
|
var resource = app.dataio.resource('builds'),
|
|
|
|
distributor = app.distributor;
|
2015-04-10 19:23:52 +00:00
|
|
|
|
2015-07-05 17:18:51 +00:00
|
|
|
resource.use('readAll', function(req, res, next) {
|
2015-05-09 19:53:19 +00:00
|
|
|
Steppy(
|
|
|
|
function() {
|
2015-07-09 20:12:24 +00:00
|
|
|
var data = req.data || {};
|
|
|
|
|
|
|
|
var start = {};
|
|
|
|
if (data.projectName) {
|
|
|
|
start.projectName = data.projectName;
|
|
|
|
}
|
2015-07-11 18:09:28 +00:00
|
|
|
|
|
|
|
start.descCreateDate = data.descCreateDate || '';
|
2015-07-09 20:12:24 +00:00
|
|
|
|
|
|
|
var findParams = _(data).pick('offset', 'limit');
|
|
|
|
findParams.start = start;
|
2015-05-09 19:53:19 +00:00
|
|
|
findParams.limit = findParams.limit || 20;
|
|
|
|
|
|
|
|
db.builds.find(findParams, this.slot());
|
|
|
|
},
|
|
|
|
function(err, builds) {
|
2015-07-22 20:18:47 +00:00
|
|
|
// omit big fields not needed for list
|
|
|
|
_(builds).each(function(build) {
|
|
|
|
delete build.stepTimings;
|
2015-10-12 17:08:24 +00:00
|
|
|
if (build.scm) {
|
|
|
|
delete build.scm.changes;
|
|
|
|
}
|
2015-07-26 13:05:54 +00:00
|
|
|
build.project = _(build.project).pick(
|
|
|
|
'name', 'scm', 'avgBuildDuration'
|
|
|
|
);
|
2015-07-22 20:18:47 +00:00
|
|
|
});
|
|
|
|
|
2015-05-09 19:53:19 +00:00
|
|
|
res.send(builds);
|
|
|
|
},
|
2015-07-05 17:18:51 +00:00
|
|
|
next
|
2015-05-09 19:53:19 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-05 17:18:51 +00:00
|
|
|
resource.use('read', function(req, res, next) {
|
2015-05-17 13:48:16 +00:00
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
var findParams = {};
|
|
|
|
findParams.start = _(req.data).pick('id');
|
|
|
|
db.builds.find(findParams, this.slot());
|
|
|
|
},
|
|
|
|
function(err, build) {
|
|
|
|
res.send(build[0]);
|
|
|
|
},
|
2015-07-05 17:18:51 +00:00
|
|
|
next
|
2015-05-17 13:48:16 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-10-03 14:14:41 +00:00
|
|
|
resource.use('getBuildLogTail', function(req, res, next) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
var findParams = {
|
|
|
|
reverse: true,
|
2015-11-30 20:08:54 +00:00
|
|
|
start: {buildId: req.data.buildId},
|
2015-10-03 14:14:41 +00:00
|
|
|
limit: req.data.length
|
|
|
|
};
|
|
|
|
|
|
|
|
db.logLines.find(findParams, this.slot());
|
|
|
|
},
|
|
|
|
function(err, logLines) {
|
2015-10-15 05:20:08 +00:00
|
|
|
var lines = logLines.reverse(),
|
2015-10-03 14:14:41 +00:00
|
|
|
total = logLines.length ? logLines[0].number : 0;
|
|
|
|
|
|
|
|
res.send({lines: lines, total: total});
|
|
|
|
},
|
|
|
|
next
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
resource.use('getBuildLogLines', function(req, res, next) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
var buildId = req.data.buildId,
|
|
|
|
from = req.data.from,
|
2015-10-15 05:20:08 +00:00
|
|
|
to = req.data.to,
|
|
|
|
count = to - from;
|
2015-10-03 14:14:41 +00:00
|
|
|
|
|
|
|
db.logLines.find({
|
2015-11-30 20:08:54 +00:00
|
|
|
start: {buildId: buildId, number: from},
|
|
|
|
end: {buildId: buildId, number: to}
|
2015-10-03 14:14:41 +00:00
|
|
|
}, this.slot());
|
2015-10-15 05:20:08 +00:00
|
|
|
|
|
|
|
this.pass(count);
|
2015-10-03 14:14:41 +00:00
|
|
|
},
|
2015-10-15 05:20:08 +00:00
|
|
|
function(err, logLines, count) {
|
2015-10-03 14:14:41 +00:00
|
|
|
res.send({
|
2015-10-15 05:20:08 +00:00
|
|
|
lines: logLines,
|
|
|
|
isLast: logLines.length < count
|
2015-10-03 14:14:41 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
next
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-11-24 22:41:20 +00:00
|
|
|
resource.use('cancel', function(req, res, next) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
var buildId = req.data.buildId;
|
|
|
|
logger.log('Cancel build: "%s"', buildId);
|
|
|
|
distributor.cancel({buildId: buildId}, this.slot());
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
res.send();
|
|
|
|
},
|
|
|
|
next
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-05-03 15:23:01 +00:00
|
|
|
return resource;
|
2015-04-10 19:23:52 +00:00
|
|
|
};
|