mirror of
https://github.com/zhigang1992/probot.git
synced 2026-06-17 04:09:31 +08:00
Add test and fix receive method
This commit is contained in:
20
index.js
20
index.js
@@ -33,7 +33,10 @@ module.exports = (options = {}) => {
|
||||
const server = createServer(webhook);
|
||||
|
||||
// Log all received webhooks
|
||||
webhook.on('*', event => logger.trace(event, 'webhook received'));
|
||||
webhook.on('*', event => {
|
||||
logger.trace(event, 'webhook received');
|
||||
receive(event);
|
||||
});
|
||||
|
||||
// Log all webhook errors
|
||||
webhook.on('error', logger.error.bind(logger));
|
||||
@@ -51,9 +54,16 @@ module.exports = (options = {}) => {
|
||||
logger.addStream(sentryStream(Raven));
|
||||
}
|
||||
|
||||
const robots = [];
|
||||
|
||||
function receive(event) {
|
||||
return Promise.all(robots.map(robot => robot.receive(event)));
|
||||
}
|
||||
|
||||
return {
|
||||
server,
|
||||
webhook,
|
||||
receive,
|
||||
|
||||
start() {
|
||||
server.listen(options.port);
|
||||
@@ -66,17 +76,11 @@ module.exports = (options = {}) => {
|
||||
// Connect the router from the robot to the server
|
||||
server.use(robot.router);
|
||||
|
||||
// Forward webhooks to robot
|
||||
webhook.on('*', event => robot.receive(event));
|
||||
|
||||
// Initialize the plugin
|
||||
plugin(robot);
|
||||
robots.push(robot);
|
||||
|
||||
return robot;
|
||||
},
|
||||
|
||||
receive(event) {
|
||||
return robot.receive(event);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -48,7 +48,6 @@ describe('Probot', () => {
|
||||
['foo', 'bar'].forEach(name => {
|
||||
probot.load(robot => {
|
||||
const app = robot.route('/' + name);
|
||||
console.log(app);
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
res.append('X-Test', name);
|
||||
@@ -68,4 +67,16 @@ describe('Probot', () => {
|
||||
.expect('X-Test', 'bar');
|
||||
});
|
||||
});
|
||||
|
||||
describe('receive', () => {
|
||||
it('forwards events to each plugin', async () => {
|
||||
const spy = expect.createSpy();
|
||||
const robot = probot.load(robot => robot.on('push', spy));
|
||||
robot.auth = expect.createSpy().andReturn(Promise.resolve({}));
|
||||
|
||||
await probot.receive(event);
|
||||
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user