Repository navigation
Expand file tree
/
Copy pathrun.ts
More file actions
219 lines (189 loc) · 5.54 KB
/
Copy pathrun.ts
File metadata and controls
219 lines (189 loc) · 5.54 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { packageConfig } from "package-config";
import type { Logger } from "pino";
import type {
ApplicationFunction,
Env,
Options,
ServerOptions,
StripUndefined,
} from "./types.js";
import type { ProbotOctokit } from "./octokit/probot-octokit.js";
import type { SmeeClientModule } from "./helpers/webhook-proxy.js";
import { setupAppFactory } from "./apps/setup.js";
import { getLog } from "./helpers/get-log.js";
import { readCliOptions } from "./bin/read-cli-options.js";
import { readEnvOptions } from "./bin/read-env-options.js";
import { defaultApp as defaultAppHandler } from "./apps/default.js";
import { defaultWebhookPath, Server } from "./server/server.js";
import { resolveAppFunction } from "./helpers/resolve-app-function.js";
import { isProduction } from "./helpers/is-production.js";
import { updateEnv } from "./helpers/update-env.js";
import { Probot } from "./probot.js";
import { applyEnv } from "./helpers/apply-env.js";
import { validateLogLevel } from "./helpers/validate-log-level.js";
type AdditionalOptions = {
env?: Env;
Octokit?: typeof ProbotOctokit;
log?: Logger;
updateEnv?: typeof updateEnv;
SmeeClient?: { createChannel: () => Promise<string | undefined> };
loadSmeeClient?: () => Promise<SmeeClientModule>;
};
/**
*
* @param appFnOrArgv set to either a probot application function: `(app) => { ... }` or to process.argv
*/
export async function run(
appFnOrArgv: ApplicationFunction | string[],
additionalOptions: AdditionalOptions = {},
): Promise<Server> {
applyEnv();
const env: Env = {
...process.env,
...additionalOptions.env,
};
const envOptions = readEnvOptions(env);
const cliOptions = Array.isArray(appFnOrArgv)
? readCliOptions(appFnOrArgv, env)
: {};
const {
// log options
logLevel: level,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,
// server options
host = "localhost",
port = 3000,
webhookPath = defaultWebhookPath,
webhookProxy,
// probot options
appId,
privateKey,
redisConfig,
secret,
baseUrl,
// others
args,
} = { ...envOptions, ...cliOptions };
if (level !== undefined) {
validateLogLevel(level);
}
const log =
additionalOptions.log ||
(await getLog({
level,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,
}));
const probotOptions: Options = {
appId,
privateKey,
redisConfig,
secret,
baseUrl,
log,
Octokit: additionalOptions.Octokit || undefined,
};
type ServerOptionsLocal = StripUndefined<
Required<
NonNullable<
Pick<ServerOptions, "host" | "port" | "webhookPath" | "log" | "Probot">
>
>
> &
Pick<ServerOptions, "webhookProxy" | "loadSmeeClient">;
const serverOptions: ServerOptionsLocal = {
host,
port,
webhookPath,
webhookProxy,
log,
loadSmeeClient: additionalOptions?.loadSmeeClient,
Probot: Probot.defaults(probotOptions),
};
let server: Server;
if (isProduction(env)) {
if (!appId) {
throw new Error(
"App ID is missing, and is required to run in production mode. " +
"To resolve, ensure the APP_ID environment variable is set.",
);
}
if (!privateKey) {
throw new Error(
"Certificate is missing, and is required to run in production mode. " +
"To resolve, ensure either the PRIVATE_KEY or PRIVATE_KEY_PATH environment variable is set and contains a valid certificate",
);
}
}
if (!appId || !privateKey) {
// Workaround for setup (#1512)
// When probot is started for the first time, it gets into a setup mode
// where `appId` and `privateKey` are not present. The setup mode gets
// these credentials. In order to not throw an error, we set the values
// to anything, as the Probot instance is not used in setup it makes no
// difference anyway.
server = new Server({
...serverOptions,
Probot: Probot.defaults({
...probotOptions,
appId: 1,
privateKey: "dummy value for setup, see #1512",
}),
});
const setupAppHandler = setupAppFactory({
host: server.host,
port: server.port,
log: serverOptions.log.child({ name: "setup" }),
...(additionalOptions.updateEnv
? { updateEnv: additionalOptions.updateEnv }
: {}),
env,
SmeeClient: additionalOptions.SmeeClient,
});
await server.loadHandlerFactory(setupAppHandler);
await server.start();
return server;
}
if (Array.isArray(appFnOrArgv)) {
const [appPath] = args;
if (!appPath) {
console.error(
"No app path provided. Please provide the path to the app you want to run.",
);
process.exit(1);
}
const pkg = await packageConfig("probot");
server = new Server(serverOptions);
await server.loadHandlerFactory(defaultAppHandler);
if (Array.isArray(pkg.apps)) {
for (const appPath of pkg.apps) {
const appFn = await resolveAppFunction(appPath);
await server.load(appFn);
}
}
const appFn = await resolveAppFunction(appPath);
await server.load(appFn);
await server.start();
return server;
}
server = new Server(serverOptions);
await server.load(appFnOrArgv);
await server.start();
process.on("SIGINT", async () => {
console.info("Stopping server...");
try {
await server.stop();
console.info("Server stopped");
process.exit(0);
} catch (e) {
console.error({ err: e }, "Error while stopping server");
process.exit(1);
}
});
return server;
}