Skip to content

Commit

Permalink
feat: use octokit-auth-probot (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 28, 2020
1 parent 506a5ed commit 8ba3a8e
Show file tree
Hide file tree
Showing 7 changed files with 888 additions and 818 deletions.
1,584 changes: 833 additions & 751 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
"author": "Brandon Keepers",
"license": "ISC",
"dependencies": {
"@octokit/auth-app": "^2.4.14",
"@octokit/auth-unauthenticated": "^1.0.0",
"@octokit/core": "^3.1.0",
"@octokit/graphql": "^4.2.0",
"@octokit/plugin-enterprise-compatibility": "^1.2.1",
Expand Down Expand Up @@ -69,6 +67,7 @@
"js-yaml": "^3.13.1",
"jsonwebtoken": "^8.1.0",
"lru-cache": "^6.0.0",
"octokit-auth-probot": "^1.2.0",
"octokit-pagination-methods": "1.1.0",
"pino": "^6.5.0",
"pino-http": "^5.2.0",
Expand Down
33 changes: 0 additions & 33 deletions src/octokit/get-authenticated-octokit-for-event.ts

This file was deleted.

61 changes: 41 additions & 20 deletions src/octokit/get-authenticated-octokit.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
import { State } from "../types";
import { ProbotOctokit } from "./probot-octokit";

type FactoryOptions = {
octokit: InstanceType<typeof ProbotOctokit>;
octokitOptions: ConstructorParameters<typeof ProbotOctokit> & {
throttle?: Record<string, unknown>;
auth?: Record<string, unknown>;
};
[key: string]: unknown;
};

export async function getAuthenticatedOctokit(
state: State,
installationId?: number
) {
const { githubToken, log, Octokit, octokit } = state;
const { log, octokit } = state;

if (!installationId) return octokit;

const constructorAuthOptions = githubToken
? {}
: { auth: { installationId: installationId } };
return octokit.auth({
type: "installation",
installationId,
factory: ({ octokit, octokitOptions, ...otherOptions }: FactoryOptions) => {
const pinoLog = log.child({ name: "github" });

const pinoLog = log.child({ name: "github" });
const options = {
log: {
fatal: pinoLog.fatal.bind(pinoLog),
error: pinoLog.error.bind(pinoLog),
warn: pinoLog.warn.bind(pinoLog),
info: pinoLog.info.bind(pinoLog),
debug: pinoLog.debug.bind(pinoLog),
trace: pinoLog.trace.bind(pinoLog),
},
throttle: {
id: installationId,
},
...constructorAuthOptions,
};
const options = {
...octokitOptions,
log: {
fatal: pinoLog.fatal.bind(pinoLog),
error: pinoLog.error.bind(pinoLog),
warn: pinoLog.warn.bind(pinoLog),
info: pinoLog.info.bind(pinoLog),
debug: pinoLog.debug.bind(pinoLog),
trace: pinoLog.trace.bind(pinoLog),
},
throttle: {
...octokitOptions.throttle,
id: installationId,
},
auth: {
...octokitOptions.auth,
otherOptions,
installationId,
},
};

const Octokit = octokit.constructor as typeof ProbotOctokit;

return new Octokit(options);
return new Octokit(options);
},
}) as Promise<InstanceType<typeof ProbotOctokit>>;
}
16 changes: 7 additions & 9 deletions src/octokit/get-probot-octokit-with-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createAppAuth } from "@octokit/auth-app";
import LRUCache from "lru-cache";
import { ProbotOctokit } from "./probot-octokit";
import Redis from "ioredis";
Expand Down Expand Up @@ -30,14 +29,13 @@ type Options = {
*/
export function getProbotOctokitWithDefaults(options: Options) {
const authOptions = options.githubToken
? { auth: options.githubToken }
? {
token: options.githubToken,
}
: {
auth: {
cache: options.cache,
id: options.appId,
privateKey: options.privateKey,
},
authStrategy: createAppAuth,
cache: options.cache,
appId: options.appId,
privateKey: options.privateKey,
};

const octokitThrottleOptions = getOctokitThrottleOptions({
Expand All @@ -49,7 +47,7 @@ export function getProbotOctokitWithDefaults(options: Options) {
baseUrl:
process.env.GHE_HOST &&
`${process.env.GHE_PROTOCOL || "https"}://${process.env.GHE_HOST}/api/v3`,
...authOptions,
auth: authOptions,
};

if (options.throttleOptions || octokitThrottleOptions) {
Expand Down
6 changes: 4 additions & 2 deletions src/octokit/octokit-webhooks-transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { WebhookEvent } from "@octokit/webhooks";

import { getAuthenticatedOctokitForEvent } from "./get-authenticated-octokit-for-event";
import { Context } from "../context";
import { State } from "../types";

Expand All @@ -11,6 +10,9 @@ import { State } from "../types";
*/
export async function webhookTransform(state: State, event: WebhookEvent) {
const log = state.log.child({ name: "event", id: event.id });
const github = await getAuthenticatedOctokitForEvent(state, event);
const github = (await state.octokit.auth({
type: "event-octokit",
event,
})) as typeof state.octokit;
return new Context(event, github, log);
}
3 changes: 2 additions & 1 deletion src/octokit/probot-octokit.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Octokit } from "@octokit/core";
import { enterpriseCompatibility } from "@octokit/plugin-enterprise-compatibility";
import { RequestOptions } from "@octokit/types";

import { paginateRest } from "@octokit/plugin-paginate-rest";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { config } from "@probot/octokit-plugin-config";
import { createProbotAuth } from "octokit-auth-probot";

import { probotRequestLogging } from "./octokit-plugin-probot-request-logging";
import { VERSION } from "../version";

const defaultOptions = {
authStrategy: createProbotAuth,
throttle: {
onAbuseLimit: (
retryAfter: number,
Expand Down

0 comments on commit 8ba3a8e

Please sign in to comment.