UNPKG

probot

Version:

A framework for building GitHub Apps to automate and improve your workflow

70 lines (69 loc) • 2.83 kB
import { request } from "@octokit/request"; import { PROBOT_OCTOKIT_DEFAULTS, ProbotOctokit } from "./probot-octokit.js"; import { getOctokitThrottleOptions } from "./get-octokit-throttle-options.js"; export function mergeThrottleOptions(runtimeOptions, explicitOptions) { return { ...runtimeOptions, ...explicitOptions }; } /** * Returns an Octokit instance with default settings for authentication. If * a `githubToken` is passed explicitly, the Octokit instance will be * pre-authenticated with that token when instantiated. Otherwise Octokit's * app authentication strategy is used, and `options.auth` options are merged * deeply when instantiated. * * Besides the authentication, the Octokit's baseUrl is set as well when run * against a GitHub Enterprise Server with a custom domain. */ export async function getProbotOctokitWithDefaults(options) { const authRequest = request.defaults({ ...(options.baseUrl ? { baseUrl: options.baseUrl } : {}), request: { fetch: options.request?.fetch, }, }); const authOptions = options.githubToken ? { token: options.githubToken, request: authRequest, } : { cache: options.cache, appId: options.appId, privateKey: options.privateKey, request: authRequest, }; const log = options.log.child({ name: "octokit" }); const octokitThrottleOptions = await getOctokitThrottleOptions({ log, redisConfig: options.redisConfig, }); const defaultOptions = { log, auth: authOptions, }; if (options.request) { defaultOptions.request = options.request; } if (options.baseUrl) { defaultOptions.baseUrl = options.baseUrl; } // ProbotOctokit already provides the built-in throttling handlers. Only add // the resolved options here when Redis-backed throttling was requested; // otherwise this extra defaults layer would overwrite explicit options from // `ProbotOctokit.defaults(...)` (for example, `{ enabled: false }` in tests). if (options.redisConfig) { const explicitDefaults = options.Octokit[PROBOT_OCTOKIT_DEFAULTS]; defaultOptions.throttle = mergeThrottleOptions(octokitThrottleOptions, explicitDefaults?.throttle); } return options.Octokit.defaults((instanceOptions) => { const options = Object.assign({}, defaultOptions, instanceOptions, { auth: instanceOptions.auth ? Object.assign({}, defaultOptions.auth, instanceOptions.auth) : defaultOptions.auth, }); if (instanceOptions.throttle) { options.throttle = Object.assign({}, defaultOptions.throttle, instanceOptions.throttle); } return options; }); }