Add Context.halt() function to make halting a rule simple

This commit is contained in:
Lee Dohm
2017-03-02 10:08:41 -08:00
parent 48b5b3f8ba
commit e4aac40b2b
3 changed files with 10 additions and 7 deletions

View File

@@ -5,6 +5,10 @@ module.exports = class Context {
this.payload = event.payload;
}
halt() {
return Promise.reject(new Error('halted'));
}
toRepo(object) {
const repo = this.payload.repository;

View File

@@ -1,13 +1,9 @@
const Plugin = require('../plugin');
function halt() {
return Promise.reject(new Error('halted'));
}
module.exports = class Filter extends Plugin {
filter(context, fn) {
const result = fn(context.event, context);
return result === false ? halt() : result;
return result === false ? context.halt() : result;
}
then(context, fn) {
@@ -21,6 +17,6 @@ module.exports = class Filter extends Plugin {
return name === event.event && (!action || action === event.payload.action);
});
return res ? Promise.resolve(res) : halt();
return res ? Promise.resolve(res) : context.halt();
}
};

View File

@@ -5,7 +5,10 @@ const createSpy = expect.createSpy;
describe('filter plugin', () => {
const event = {};
const context = {event};
const context = {
event,
halt: createSpy().andReturn(Promise.reject(new Error('halted')))
};
let filter;