-
Notifications
You must be signed in to change notification settings - Fork 923
/
probot.ts
136 lines (117 loc) 路 3.85 KB
/
probot.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { LRUCache } from "lru-cache";
import type { Logger } from "pino";
import type { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks";
import { auth } from "./auth.js";
import { getLog } from "./helpers/get-log.js";
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults.js";
import { getWebhooks } from "./octokit/get-webhooks.js";
import { ProbotOctokit } from "./octokit/probot-octokit.js";
import { VERSION } from "./version.js";
import type {
ApplicationFunction,
ApplicationFunctionOptions,
Options,
ProbotWebhooks,
State,
} from "./types.js";
import { defaultWebhooksPath } from "./server/server.js";
import { rebindLog } from "./helpers/rebind-log.js";
export type Constructor<T = any> = new (...args: any[]) => T;
export class Probot {
static version = VERSION;
static defaults<S extends Constructor>(this: S, defaults: Options) {
const ProbotWithDefaults = class extends this {
constructor(...args: any[]) {
const options = args[0] || {};
super(Object.assign({}, defaults, options));
}
};
return ProbotWithDefaults;
}
public webhooks: ProbotWebhooks;
public webhookPath: string;
public log: Logger;
public version: String;
public on: ProbotWebhooks["on"];
public onAny: ProbotWebhooks["onAny"];
public onError: ProbotWebhooks["onError"];
public auth: (
installationId?: number,
log?: Logger,
) => Promise<ProbotOctokit>;
private state: State;
constructor(options: Options = {}) {
options.secret = options.secret || "development";
let level = options.logLevel;
const logMessageKey = options.logMessageKey;
this.log = options.log
? rebindLog(options.log)
: getLog({ level, logMessageKey });
// TODO: support redis backend for access token cache if `options.redisConfig`
const cache = new LRUCache<number, string>({
// cache max. 15000 tokens, that will use less than 10mb memory
max: 15000,
// Cache for 1 minute less than GitHub expiry
ttl: 1000 * 60 * 59,
});
const Octokit = getProbotOctokitWithDefaults({
githubToken: options.githubToken,
Octokit: options.Octokit || ProbotOctokit,
appId: Number(options.appId),
privateKey: options.privateKey,
cache,
log: rebindLog(this.log),
redisConfig: options.redisConfig,
baseUrl: options.baseUrl,
});
const octokitLogger = rebindLog(this.log.child({ name: "octokit" }));
const octokit = new Octokit({
request: options.request,
log: {
debug: octokitLogger.debug.bind(octokitLogger),
info: octokitLogger.info.bind(octokitLogger),
warn: octokitLogger.warn.bind(octokitLogger),
error: octokitLogger.error.bind(octokitLogger),
},
});
this.state = {
cache,
githubToken: options.githubToken,
log: rebindLog(this.log),
Octokit,
octokit,
webhooks: {
secret: options.secret,
},
appId: Number(options.appId),
privateKey: options.privateKey,
host: options.host,
port: options.port,
webhookPath: options.webhookPath || defaultWebhooksPath,
request: options.request,
};
this.auth = auth.bind(null, this.state);
this.webhooks = getWebhooks(this.state);
this.webhookPath = this.state.webhookPath;
this.on = this.webhooks.on;
this.onAny = this.webhooks.onAny;
this.onError = this.webhooks.onError;
this.version = VERSION;
}
public receive(event: WebhookEvent) {
this.log.debug({ event }, "Webhook received");
return this.webhooks.receive(event);
}
public async load(
appFn: ApplicationFunction | ApplicationFunction[],
options: ApplicationFunctionOptions = {},
) {
if (Array.isArray(appFn)) {
for (const fn of appFn) {
await this.load(fn);
}
return;
}
return appFn(this, options);
}
}