Skip to content

Commit 7a8f268

Browse files
committed
feat: deprecate probot.start() / probot.stop() / probot.setup()
1 parent a5c78b1 commit 7a8f268

16 files changed

Lines changed: 318 additions & 177 deletions

src/bin/probot-receive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const log = getLog({
7979
logWarningsForObsoleteEnvironmentVariables(log);
8080

8181
const probot = new Probot({
82-
id: program.app,
82+
appId: program.app,
8383
privateKey: String(privateKey),
8484
githubToken: githubToken,
8585
log,

src/bin/read-cli-options.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ export function readCliOptions(
7272
)
7373
.parse(argv);
7474

75-
const { app: id, privateKey: privateKeyPath, redisUrl, ...options } = program;
75+
const {
76+
app: appId,
77+
privateKey: privateKeyPath,
78+
redisUrl,
79+
...options
80+
} = program;
7681

7782
return {
7883
privateKey: getPrivateKey({ filepath: privateKeyPath }) || undefined,
79-
id,
84+
appId,
8085
redisConfig: redisUrl,
8186
...options,
8287
};

src/bin/read-env-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function readEnvOptions(
99
return {
1010
args: [],
1111
privateKey: (privateKey && privateKey.toString()) || undefined,
12-
id: Number(env.APP_ID),
12+
appId: Number(env.APP_ID),
1313
port: Number(env.PORT) || 3000,
1414
host: env.HOST,
1515
secret: env.WEBHOOK_SECRET,

src/load.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function bindMethod(app: Probot | Application, key: keyof Application) {
3535
* Loads an ApplicationFunction into the current Application
3636
* @param appFn - Probot application function to load
3737
*/
38-
export function load(
38+
export async function load(
3939
app: Application | Probot,
4040
router: Router | null,
4141
appFn: string | ApplicationFunction | ApplicationFunction[]
@@ -63,17 +63,20 @@ export function load(
6363
);
6464

6565
if (Array.isArray(appFn)) {
66-
appFn.forEach((fn) => load(app, router, fn));
67-
} else {
68-
const fn = typeof appFn === "string" ? resolveAppFunction(appFn) : appFn;
69-
70-
fn(
71-
(Object.assign(deprecatedApp, {
72-
app,
73-
getRouter: getRouter.bind(null, router || app.router),
74-
}) as unknown) as ApplicationFunctionOptions
75-
);
66+
for (const fn of appFn) {
67+
await load(app, router, fn);
68+
}
69+
return app;
7670
}
7771

72+
const fn = typeof appFn === "string" ? resolveAppFunction(appFn) : appFn;
73+
74+
await fn(
75+
(Object.assign(deprecatedApp, {
76+
app,
77+
getRouter: getRouter.bind(null, router || app.router),
78+
}) as unknown) as ApplicationFunctionOptions
79+
);
80+
7881
return app;
7982
}

src/probot.ts

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,22 @@ export class Probot {
9595
this.log.warn(new Deprecation(logEnvVariableDeprecation));
9696
}
9797

98-
if (options.cert) {
98+
if (options.id) {
9999
this.log.warn(
100100
new Deprecation(
101-
`[probot] "cert" option is deprecated. Use "privateKey" instead`
101+
`[probot] "id" option is deprecated. Use "appId" instead`
102102
)
103103
);
104-
options.privateKey = options.privateKey || options.cert;
104+
options.appId = options.appId || options.id;
105105
}
106106

107-
if (options.id) {
107+
if (options.cert) {
108108
this.log.warn(
109109
new Deprecation(
110-
`[probot] "id" option is deprecated. Use "appId" instead`
110+
`[probot] "cert" option is deprecated. Use "privateKey" instead`
111111
)
112112
);
113-
options.appId = options.appId || options.id;
113+
options.privateKey = options.privateKey || options.cert;
114114
}
115115

116116
if (process.env.INSTALLATION_TOKEN_TTL) {
@@ -150,7 +150,7 @@ export class Probot {
150150
path: options.webhookPath,
151151
secret: options.secret,
152152
},
153-
id: options.id,
153+
appId: Number(options.appId),
154154
privateKey: options.privateKey,
155155
host: options.host,
156156
port: options.port,
@@ -218,6 +218,37 @@ export class Probot {
218218
}
219219

220220
public setup(appFns: Array<string | ApplicationFunction>) {
221+
this.log.warn(
222+
new Deprecation(
223+
`[probot] "probot.setup()" is deprecated. Use the new "Server" class instead. Pass the function as "new Server({ app })" parameter:
224+
225+
const { Server, Probot } = require("probot")
226+
const server = new Server(async ({ probot }) => {}, {
227+
// optional:
228+
host,
229+
port,
230+
webhookPath,
231+
webhookProxy,
232+
Probot: Probot.defaults({ id, privateKey })
233+
})
234+
235+
// start listening to requests
236+
await server.start()
237+
// stop server with: await server.stop()
238+
239+
If you have more than one app function, combine them in a function instead
240+
241+
const app1 = require("./app1")
242+
const app2 = require("./app2")
243+
244+
async function app ({ probot, getRouter }) {
245+
await app1({ probot, getRouter })
246+
await app2({ probot, getRouter })
247+
}
248+
`
249+
)
250+
);
251+
221252
// Log all unhandled rejections
222253
process.on("unhandledRejection", getErrorHandler(this.log));
223254

@@ -226,6 +257,26 @@ export class Probot {
226257
}
227258

228259
public start() {
260+
this.log.warn(
261+
new Deprecation(
262+
`[probot] "probot.start()" is deprecated. Use the new "Server" class instead:
263+
264+
const { Server, Probot } = require("probot")
265+
const server = new Server(async ({ probot }) => {}, {
266+
// optional:
267+
host,
268+
port,
269+
webhookPath,
270+
webhookProxy,
271+
Probot: Probot.defaults({ id, privateKey, ... })
272+
})
273+
274+
// start listening to requests
275+
await server.start()
276+
// stop server with: await server.stop()
277+
`
278+
)
279+
);
229280
this.log.info(
230281
`Running Probot v${this.version} (Node.js: ${process.version})`
231282
);

src/run.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { getLog, GetLogOptions } from "./helpers/get-log";
1010
import { readCliOptions } from "./bin/read-cli-options";
1111
import { readEnvOptions } from "./bin/read-env-options";
1212
import { Server, ServerOptions } from "./server/server";
13-
import { load } from "./load";
1413
import { defaultApp } from "./apps/default";
14+
import { resolveAppFunction } from "./helpers/resolve-app-function";
15+
import { load } from "./load";
1516

1617
type AdditionalOptions = {
1718
env: Record<string, string | undefined>;
@@ -39,7 +40,7 @@ export async function run(
3940
webhookProxy,
4041

4142
// probot options
42-
id,
43+
appId,
4344
privateKey,
4445
redisConfig,
4546
secret,
@@ -61,30 +62,28 @@ export async function run(
6162
const log = getLog(logOptions);
6263
logWarningsForObsoleteEnvironmentVariables(log);
6364

65+
const probotOptions: Options = {
66+
appId,
67+
privateKey,
68+
redisConfig,
69+
secret,
70+
baseUrl,
71+
log: log.child({ name: "probot" }),
72+
};
6473
const serverOptions: ServerOptions = {
6574
host,
6675
port,
6776
webhookPath,
6877
webhookProxy,
6978
log: log.child({ name: "server" }),
79+
Probot: Probot.defaults(probotOptions),
7080
};
7181

72-
const server = new Server(serverOptions);
73-
const router = server.router();
74-
75-
const probotOptions: Options = {
76-
id,
77-
privateKey,
78-
redisConfig,
79-
secret,
80-
baseUrl,
81-
log: log.child({ name: "probot" }),
82-
};
83-
const probot = new Probot(probotOptions);
82+
let server: Server;
8483

85-
if (!id || !privateKey) {
84+
if (!appId || !privateKey) {
8685
if (process.env.NODE_ENV === "production") {
87-
if (!id) {
86+
if (!appId) {
8887
throw new Error(
8988
"Application ID is missing, and is required to run in production mode. " +
9089
"To resolve, ensure the APP_ID environment variable is set."
@@ -96,27 +95,35 @@ export async function run(
9695
);
9796
}
9897
}
99-
load(probot, router, setupAppFactory(host, port));
100-
} else if (Array.isArray(appFnOrArgv)) {
101-
const pkg = await pkgConf("probot");
102-
load(probot, router, defaultApp);
98+
server = new Server(setupAppFactory(host, port), serverOptions);
99+
await server.start();
100+
return server;
101+
}
103102

104-
if (Array.isArray(pkg.apps)) {
105-
for (const app of pkg.apps) {
106-
load(probot, router, app);
107-
}
108-
}
103+
if (Array.isArray(appFnOrArgv)) {
104+
const pkg = await pkgConf("probot");
109105

110-
const [appFn] = args;
106+
const combinedApps: ApplicationFunction = ({ app }) => {
107+
load(app, server.router(), defaultApp);
111108

112-
load(probot, router, appFn);
109+
if (Array.isArray(pkg.apps)) {
110+
for (const appPath of pkg.apps) {
111+
const appFn = resolveAppFunction(appPath);
112+
load(app, server.router(), appFn);
113+
}
114+
}
113115

114-
server.app.use(webhookPath ? webhookPath : "/", probot.webhooks.middleware);
115-
} else {
116-
load(probot, router, appFnOrArgv);
116+
const [appPath] = args;
117+
const appFn = resolveAppFunction(appPath);
118+
load(app, server.router(), appFn);
119+
};
117120

118-
server.app.use(webhookPath ? webhookPath : "/", probot.webhooks.middleware);
121+
server = new Server(combinedApps, serverOptions);
122+
await server.start();
123+
return server;
119124
}
125+
126+
server = new Server(appFnOrArgv, serverOptions);
120127
await server.start();
121128

122129
return server;

0 commit comments

Comments
 (0)