/
get-probot-octokit-with-defaults.ts
93 lines (82 loc) 路 2.6 KB
/
get-probot-octokit-with-defaults.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { LRUCache } from "lru-cache";
import { ProbotOctokit } from "./probot-octokit.js";
import type { RedisOptions } from "ioredis";
import { request } from "@octokit/request";
import { getOctokitThrottleOptions } from "./get-octokit-throttle-options.js";
import type { Logger } from "pino";
import type { RequestRequestOptions } from "@octokit/types";
import type { OctokitOptions } from "../types.js";
type Options = {
cache: LRUCache<number, string>;
Octokit: typeof ProbotOctokit;
log: Logger;
githubToken?: string;
appId?: number;
privateKey?: string;
redisConfig?: RedisOptions | string;
webhookPath?: string;
baseUrl?: string;
request?: RequestRequestOptions;
};
/**
* Returns an Octokit instance with default settings for authentication. If
* a `githubToken` is passed explicitly, the Octokit instance will be
* pre-authenticated with that token when instantiated. Otherwise Octokit's
* app authentication strategy is used, and `options.auth` options are merged
* deeply when instantiated.
*
* Besides the authentication, the Octokit's baseUrl is set as well when run
* against a GitHub Enterprise Server with a custom domain.
*/
export function getProbotOctokitWithDefaults(options: Options) {
const authOptions = options.githubToken
? {
token: options.githubToken,
request: request.defaults({
request: {
fetch: options.request?.fetch,
},
}),
}
: {
cache: options.cache,
appId: options.appId,
privateKey: options.privateKey,
request: request.defaults({
request: {
fetch: options.request?.fetch,
},
}),
};
const octokitThrottleOptions = getOctokitThrottleOptions({
log: options.log,
redisConfig: options.redisConfig,
});
let defaultOptions: Partial<OctokitOptions> = {
auth: authOptions,
log: options.log.child
? options.log.child({ name: "octokit" })
: options.log,
};
if (options.baseUrl) {
defaultOptions.baseUrl = options.baseUrl;
}
if (octokitThrottleOptions) {
defaultOptions.throttle = octokitThrottleOptions;
}
return options.Octokit.defaults((instanceOptions: OctokitOptions) => {
const options = Object.assign({}, defaultOptions, instanceOptions, {
auth: instanceOptions.auth
? Object.assign({}, defaultOptions.auth, instanceOptions.auth)
: defaultOptions.auth,
});
if (instanceOptions.throttle) {
options.throttle = Object.assign(
{},
defaultOptions.throttle,
instanceOptions.throttle,
);
}
return options;
});
}