notify on change

This commit is contained in:
oleg 2015-06-12 16:26:34 +03:00
parent 6d50b6ba06
commit f618ff6713
4 changed files with 134 additions and 7 deletions

View File

@ -8,8 +8,9 @@ scm:
notify: notify:
on: on:
- success # - success
- fail # - fail
- change
to: to:
console: console:
# email: # email:

14
db.js
View File

@ -13,15 +13,23 @@ exports.builds = new nlevel.DocsSection(ldb, 'builds', {
{key: {createDate: 1}, value: pickId}, {key: {createDate: 1}, value: pickId},
{key: {descCreateDate: descCreateDate, id: 1}}, {key: {descCreateDate: descCreateDate, id: 1}},
{key: { {key: {
projectName: function(build) { projectName: pickProjectName,
return build.project.name;
},
descCreateDate: descCreateDate, descCreateDate: descCreateDate,
id: 1 id: 1
}},
// note that's unordered projection (coz number is numeric)
{key: {
projectName: pickProjectName,
number: 1,
id: 1
}} }}
] ]
}); });
function pickProjectName(build) {
return build.project.name;
}
exports.builds._beforePut = function(builds, callback) { exports.builds._beforePut = function(builds, callback) {
var self = this, var self = this,
build; build;

View File

@ -2,7 +2,8 @@
var Steppy = require('twostep').Steppy, var Steppy = require('twostep').Steppy,
_ = require('underscore'), _ = require('underscore'),
utils = require('../utils'); utils = require('../utils'),
db = require('../../db');
var constructors = {}, var constructors = {},
instances = {}; instances = {};
@ -24,6 +25,25 @@ exports.init = function(params, callback) {
); );
}; };
// 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
);
};
/* /*
* Check if that's completed build should be notified, then notify * Check if that's completed build should be notified, then notify
*/ */
@ -42,11 +62,25 @@ exports.send = function(build, callback) {
return callback(); return callback();
} }
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) {
var strategy = _(notify.on).find(function(strategy) { var strategy = _(notify.on).find(function(strategy) {
if (strategy === 'success') { if (strategy === 'success') {
return build.status === 'done'; return build.status === 'done';
} else if (strategy === 'fail') { } else if (strategy === 'fail') {
return build.status === 'error'; return build.status === 'error';
} else if (strategy === 'change') {
// notify on status change or about first build
return prevBuild ? build.status !== prevBuild.status: true;
} }
}); });

View File

@ -2,7 +2,8 @@
var notifier = require('../lib/notifier'), var notifier = require('../lib/notifier'),
expect = require('expect.js'), expect = require('expect.js'),
sinon = require('sinon'); sinon = require('sinon'),
_ = require('underscore');
describe('notifier module', function() { describe('notifier module', function() {
@ -147,4 +148,87 @@ describe('notifier module', function() {
}); });
}); });
var secondBuild;
// for all previos build related strategies
describe('Stub getting of previos build', function() {
it('', function() {
sinon.stub(notifier, '_getPrevBuild').callsArgWith(1, null, build);
});
});
describe('notify on change', function() {
it('set build info', function() {
build = {
completed: true,
status: 'done',
number: 1,
project: {
name: 'project1',
notify: {
on: ['change'],
to: {test: ['recipient1', 'recipient2']}
}
}
};
sendSpy.reset();
});
it('should notify for the first build', function(done) {
notifier.send(build, function(err) {
expect(err).not.ok();
expect(sendSpy.calledOnce).equal(true);
done();
});
});
it('should be notified with right params', function() {
expect(sendSpy.calledWith({
notifyReason: {strategy: 'change'},
build: build
})).equal(true);
});
it('set second build info (same status)', function() {
secondBuild = _(build).clone();
secondBuild.number = 2;
sendSpy.reset();
});
it('should not notify when same second build status', function(done) {
notifier.send(secondBuild, function(err) {
expect(err).not.ok();
expect(sendSpy.calledOnce).equal(false);
done();
});
});
it('set second build info (changed status)', function() {
secondBuild.status = 'error';
sendSpy.reset();
});
it('should notify when status is changed', function(done) {
notifier.send(secondBuild, function(err) {
expect(err).not.ok();
expect(sendSpy.calledOnce).equal(true);
done();
});
});
it('should be notified with right params', function() {
expect(sendSpy.calledWith({
notifyReason: {strategy: 'change'},
build: secondBuild
})).equal(true);
});
});
describe('Restore getting of previos build', function() {
it('', function() {
notifier._getPrevBuild.restore();
});
});
}); });