From d1952641f23600a30691c16f42ec8e1c1ec44484 Mon Sep 17 00:00:00 2001 From: Harry Dobrev Date: Tue, 12 Mar 2024 19:00:08 +0200 Subject: [PATCH] feat(run): allow passing `Octokit` and `log` (#1984) --- docs/development.md | 16 +++++++++++++++- src/run.ts | 9 ++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/development.md b/docs/development.md index a34788b8..69435deb 100644 --- a/docs/development.md +++ b/docs/development.md @@ -166,6 +166,20 @@ import app from "./index.js"; run(app); ``` +You can also set your own [`Octokit` constructor](https://github.com/octokit/core.js) with custom plugins and a custom logger while still reading default options from `.env`: + +```js +// main.js +import { run } from "probot"; +import app from "./index.js"; + +// pass a probot app function by overriding Probot options +run(app, { + Octokit: ProbotOctokit.plugin(myPlugin), + log: pino(), +}); +``` + Now you can run `main.js` however you want. ### Use server @@ -175,7 +189,7 @@ The [`run`](https://github.com/probot/probot/blob/master/src/run.ts) function th 1. Read configuration from environment variables and local files 2. Start a [`Server`](https://probot.github.io/api/latest/classes/server_server.Server.html) instance -Among other things, using the Server instance permits you to set your own [`Octokit` constructor](https://github.com/octokit/core.js) with custom plugins and a custom logger. +If you need more control over the Server instance, you can use the `Server` class directly: ```js import { Server, Probot } from "probot"; diff --git a/src/run.ts b/src/run.ts index b4f8af99..1048a8f0 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,7 +1,7 @@ import pkgConf from "pkg-conf"; import type { ApplicationFunction, Options, ServerOptions } from "./types.js"; -import { Probot } from "./index.js"; +import { Logger, Probot, ProbotOctokit } from "./index.js"; import { setupAppFactory } from "./apps/setup.js"; import { getLog } from "./helpers/get-log.js"; import { readCliOptions } from "./bin/read-cli-options.js"; @@ -13,7 +13,9 @@ import { isProduction } from "./helpers/is-production.js"; import { config as dotenvConfig } from "dotenv"; type AdditionalOptions = { - env: NodeJS.ProcessEnv; + env?: NodeJS.ProcessEnv; + Octokit?: typeof ProbotOctokit; + log?: Logger; }; /** @@ -70,7 +72,8 @@ export async function run( redisConfig, secret, baseUrl, - log: log.child({ name: "probot" }), + log: additionalOptions?.log || log.child({ name: "probot" }), + Octokit: additionalOptions?.Octokit || undefined, }; const serverOptions: ServerOptions = {