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 handlebars = require('handlebars');
|
||||||
|
const Plugin = require('../plugin');
|
||||||
|
|
||||||
module.exports = class IssuesPlugin {
|
module.exports = class Issues extends Plugin {
|
||||||
// checkIfEventApplies(event) {
|
// checkIfEventApplies(event) {
|
||||||
// return event.issue !== undefined || event.pull_request !== undefined;
|
// return event.issue !== undefined || event.pull_request !== undefined;
|
||||||
// }
|
// }
|
||||||
|
|
|
@ -11,14 +11,10 @@ module.exports = class Workflow {
|
||||||
this.filterFn = () => true;
|
this.filterFn = () => true;
|
||||||
this.api = {};
|
this.api = {};
|
||||||
|
|
||||||
|
// Define a new function in the API for each plugin method
|
||||||
for (const plugin of plugins) {
|
for (const plugin of plugins) {
|
||||||
// Get all the property names of the plugin
|
for (const method of plugin.api) {
|
||||||
for (const method of Object.getOwnPropertyNames(plugin.constructor.prototype)) {
|
this.api[method] = this.proxy(plugin[method]).bind(this);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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