/
get-probot-octokit-with-defaults.ts
90 lines (78 loc) 路 2.51 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
import { Deprecation } from "deprecation";
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;
throttleOptions?: any;
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,
});
if (!options.baseUrl && process.env.GHE_HOST) {
options.baseUrl = `${process.env.GHE_PROTOCOL || "https"}://${
process.env.GHE_HOST
}/api/v3`;
options.log.warn(
new Deprecation(
`[probot] "GHE_HOST"/"GHE_PROTOCOL" is deprecated when using with the Probot constructor. Use "new Probot({ baseUrl: 'https://github.acme-inc.com/api/v3' })" instead`
)
);
}
const defaultOptions: any = {
baseUrl: options.baseUrl,
auth: authOptions,
};
if (options.throttleOptions || octokitThrottleOptions) {
defaultOptions.throttle = Object.assign(
{},
options.throttleOptions,
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;
});
}