Skip to content

Commit

Permalink
feat: context.octokit is set for all events, but unauthenticated fo…
Browse files Browse the repository at this point in the history
…r `"token.deleted"` and `"authorization.deleted"` (#149)

BREAKING CHANGE: Before this release, `context.octokit` was not set for `"token.deleted"` and `"authorization.deleted"` events
  • Loading branch information
gr2m authored Nov 2, 2020
1 parent d4ec2c3 commit 0871a92
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test
"on":
push:
branches:
- dependabot/npm_and_yarn/**
- master
pull_request:
types:
- opened
Expand All @@ -15,7 +15,7 @@ jobs:
node_version:
- "10"
- "12"
- "13"
- "14"
steps:
- uses: actions/checkout@v2
- name: "Use Node.js ${{ matrix.node_version }}"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ All event handlers are awaited before continuing.

Authenticated instance using the `Octokit` option passed to the constructor.

Not set for `"token.deleted"` and `"authorization.deleted"` events.
For `"token.deleted"` and `"authorization.deleted"` events the `octokit` instance is unauthenticated.

</td></tr>
<tr>
Expand Down
33 changes: 31 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "MIT",
"dependencies": {
"@octokit/auth-oauth-app": "^3.0.0",
"@octokit/auth-unauthenticated": "^2.0.0",
"@octokit/core": "^3.0.0",
"@octokit/oauth-authorization-url": "^4.1.0",
"@types/btoa-lite": "^1.0.0",
Expand Down
17 changes: 17 additions & 0 deletions src/methods/delete-authorization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
import { request as defaultRequest } from "@octokit/request";

import { emitEvent } from "../emit-event";
Expand Down Expand Up @@ -77,12 +78,28 @@ export async function deleteAuthorizationWithState(
name: "token",
action: "deleted",
token: options.token,
get octokit() {
return new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`,
},
});
},
});

await emitEvent(state, {
name: "authorization",
action: "deleted",
token: options.token,
get octokit() {
return new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "authorization.deleted" event. The access for the app has been revoked.`,
},
});
},
});

return result;
Expand Down
9 changes: 9 additions & 0 deletions src/methods/delete-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { request as defaultRequest } from "@octokit/request";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
import btoa from "btoa-lite";

import { emitEvent } from "../emit-event";
Expand Down Expand Up @@ -67,6 +68,14 @@ export async function deleteTokenWithState(
name: "token",
action: "deleted",
token: options.token,
get octokit() {
return new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`,
},
});
},
});

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type EventHandlerContext = {
action: ActionName;
token: Token;
scopes?: Scope[];
octokit?: InstanceType<typeof OAuthAppOctokit>;
octokit: InstanceType<typeof OAuthAppOctokit>;
};
export type EventHandler = (context: EventHandlerContext) => void;
export type AddEventHandler = (
Expand Down
10 changes: 7 additions & 3 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ describe("app", () => {
});
expect(context_before_deleted.octokit).toBeInstanceOf(Mocktokit);

expect(context_deleted).toStrictEqual({
expect(context_deleted).toMatchObject({
name: "token",
action: "deleted",
token: "token123",
});
expect(context_deleted.octokit).toBeInstanceOf(Mocktokit);
});

it("app.deleteAuthorization(options)", async () => {
Expand Down Expand Up @@ -281,16 +282,19 @@ describe("app", () => {
});
expect(context_token_before_deleted.octokit).toBeInstanceOf(Mocktokit);

expect(context_token_deleted).toStrictEqual({
expect(context_token_deleted).toMatchObject({
name: "token",
action: "deleted",
token: "token123",
});
expect(context_authorization_deleted).toStrictEqual({
expect(context_token_deleted.octokit).toBeInstanceOf(Mocktokit);

expect(context_authorization_deleted).toMatchObject({
name: "authorization",
action: "deleted",
token: "token123",
});
expect(context_authorization_deleted.octokit).toBeInstanceOf(Mocktokit);
});

it("app.on multiple events", async () => {
Expand Down

0 comments on commit 0871a92

Please sign in to comment.