2015-05-09 19:53:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-15 22:25:28 +00:00
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
|
|
|
nlevel = require('nlevel'),
|
2015-05-09 19:53:19 +00:00
|
|
|
ldb = nlevel.db('path/to/db/ignored/for/memdown', {
|
|
|
|
db: require('memdown'),
|
|
|
|
valueEncoding: 'json'
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.builds = new nlevel.DocsSection(ldb, 'builds', {
|
|
|
|
projections: [
|
|
|
|
{key: {createDate: 1}, value: pickId},
|
|
|
|
{key: {descCreateDate: descCreateDate, id: 1}},
|
2015-05-17 10:26:28 +00:00
|
|
|
{key: {
|
|
|
|
projectName: function(build) {
|
|
|
|
return build.project.name;
|
|
|
|
},
|
|
|
|
descCreateDate: descCreateDate,
|
|
|
|
id: 1
|
|
|
|
}}
|
2015-05-09 19:53:19 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2015-05-17 10:26:28 +00:00
|
|
|
exports.builds._beforePut = function(builds, callback) {
|
|
|
|
var self = this,
|
|
|
|
build;
|
|
|
|
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
if (builds.length > 1) {
|
|
|
|
throw new Error('Build put hooks work only with single build');
|
|
|
|
}
|
|
|
|
build = builds[0];
|
|
|
|
|
|
|
|
// generate number for build
|
|
|
|
if (!build.number && build.status === 'in-progress') {
|
|
|
|
// find last build with number in the same project
|
|
|
|
self.find({
|
|
|
|
start: {projectName: build.project.name, descCreateDate: ''},
|
|
|
|
filter: function(build) {
|
|
|
|
return 'number' in build;
|
|
|
|
},
|
|
|
|
limit: 1
|
|
|
|
}, this.slot());
|
|
|
|
} else {
|
|
|
|
this.pass([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
generateIds(self, builds, this.slot());
|
|
|
|
},
|
|
|
|
function(err, prevBuilds) {
|
|
|
|
var prevBuild = prevBuilds[0];
|
|
|
|
if (!build.number && build.status === 'in-progress') {
|
|
|
|
build.number = prevBuild ? prevBuild.number + 1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pass(null);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2015-05-15 22:25:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function generateIds(section, docs, callback) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
2015-05-17 10:26:28 +00:00
|
|
|
var isAllDocsWithId = _(docs).all(function(doc) {
|
|
|
|
return 'id' in doc;
|
|
|
|
});
|
|
|
|
if (isAllDocsWithId) {
|
2015-05-15 22:25:28 +00:00
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:26:28 +00:00
|
|
|
var isAllDocsWithoutId = _(docs).all(function(doc) {
|
|
|
|
return 'id' in doc === false;
|
|
|
|
});
|
|
|
|
if (!isAllDocsWithoutId) {
|
|
|
|
throw new Error(
|
|
|
|
'Documents with id and without should not be mixed'
|
|
|
|
);
|
|
|
|
}
|
2015-05-09 19:53:19 +00:00
|
|
|
|
2015-05-15 22:25:28 +00:00
|
|
|
section.find({
|
|
|
|
start: {createDate: ''}, limit: 1, reverse: true
|
|
|
|
}, this.slot());
|
|
|
|
},
|
|
|
|
function(err, lastDocs) {
|
|
|
|
var id = lastDocs[0] && ++lastDocs[0].id || 1;
|
|
|
|
|
|
|
|
_(docs).each(function(doc) {
|
|
|
|
doc.id = id;
|
|
|
|
id++;
|
2015-05-09 19:53:19 +00:00
|
|
|
});
|
|
|
|
|
2015-05-15 22:25:28 +00:00
|
|
|
this.pass(null);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-09 19:53:19 +00:00
|
|
|
function pickId(doc) {
|
|
|
|
return {id: doc.id};
|
|
|
|
}
|
|
|
|
|
|
|
|
// reversed date - for sorting forward (it's fatster for leveldb then
|
|
|
|
// reverse: true, see levelup reverse notes for details) but have documents
|
|
|
|
// sorted by some date in descending order
|
|
|
|
var maxTime = new Date('03:14:07 UTC 2138-01-19').getTime();
|
|
|
|
|
|
|
|
function descCreateDate(doc) {
|
|
|
|
return maxTime - doc.createDate;
|
|
|
|
}
|