diff --git a/lib/plugin.js b/lib/plugin.js new file mode 100644 index 00000000..b4fa0806 --- /dev/null +++ b/lib/plugin.js @@ -0,0 +1,7 @@ +module.exports = class Plugin { + get api() { + const properties = Object.getOwnPropertyNames(this.constructor.prototype); + properties.splice(properties.indexOf('constructor'), 1); + return properties; + } +}; diff --git a/lib/plugins/issues.js b/lib/plugins/issues.js index fa209c5a..1daa25e1 100644 --- a/lib/plugins/issues.js +++ b/lib/plugins/issues.js @@ -1,6 +1,7 @@ const handlebars = require('handlebars'); +const Plugin = require('../plugin'); -module.exports = class IssuesPlugin { +module.exports = class Issues extends Plugin { // checkIfEventApplies(event) { // return event.issue !== undefined || event.pull_request !== undefined; // } diff --git a/lib/workflow.js b/lib/workflow.js index 876f6bf6..ce842819 100644 --- a/lib/workflow.js +++ b/lib/workflow.js @@ -11,14 +11,10 @@ module.exports = class Workflow { this.filterFn = () => true; this.api = {}; + // Define a new function in the API for each plugin method for (const plugin of plugins) { - // Get all the property names of the plugin - for (const method of Object.getOwnPropertyNames(plugin.constructor.prototype)) { - if (method !== 'constructor') { - // Define a new function in the API for this plugin method, forcing - // the binding to this to prevent any tampering. - this.api[method] = this.proxy(plugin[method]).bind(this); - } + for (const method of plugin.api) { + this.api[method] = this.proxy(plugin[method]).bind(this); } } diff --git a/test/plugin.js b/test/plugin.js new file mode 100644 index 00000000..d1f6109f --- /dev/null +++ b/test/plugin.js @@ -0,0 +1,22 @@ +const expect = require('expect'); +const Plugin = require('../lib/plugin'); + +class TestPlugin extends Plugin { + foo() { + + } +} + +describe('Plugin', () => { + const plugin = new TestPlugin(); + + describe('api', () => { + it('includes methods', () => { + expect(plugin.api.indexOf('foo') >= 0).toBe(true); + }); + + it('exludes constructor', () => { + expect(plugin.api.indexOf('constructor')).toBe(-1); + }); + }); +});