Skip to content

Commit

Permalink
feat(auth): pass octokit and octokitOptions to `options.authStrat…
Browse files Browse the repository at this point in the history
…egy` (#233)
  • Loading branch information
gr2m authored Oct 27, 2020
1 parent 690b9fb commit d4f580a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,19 @@ export class Octokit {
this.auth = auth;
}
} else {
const auth = options.authStrategy(
const { authStrategy, ...otherOptions } = options;
const auth = authStrategy(
Object.assign(
{
request: this.request,
log: this.log,
// we pass the current octokit instance as well as its constructor options
// to allow for authentication strategies that return a new octokit instance
// that shares the same internal state as the current one. The original
// requirement for this was the "event-octokit" authentication strategy
// of https://github.com/probot/octokit-auth-probot.
octokit: this,
octokitOptions: otherOptions,
},
options.auth
)
Expand Down
29 changes: 29 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,33 @@ describe("Authentication", () => {
"[@octokit/auth-app] Retrying after 401 response to account for token replication delay (retry: 1, wait: 1s)"
);
});

it("should pass octokit and octokitOptions if a custom authStrategy was set", () => {
const authStrategy = jest.fn().mockReturnValue({
hook() {},
});
new Octokit({
authStrategy,
auth: {
secret: "123",
},
someUnrelatedOption: "value",
});

const strategyOptions = authStrategy.mock.calls[0][0];

expect(Object.keys(strategyOptions).sort()).toStrictEqual([
"log",
"octokit",
"octokitOptions",
"request",
"secret",
]);
expect(strategyOptions.octokitOptions).toStrictEqual({
auth: {
secret: "123",
},
someUnrelatedOption: "value",
});
});
});

0 comments on commit d4f580a

Please sign in to comment.