nci/lib/notifier/index.js

107 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-05-21 21:09:16 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
2015-06-12 13:26:34 +00:00
utils = require('../utils'),
db = require('../../db');
2015-05-21 21:09:16 +00:00
var constructors = {},
instances = {};
exports.register = function(type, constructor) {
constructors[type] = constructor;
};
exports.init = function(params, callback) {
Steppy(
function() {
var initGroup = this.makeGroup();
_(constructors).each(function(Constructor, type) {
instances[type] = new Constructor();
instances[type].init(params[type], initGroup.slot());
});
},
callback
);
};
2015-06-12 13:26:34 +00:00
// Returns previous (by number) build from the same project
exports._getPrevBuild = function(build, callback) {
Steppy(
function() {
db.builds.find({
start: {
projectName: build.project.name,
number: build.number - 1
},
limit: 1
}, this.slot());
},
function(err, builds) {
this.pass(builds[0]);
},
callback
);
};
2015-05-21 21:09:16 +00:00
/*
2015-05-25 19:36:09 +00:00
* Check if that's completed build should be notified, then notify
2015-05-21 21:09:16 +00:00
*/
exports.send = function(build, callback) {
callback = callback || utils.logErrorCallback;
Steppy(
function() {
if (!build.completed) {
throw new Error('Build should be completed before notify');
}
var notify = build.project.notify;
// TODO: move to project validation during load
if (!notify || !notify.on || !notify.to) {
return callback();
}
2015-06-12 13:26:34 +00:00
this.pass(notify);
// get previous build (for some strategies)
if (
build.number > 1 &&
_(notify.on).intersection(['change']).length
) {
exports._getPrevBuild(build, this.slot());
}
},
function(err, notify, prevBuild) {
2015-05-21 21:09:16 +00:00
var strategy = _(notify.on).find(function(strategy) {
if (strategy === 'done') {
2015-05-21 21:09:16 +00:00
return build.status === 'done';
} else if (strategy === 'error') {
2015-05-21 21:09:16 +00:00
return build.status === 'error';
2015-06-12 13:26:34 +00:00
} else if (strategy === 'change') {
// notify on status change or about first build
return prevBuild ? build.status !== prevBuild.status: true;
2015-05-21 21:09:16 +00:00
}
});
// Nothing to notify about
if (!strategy) {
return callback();
}
var notifyGroup = this.makeGroup();
_(notify.to).each(function(recipients, type) {
if (type in instances) {
instances[type].send({
build: build,
notifyReason: {strategy: strategy}
}, notifyGroup.slot());
} else {
throw new Error('Unknown notifier: ' + type);
}
});
},
callback
);
};