forked from mirrors/probot
Add plugin base class
This commit is contained in:
parent
838f4801d9
commit
cc18091356
|
@ -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;
|
||||
}
|
||||
};
|
|
@ -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;
|
||||
// }
|
||||
|
|
|
@ -11,16 +11,12 @@ 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.
|
||||
for (const method of plugin.api) {
|
||||
this.api[method] = this.proxy(plugin[method]).bind(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.api.filter = this.filter.bind(this);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue