From d4f580a2dc9aec86c50f174ca83eba042a41b0d0 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 27 Oct 2020 16:43:47 -0700 Subject: [PATCH] feat(auth): pass `octokit` and `octokitOptions` to `options.authStrategy` (#233) --- src/index.ts | 10 +++++++++- test/auth.test.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index a604554d6..1a6a86904 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ) diff --git a/test/auth.test.ts b/test/auth.test.ts index 1c87e9791..46ae1d2ec 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -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", + }); + }); });