-
Notifications
You must be signed in to change notification settings - Fork 924
/
probot.ts
126 lines (105 loc) 路 3.38 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
import LRUCache from "lru-cache";
import { Logger } from "pino";
import { WebhookEvent } from "@octokit/webhooks";
import { aliasLog } from "./helpers/alias-log";
import { auth } from "./auth";
import { getLog } from "./helpers/get-log";
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults";
import { getWebhooks } from "./octokit/get-webhooks";
import { ProbotOctokit } from "./octokit/probot-octokit";
import { VERSION } from "./version";
import {
ApplicationFunction,
DeprecatedLogger,
Options,
ProbotWebhooks,
State,
} from "./types";
export type Constructor<T> = new (...args: any[]) => T;
export class Probot {
static version = VERSION;
static defaults<S extends Constructor<any>>(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 log: DeprecatedLogger;
public version: String;
public on: ProbotWebhooks["on"];
public onAny: ProbotWebhooks["onAny"];
public onError: ProbotWebhooks["onError"];
public auth: (
installationId?: number,
log?: Logger
) => Promise<InstanceType<typeof ProbotOctokit>>;
private state: State;
constructor(options: Options = {}) {
options.webhookPath = options.webhookPath || "/";
options.secret = options.secret || "development";
let level = options.logLevel;
this.log = aliasLog(options.log || getLog({ level }));
// 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
maxAge: 1000 * 60 * 59,
});
const Octokit = getProbotOctokitWithDefaults({
githubToken: options.githubToken,
Octokit: options.Octokit || ProbotOctokit,
appId: Number(options.appId),
privateKey: options.privateKey,
cache,
log: this.log,
redisConfig: options.redisConfig,
baseUrl: options.baseUrl,
});
const octokit = new Octokit();
this.state = {
cache,
githubToken: options.githubToken,
log: this.log,
Octokit,
octokit,
webhooks: {
path: options.webhookPath,
secret: options.secret,
},
appId: Number(options.appId),
privateKey: options.privateKey,
host: options.host,
port: options.port,
};
this.auth = auth.bind(null, this.state);
this.webhooks = getWebhooks(this.state);
this.on = (eventNameOrNames, callback) => {
if (eventNameOrNames === "*") {
// @ts-ignore this.webhooks.on("*") is deprecated
return this.webhooks.onAny(callback);
}
return this.webhooks.on(eventNameOrNames, callback);
};
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[]) {
if (Array.isArray(appFn)) {
for (const fn of appFn) {
await this.load(fn);
}
return;
}
return appFn(this, {});
}
}