-
Notifications
You must be signed in to change notification settings - Fork 950
/
get-authenticated-octokit.ts
53 lines (46 loc) · 1.41 KB
/
get-authenticated-octokit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 { log, octokit } = state;
if (!installationId) return octokit;
return octokit.auth({
type: "installation",
installationId,
factory: ({ octokit, octokitOptions, ...otherOptions }: FactoryOptions) => {
const pinoLog = log.child({ name: "github" });
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);
},
}) as Promise<InstanceType<typeof ProbotOctokit>>;
}