diff --git a/.gitignore b/.gitignore index 76b1021d..34f44cb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .nyc_output coverage node_modules +.idea diff --git a/event-handler/receive.js b/event-handler/receive.js index d8494dd4..75ea6edb 100644 --- a/event-handler/receive.js +++ b/event-handler/receive.js @@ -48,9 +48,15 @@ function receiverHandle (state, event) { .catch(error => errors.push(Object.assign(error, { event }))) }) - return Promise.all(promises).then(() => { + return Promise.all(promises).then((results) => { + const mergedResults = results.reduce((merged, curr) => { + if (typeof curr === 'object') { + return Object.assign({}, merged, curr) + } else { return merged } + }, {}) + if (errors.length === 0) { - return + return mergedResults } errorHandlers.forEach(handler => errors.forEach(wrapErrorHandler.bind(null, handler))) diff --git a/index.d.ts b/index.d.ts index 255942f4..d29fecab 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4105,7 +4105,7 @@ declare class Webhooks { public on(event: "error", callback: (event: Error) => void): void; public on( event: "*" | string[], - callback: (event: Webhooks.WebhookEvent) => Promise | void + callback: (event: Webhooks.WebhookEvent) => Promise | object | void ): void; public on( @@ -4117,7 +4117,7 @@ declare class Webhooks { | "check_run.rerequested", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4128,63 +4128,63 @@ declare class Webhooks { | "check_suite.rerequested", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "commit_comment" | "commit_comment.created", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "content_reference", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "create", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "delete", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "deploy_key" | "deploy_key.created" | "deploy_key.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "deployment", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "deployment_status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "fork", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4193,14 +4193,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadGithubAppAuthorization > - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "gollum", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4211,7 +4211,7 @@ declare class Webhooks { | "installation.new_permissions_accepted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4223,7 +4223,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadInstallationRepositories > - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4234,7 +4234,7 @@ declare class Webhooks { | "issue_comment.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4258,14 +4258,14 @@ declare class Webhooks { | "issues.unpinned", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "label" | "label.created" | "label.deleted" | "label.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4278,28 +4278,28 @@ declare class Webhooks { | "marketplace_purchase.purchased", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "member" | "member.added" | "member.deleted" | "member.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "membership" | "membership.added" | "membership.removed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "meta" | "meta.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4312,7 +4312,7 @@ declare class Webhooks { | "milestone.opened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4325,21 +4325,21 @@ declare class Webhooks { | "organization.renamed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "org_block" | "org_block.blocked" | "org_block.unblocked", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "page_build", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4352,7 +4352,7 @@ declare class Webhooks { | "project_card.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4364,7 +4364,7 @@ declare class Webhooks { | "project_column.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4377,14 +4377,14 @@ declare class Webhooks { | "project.reopened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "public", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4406,7 +4406,7 @@ declare class Webhooks { | "pull_request.synchronize", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4417,7 +4417,7 @@ declare class Webhooks { | "pull_request_review.submitted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4430,14 +4430,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadPullRequestReviewComment > - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "push", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4447,7 +4447,7 @@ declare class Webhooks { | "registry_package.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4461,14 +4461,14 @@ declare class Webhooks { | "release.unpublished", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "repository_dispatch", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4485,14 +4485,14 @@ declare class Webhooks { | "repository.unarchived", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "repository_import", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4505,7 +4505,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadRepositoryVulnerabilityAlert > - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4516,21 +4516,21 @@ declare class Webhooks { | "security_advisory.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "star" | "star.created" | "star.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( @@ -4543,21 +4543,21 @@ declare class Webhooks { | "team.removed_from_repository", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "team_add", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public on( event: "watch" | "watch.started", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | object | void ): void; public sign(data: any): string; diff --git a/middleware/middleware.js b/middleware/middleware.js index 3daffad3..7b09aec1 100644 --- a/middleware/middleware.js +++ b/middleware/middleware.js @@ -11,7 +11,7 @@ function middleware (state, request, response, next) { // the next callback is set when used as an express middleware. That allows // it to define custom routes like /my/custom/page while the webhooks are // expected to be sent to the / root path. Otherwise the root path would - // match all requests and would make it impossible to define custom rooutes + // match all requests and would make it impossible to define custom routes if (typeof next === 'function') { next() return @@ -51,8 +51,9 @@ function middleware (state, request, response, next) { }) }) - .then(() => { - response.end('ok\n') + .then((responseBody) => { + response.setHeader('Content-Type', 'application/json') + response.end(JSON.stringify(responseBody)) }) .catch(error => { diff --git a/test/integration/event-handler-test.js b/test/integration/event-handler-test.js index c805c846..a2317812 100644 --- a/test/integration/event-handler-test.js +++ b/test/integration/event-handler-test.js @@ -129,3 +129,34 @@ test('async options.transform', t => { payload: pushEventPayload }) }) + +test('returned responses', t => { + t.plan(1) + + const eventHandler = new EventHandler() + + function hook1 () { + return Promise.resolve() + .then(() => ({ a: 1 })) + } + function hook2 () { + return { b: false } + } + function hook3 () { + return undefined + } + + eventHandler.on('push', hook1) + eventHandler.on('push', hook2) + eventHandler.on('push', hook3) + + eventHandler.receive({ + id: '123', + name: 'push', + payload: pushEventPayload + }) + .then((responseBody) => { + t.ok(responseBody, { a: 1, b: false }) + }) + .catch(t.error) +}) diff --git a/test/unit/event-handler-receive-test.js b/test/unit/event-handler-receive-test.js index f72ee422..a8e2a177 100644 --- a/test/unit/event-handler-receive-test.js +++ b/test/unit/event-handler-receive-test.js @@ -27,3 +27,14 @@ test('options: name, payload', t => { }) t.end() }) + +test('receive: use returned body for response', t => { + const responseState = { + secret: 'mysecret', + hooks: { + 'a.b': () => ({ a: 'b' }) + } + } + t.resolveMatch(receive(responseState, { name: 'a.b', payload: {} }), { a: 'b' }) + t.end() +})