Skip to content

Commit

Permalink
feat: add onAny and onError methods from @octokit/webhooks (#1480)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
  • Loading branch information
wolfy1339 and gr2m committed Mar 1, 2021
1 parent 2c97c99 commit 9a24f9d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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);
});
};
```

Expand Down
4 changes: 2 additions & 2 deletions docs/webhooks.md
Expand Up @@ -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 });
});
};
Expand Down
5 changes: 5 additions & 0 deletions src/probot.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions test/probot.test.ts
Expand Up @@ -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,
Expand Down

0 comments on commit 9a24f9d

Please sign in to comment.