/
get-probot-octokit-with-defaults.ts
72 lines (62 loc) 路 1.96 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
import LRUCache from "lru-cache";
import { ProbotOctokit } from "./probot-octokit";
import Redis from "ioredis";
import { getOctokitThrottleOptions } from "./get-octokit-throttle-options";
import type { Logger } from "pino";
type Options = {
cache: LRUCache<number, string>;
Octokit: typeof ProbotOctokit;
log: Logger;
githubToken?: string;
appId?: number;
privateKey?: string;
redisConfig?: Redis.RedisOptions | string;
baseUrl?: string;
};
/**
* 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,
}
: {
cache: options.cache,
appId: options.appId,
privateKey: options.privateKey,
};
const octokitThrottleOptions = getOctokitThrottleOptions({
log: options.log,
redisConfig: options.redisConfig,
});
const defaultOptions: any = {
baseUrl: options.baseUrl,
auth: authOptions,
};
if (octokitThrottleOptions) {
defaultOptions.throttle = octokitThrottleOptions;
}
return options.Octokit.defaults((instanceOptions: any) => {
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;
});
}