From 9a24f9dcb50eda72f05eb2cdd777ac47b3c0fecb Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Mon, 1 Mar 2021 13:14:14 -0500 Subject: [PATCH] feat: add `onAny` and `onError` methods from `@octokit/webhooks` (#1480) Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> --- README.md | 8 ++++++++ docs/webhooks.md | 4 ++-- src/probot.ts | 5 +++++ test/probot.test.ts | 13 +++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0b07faac7f..769de09658 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ module.exports = (app) => { }); return context.octokit.issues.createComment(issueComment); }); + + app.onAny(async (context) => { + context.log.info({ event: context.name, action: context.payload.action }); + }); + + app.onError(async (error) => { + context.log.error(error); + }); }; ``` diff --git a/docs/webhooks.md b/docs/webhooks.md index 401722aa57..b09552902b 100644 --- a/docs/webhooks.md +++ b/docs/webhooks.md @@ -40,11 +40,11 @@ module.exports = (app) => { }; ``` -You can also use `app.webhooks.onAny()` to listen for any event that your app is subscribed to: +You can also use `app.onAny()` to listen for any event that your app is subscribed to: ```js module.exports = (app) => { - app.webhooks.onAny(async (context) => { + app.onAny(async (context) => { context.log.info({ event: context.name, action: context.payload.action }); }); }; diff --git a/src/probot.ts b/src/probot.ts index 2fd231be53..73cf316ad1 100644 --- a/src/probot.ts +++ b/src/probot.ts @@ -36,6 +36,8 @@ export class Probot { public log: DeprecatedLogger; public version: String; public on: ProbotWebhooks["on"]; + public onAny: ProbotWebhooks["onAny"]; + public onError: ProbotWebhooks["onError"]; public auth: ( installationId?: number, log?: Logger @@ -100,6 +102,9 @@ export class Probot { return this.webhooks.on(eventNameOrNames, callback); }; + this.onAny = this.webhooks.onAny; + this.onError = this.webhooks.onError; + this.version = VERSION; } diff --git a/test/probot.test.ts b/test/probot.test.ts index 2cc619cc3e..2f8d653341 100644 --- a/test/probot.test.ts +++ b/test/probot.test.ts @@ -330,6 +330,19 @@ describe("Probot", () => { expect(spy).toHaveBeenCalled(); }); + it("calls callback with onAny", async () => { + const probot = new Probot({ + appId, + privateKey, + }); + + const spy = jest.fn(); + probot.onAny(spy); + + await probot.receive(event); + expect(spy).toHaveBeenCalled(); + }); + it("calls callback x amount of times when an array of x actions is passed", async () => { const probot = new Probot({ appId,