From a1555f2711562cd4a2f33c386be4e097b73bfae6 Mon Sep 17 00:00:00 2001 From: taranvohra Date: Wed, 3 Jan 2024 20:56:23 +0530 Subject: [PATCH] refactor --- integrations/github/src/tasks.ts | 89 ++++++++++++++++++++++++++++- integrations/github/src/webhooks.ts | 88 +--------------------------- 2 files changed, 90 insertions(+), 87 deletions(-) diff --git a/integrations/github/src/tasks.ts b/integrations/github/src/tasks.ts index 0a88a56f5..6001d72ee 100644 --- a/integrations/github/src/tasks.ts +++ b/integrations/github/src/tasks.ts @@ -1,5 +1,11 @@ +import { GitBookAPI } from '@gitbook/api'; +import { Logger } from '@gitbook/runtime'; + +import { querySpaceInstallations } from './installation'; +import { triggerImport } from './sync'; import type { GithubRuntimeContext, IntegrationTask, IntegrationTaskImportSpaces } from './types'; -import { handleImportDispatchForSpaces } from './webhooks'; + +const logger = Logger('github:tasks'); /** * Queue a task for the integration to import spaces. @@ -32,3 +38,84 @@ export async function handleIntegrationTask( throw new Error(`Unknown integration task type: ${task}`); } } + +/** + * This function is used to trigger an import for all the spaces that match the given config query. + * It will handle pagination by queueing itself if there are more spaces to import. + * + * `NOTE`: It is important that the total number of external network calls in this function is less + * than 50 as that is the limit imposed by Cloudflare workers. + */ +export async function handleImportDispatchForSpaces( + context: GithubRuntimeContext, + payload: IntegrationTaskImportSpaces['payload'] +): Promise { + const { configQuery, page, standaloneRef } = payload; + + logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`); + + const { + data: spaceInstallations, + nextPage, + total, + } = await querySpaceInstallations(context, configQuery, { + limit: 10, + page, + }); + + await Promise.allSettled( + spaceInstallations.map(async (spaceInstallation) => { + try { + // Obtain the installation API token needed to trigger the import + const { data: installationAPIToken } = + await context.api.integrations.createIntegrationInstallationToken( + spaceInstallation.integration, + spaceInstallation.installation + ); + + // Set the token in the duplicated context to be used by the API client + const installationContext: GithubRuntimeContext = { + ...context, + api: new GitBookAPI({ + endpoint: context.environment.apiEndpoint, + authToken: installationAPIToken.token, + }), + environment: { + ...context.environment, + authToken: installationAPIToken.token, + }, + }; + + await triggerImport(installationContext, spaceInstallation, { + standalone: standaloneRef + ? { + ref: standaloneRef, + } + : undefined, + }); + } catch (error) { + logger.error( + `error while triggering ${ + standaloneRef ? `standalone (${standaloneRef})` : '' + } import for space ${spaceInstallation.space}`, + error + ); + } + }) + ); + + // Queue the next page if there is one + if (nextPage) { + logger.debug(`queueing next page ${nextPage} of import dispatch for spaces`); + await queueTaskForImportSpaces(context, { + type: 'import:spaces', + payload: { + page: nextPage, + configQuery, + standaloneRef, + }, + }); + } + + return total; +} diff --git a/integrations/github/src/webhooks.ts b/integrations/github/src/webhooks.ts index 2c21badee..9a8a82d1f 100644 --- a/integrations/github/src/webhooks.ts +++ b/integrations/github/src/webhooks.ts @@ -5,13 +5,10 @@ import type { } from '@octokit/webhooks-types'; import httpError from 'http-errors'; -import { GitBookAPI } from '@gitbook/api'; import { Logger } from '@gitbook/runtime'; -import { querySpaceInstallations } from './installation'; -import { triggerImport } from './sync'; -import { queueTaskForImportSpaces } from './tasks'; -import { GithubRuntimeContext, IntegrationTaskImportSpaces } from './types'; +import { handleImportDispatchForSpaces } from './tasks'; +import { GithubRuntimeContext } from './types'; import { arrayToHex, computeConfigQueryKey, safeCompare } from './utils'; const logger = Logger('github:webhooks'); @@ -110,84 +107,3 @@ export async function handlePullRequestEvents( logger.debug(`${total} space configurations are affected`); } } - -/** - * This function is used to trigger an import for all the spaces that match the given config query. - * It will handle pagination by queueing itself if there are more spaces to import. - * - * `NOTE`: It is important that the total number of external network calls in this function is less - * than 50 as that is the limit imposed by Cloudflare workers. - */ -export async function handleImportDispatchForSpaces( - context: GithubRuntimeContext, - payload: IntegrationTaskImportSpaces['payload'] -): Promise { - const { configQuery, page, standaloneRef } = payload; - - logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`); - - const { - data: spaceInstallations, - nextPage, - total, - } = await querySpaceInstallations(context, configQuery, { - limit: 10, - page, - }); - - await Promise.allSettled( - spaceInstallations.map(async (spaceInstallation) => { - try { - // Obtain the installation API token needed to trigger the import - const { data: installationAPIToken } = - await context.api.integrations.createIntegrationInstallationToken( - spaceInstallation.integration, - spaceInstallation.installation - ); - - // Set the token in the duplicated context to be used by the API client - const installationContext: GithubRuntimeContext = { - ...context, - api: new GitBookAPI({ - endpoint: context.environment.apiEndpoint, - authToken: installationAPIToken.token, - }), - environment: { - ...context.environment, - authToken: installationAPIToken.token, - }, - }; - - await triggerImport(installationContext, spaceInstallation, { - standalone: standaloneRef - ? { - ref: standaloneRef, - } - : undefined, - }); - } catch (error) { - logger.error( - `error while triggering ${ - standaloneRef ? `standalone (${standaloneRef})` : '' - } import for space ${spaceInstallation.space}`, - error - ); - } - }) - ); - - // Queue the next page if there is one - if (nextPage) { - logger.debug(`queueing next page ${nextPage} of import dispatch for spaces`); - await queueTaskForImportSpaces(context, { - type: 'import:spaces', - payload: { - page: nextPage, - configQuery, - standaloneRef, - }, - }); - } - - return total; -}