From ac4a20bc3e484d83c1de36639f6570d637d7be2e Mon Sep 17 00:00:00 2001 From: moliver Date: Mon, 4 Nov 2019 14:51:18 -0500 Subject: [PATCH 1/5] Send optional response body --- .idea/$PRODUCT_WORKSPACE_FILE$ | 19 ++++ .idea/.gitignore | 2 + .idea/checkstyle-idea.xml | 16 ++++ .idea/codeStyles/Project.xml | 23 +++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/inspectionProfiles/Project_Default.xml | 6 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/sbt.xml | 6 ++ .idea/vcs.xml | 6 ++ .idea/webhooks.js.iml | 9 ++ index.d.ts | 92 ++++++++++---------- middleware/middleware.js | 13 ++- package.json | 2 +- 14 files changed, 163 insertions(+), 50 deletions(-) create mode 100644 .idea/$PRODUCT_WORKSPACE_FILE$ create mode 100644 .idea/.gitignore create mode 100644 .idea/checkstyle-idea.xml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sbt.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/webhooks.js.iml diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 00000000..3733e0d3 --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 00000000..5eec315c --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..0919b7f8 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..79ee123c --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..0be03f46 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..28a804d8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..ef42f20b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sbt.xml b/.idea/sbt.xml new file mode 100644 index 00000000..20187435 --- /dev/null +++ b/.idea/sbt.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/webhooks.js.iml b/.idea/webhooks.js.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/webhooks.js.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 255942f4..1c549d1b 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 | string | object | void ): void; public on( @@ -4117,7 +4117,7 @@ declare class Webhooks { | "check_run.rerequested", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4128,63 +4128,63 @@ declare class Webhooks { | "check_suite.rerequested", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "commit_comment" | "commit_comment.created", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "content_reference", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "create", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "delete", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "deploy_key" | "deploy_key.created" | "deploy_key.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "deployment", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "deployment_status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "fork", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4193,14 +4193,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadGithubAppAuthorization > - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "gollum", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4211,7 +4211,7 @@ declare class Webhooks { | "installation.new_permissions_accepted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4223,7 +4223,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadInstallationRepositories > - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4234,7 +4234,7 @@ declare class Webhooks { | "issue_comment.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4258,14 +4258,14 @@ declare class Webhooks { | "issues.unpinned", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "label" | "label.created" | "label.deleted" | "label.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4278,28 +4278,28 @@ declare class Webhooks { | "marketplace_purchase.purchased", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "member" | "member.added" | "member.deleted" | "member.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "membership" | "membership.added" | "membership.removed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "meta" | "meta.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4312,7 +4312,7 @@ declare class Webhooks { | "milestone.opened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4325,21 +4325,21 @@ declare class Webhooks { | "organization.renamed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "org_block" | "org_block.blocked" | "org_block.unblocked", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "page_build", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4352,7 +4352,7 @@ declare class Webhooks { | "project_card.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4364,7 +4364,7 @@ declare class Webhooks { | "project_column.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4377,14 +4377,14 @@ declare class Webhooks { | "project.reopened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "public", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4406,7 +4406,7 @@ declare class Webhooks { | "pull_request.synchronize", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4417,7 +4417,7 @@ declare class Webhooks { | "pull_request_review.submitted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4430,14 +4430,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadPullRequestReviewComment > - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "push", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4447,7 +4447,7 @@ declare class Webhooks { | "registry_package.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4461,14 +4461,14 @@ declare class Webhooks { | "release.unpublished", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "repository_dispatch", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4485,14 +4485,14 @@ declare class Webhooks { | "repository.unarchived", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "repository_import", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4505,7 +4505,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadRepositoryVulnerabilityAlert > - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4516,21 +4516,21 @@ declare class Webhooks { | "security_advisory.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "star" | "star.created" | "star.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( @@ -4543,21 +4543,21 @@ declare class Webhooks { | "team.removed_from_repository", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "team_add", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public on( event: "watch" | "watch.started", callback: ( event: Webhooks.WebhookEvent - ) => Promise | void + ) => Promise | string | object | void ): void; public sign(data: any): string; diff --git a/middleware/middleware.js b/middleware/middleware.js index 3daffad3..4544c568 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,15 @@ function middleware (state, request, response, next) { }) }) - .then(() => { - response.end('ok\n') + .then((responseBody) => { + if (typeof responseBody === 'object') { + response.setHeader('Content-Type', 'application/json') + response.end(JSON.stringify(responseBody)) + } else if (typeof responseBody === 'string') { + response.end(responseBody) + } else { + response.end('ok\n') + } }) .catch(error => { diff --git a/package.json b/package.json index 794a0d58..73adcf14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@octokit/webhooks", - "version": "0.0.0-development", + "version": "1.0.0-MLB", "publishConfig": { "access": "public" }, From 21bd6a9d1c2a021b22aa82a10229016b5673b550 Mon Sep 17 00:00:00 2001 From: moliver Date: Mon, 4 Nov 2019 14:52:00 -0500 Subject: [PATCH 2/5] Remove idea --- .gitignore | 1 + .idea/$PRODUCT_WORKSPACE_FILE$ | 19 ---------------- .idea/.gitignore | 2 -- .idea/checkstyle-idea.xml | 16 -------------- .idea/codeStyles/Project.xml | 23 -------------------- .idea/codeStyles/codeStyleConfig.xml | 5 ----- .idea/inspectionProfiles/Project_Default.xml | 6 ----- .idea/misc.xml | 6 ----- .idea/modules.xml | 8 ------- .idea/sbt.xml | 6 ----- .idea/vcs.xml | 6 ----- .idea/webhooks.js.iml | 9 -------- 12 files changed, 1 insertion(+), 106 deletions(-) delete mode 100644 .idea/$PRODUCT_WORKSPACE_FILE$ delete mode 100644 .idea/.gitignore delete mode 100644 .idea/checkstyle-idea.xml delete mode 100644 .idea/codeStyles/Project.xml delete mode 100644 .idea/codeStyles/codeStyleConfig.xml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/sbt.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/webhooks.js.iml 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/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ deleted file mode 100644 index 3733e0d3..00000000 --- a/.idea/$PRODUCT_WORKSPACE_FILE$ +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - 1.8 - - - - - - - - \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 5c98b428..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml deleted file mode 100644 index 5eec315c..00000000 --- a/.idea/checkstyle-idea.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 0919b7f8..00000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml deleted file mode 100644 index 79ee123c..00000000 --- a/.idea/codeStyles/codeStyleConfig.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 0be03f46..00000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d8..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index ef42f20b..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/sbt.xml b/.idea/sbt.xml deleted file mode 100644 index 20187435..00000000 --- a/.idea/sbt.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddf..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/webhooks.js.iml b/.idea/webhooks.js.iml deleted file mode 100644 index d6ebd480..00000000 --- a/.idea/webhooks.js.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file From 9c93334e30430d0093436cb4a337d7bdb91250c3 Mon Sep 17 00:00:00 2001 From: moliver Date: Tue, 5 Nov 2019 12:06:32 -0500 Subject: [PATCH 3/5] Add merged response bodies from hooks --- event-handler/receive.js | 10 ++- index.d.ts | 92 ++++++++++++------------- middleware/middleware.js | 10 +-- test/integration/event-handler-test.js | 27 ++++++++ test/unit/event-handler-receive-test.js | 11 +++ 5 files changed, 94 insertions(+), 56 deletions(-) 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 1c549d1b..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 | string | object | 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 | string | object | void + ) => Promise | object | void ): void; public on( @@ -4128,63 +4128,63 @@ declare class Webhooks { | "check_suite.rerequested", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "commit_comment" | "commit_comment.created", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "content_reference", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "create", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "delete", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "deploy_key" | "deploy_key.created" | "deploy_key.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "deployment", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "deployment_status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "fork", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4193,14 +4193,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadGithubAppAuthorization > - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "gollum", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4211,7 +4211,7 @@ declare class Webhooks { | "installation.new_permissions_accepted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4223,7 +4223,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadInstallationRepositories > - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4234,7 +4234,7 @@ declare class Webhooks { | "issue_comment.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4258,14 +4258,14 @@ declare class Webhooks { | "issues.unpinned", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "label" | "label.created" | "label.deleted" | "label.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4278,28 +4278,28 @@ declare class Webhooks { | "marketplace_purchase.purchased", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "member" | "member.added" | "member.deleted" | "member.edited", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "membership" | "membership.added" | "membership.removed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "meta" | "meta.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4312,7 +4312,7 @@ declare class Webhooks { | "milestone.opened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4325,21 +4325,21 @@ declare class Webhooks { | "organization.renamed", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "org_block" | "org_block.blocked" | "org_block.unblocked", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "page_build", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4352,7 +4352,7 @@ declare class Webhooks { | "project_card.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4364,7 +4364,7 @@ declare class Webhooks { | "project_column.moved", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4377,14 +4377,14 @@ declare class Webhooks { | "project.reopened", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "public", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4406,7 +4406,7 @@ declare class Webhooks { | "pull_request.synchronize", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4417,7 +4417,7 @@ declare class Webhooks { | "pull_request_review.submitted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4430,14 +4430,14 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadPullRequestReviewComment > - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "push", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4447,7 +4447,7 @@ declare class Webhooks { | "registry_package.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4461,14 +4461,14 @@ declare class Webhooks { | "release.unpublished", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "repository_dispatch", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4485,14 +4485,14 @@ declare class Webhooks { | "repository.unarchived", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "repository_import", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4505,7 +4505,7 @@ declare class Webhooks { event: Webhooks.WebhookEvent< Webhooks.WebhookPayloadRepositoryVulnerabilityAlert > - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4516,21 +4516,21 @@ declare class Webhooks { | "security_advisory.updated", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "star" | "star.created" | "star.deleted", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "status", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( @@ -4543,21 +4543,21 @@ declare class Webhooks { | "team.removed_from_repository", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "team_add", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public on( event: "watch" | "watch.started", callback: ( event: Webhooks.WebhookEvent - ) => Promise | string | object | void + ) => Promise | object | void ): void; public sign(data: any): string; diff --git a/middleware/middleware.js b/middleware/middleware.js index 4544c568..7b09aec1 100644 --- a/middleware/middleware.js +++ b/middleware/middleware.js @@ -52,14 +52,8 @@ function middleware (state, request, response, next) { }) .then((responseBody) => { - if (typeof responseBody === 'object') { - response.setHeader('Content-Type', 'application/json') - response.end(JSON.stringify(responseBody)) - } else if (typeof responseBody === 'string') { - response.end(responseBody) - } else { - response.end('ok\n') - } + 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..26868a2b 100644 --- a/test/integration/event-handler-test.js +++ b/test/integration/event-handler-test.js @@ -129,3 +129,30 @@ 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 } + } + + eventHandler.on('push', hook1) + eventHandler.on('push', hook2) + + 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() +}) From 0e956c8ea67a87fbacccd3e0604e270329fe6e32 Mon Sep 17 00:00:00 2001 From: moliver Date: Tue, 5 Nov 2019 12:13:47 -0500 Subject: [PATCH 4/5] Add undefined case --- test/integration/event-handler-test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/integration/event-handler-test.js b/test/integration/event-handler-test.js index 26868a2b..a2317812 100644 --- a/test/integration/event-handler-test.js +++ b/test/integration/event-handler-test.js @@ -142,9 +142,13 @@ test('returned responses', t => { 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', From 6fa376bfb15d09980a44bf157c09335eee6ccb66 Mon Sep 17 00:00:00 2001 From: moliver Date: Tue, 5 Nov 2019 12:21:41 -0500 Subject: [PATCH 5/5] Revert package.json change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 73adcf14..794a0d58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@octokit/webhooks", - "version": "1.0.0-MLB", + "version": "0.0.0-development", "publishConfig": { "access": "public" },