|
| 1 | +import { GitBookAPI } from '@gitbook/api'; |
| 2 | +import { Logger } from '@gitbook/runtime'; |
| 3 | + |
| 4 | +import { querySpaceInstallations } from './installation'; |
| 5 | +import { triggerImport } from './sync'; |
| 6 | +import type { GithubRuntimeContext, IntegrationTask, IntegrationTaskImportSpaces } from './types'; |
| 7 | + |
| 8 | +const logger = Logger('github:tasks'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Queue a task for the integration to import spaces. |
| 12 | + */ |
| 13 | +export async function queueTaskForImportSpaces( |
| 14 | + context: GithubRuntimeContext, |
| 15 | + task: IntegrationTaskImportSpaces |
| 16 | +): Promise<void> { |
| 17 | + const { api, environment } = context; |
| 18 | + await api.integrations.queueIntegrationTask(environment.integration.name, { |
| 19 | + task: { |
| 20 | + type: task.type, |
| 21 | + payload: task.payload, |
| 22 | + }, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Handle an integration task. |
| 28 | + */ |
| 29 | +export async function handleIntegrationTask( |
| 30 | + context: GithubRuntimeContext, |
| 31 | + task: IntegrationTask |
| 32 | +): Promise<void> { |
| 33 | + switch (task.type) { |
| 34 | + case 'import:spaces': |
| 35 | + await handleImportDispatchForSpaces(context, task.payload); |
| 36 | + break; |
| 37 | + default: |
| 38 | + throw new Error(`Unknown integration task type: ${task}`); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * This function is used to trigger an import for all the spaces that match the given config query. |
| 44 | + * It will handle pagination by queueing itself if there are more spaces to import. |
| 45 | + * |
| 46 | + * `NOTE`: It is important that the total number of external network calls in this function is less |
| 47 | + * than 50 as that is the limit imposed by Cloudflare workers. |
| 48 | + */ |
| 49 | +export async function handleImportDispatchForSpaces( |
| 50 | + context: GithubRuntimeContext, |
| 51 | + payload: IntegrationTaskImportSpaces['payload'] |
| 52 | +): Promise<number | undefined> { |
| 53 | + const { configQuery, page, standaloneRef } = payload; |
| 54 | + |
| 55 | + logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`); |
| 56 | + |
| 57 | + const { |
| 58 | + data: spaceInstallations, |
| 59 | + nextPage, |
| 60 | + total, |
| 61 | + } = await querySpaceInstallations(context, configQuery, { |
| 62 | + limit: 10, |
| 63 | + page, |
| 64 | + }); |
| 65 | + |
| 66 | + await Promise.allSettled( |
| 67 | + spaceInstallations.map(async (spaceInstallation) => { |
| 68 | + try { |
| 69 | + // Obtain the installation API token needed to trigger the import |
| 70 | + const { data: installationAPIToken } = |
| 71 | + await context.api.integrations.createIntegrationInstallationToken( |
| 72 | + spaceInstallation.integration, |
| 73 | + spaceInstallation.installation |
| 74 | + ); |
| 75 | + |
| 76 | + // Set the token in the duplicated context to be used by the API client |
| 77 | + const installationContext: GithubRuntimeContext = { |
| 78 | + ...context, |
| 79 | + api: new GitBookAPI({ |
| 80 | + endpoint: context.environment.apiEndpoint, |
| 81 | + authToken: installationAPIToken.token, |
| 82 | + }), |
| 83 | + environment: { |
| 84 | + ...context.environment, |
| 85 | + authToken: installationAPIToken.token, |
| 86 | + }, |
| 87 | + }; |
| 88 | + |
| 89 | + await triggerImport(installationContext, spaceInstallation, { |
| 90 | + standalone: standaloneRef |
| 91 | + ? { |
| 92 | + ref: standaloneRef, |
| 93 | + } |
| 94 | + : undefined, |
| 95 | + }); |
| 96 | + } catch (error) { |
| 97 | + logger.error( |
| 98 | + `error while triggering ${ |
| 99 | + standaloneRef ? `standalone (${standaloneRef})` : '' |
| 100 | + } import for space ${spaceInstallation.space}`, |
| 101 | + error |
| 102 | + ); |
| 103 | + } |
| 104 | + }) |
| 105 | + ); |
| 106 | + |
| 107 | + // Queue the next page if there is one |
| 108 | + if (nextPage) { |
| 109 | + logger.debug(`queueing next page ${nextPage} of import dispatch for spaces`); |
| 110 | + await queueTaskForImportSpaces(context, { |
| 111 | + type: 'import:spaces', |
| 112 | + payload: { |
| 113 | + page: nextPage, |
| 114 | + configQuery, |
| 115 | + standaloneRef, |
| 116 | + }, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + return total; |
| 121 | +} |
0 commit comments