Skip to content

Commit a1555f2

Browse files
committed
refactor
1 parent 4b212ef commit a1555f2

File tree

2 files changed

+90
-87
lines changed

2 files changed

+90
-87
lines changed

integrations/github/src/tasks.ts

+88-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { GitBookAPI } from '@gitbook/api';
2+
import { Logger } from '@gitbook/runtime';
3+
4+
import { querySpaceInstallations } from './installation';
5+
import { triggerImport } from './sync';
16
import type { GithubRuntimeContext, IntegrationTask, IntegrationTaskImportSpaces } from './types';
2-
import { handleImportDispatchForSpaces } from './webhooks';
7+
8+
const logger = Logger('github:tasks');
39

410
/**
511
* Queue a task for the integration to import spaces.
@@ -32,3 +38,84 @@ export async function handleIntegrationTask(
3238
throw new Error(`Unknown integration task type: ${task}`);
3339
}
3440
}
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+
}

integrations/github/src/webhooks.ts

+2-86
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import type {
55
} from '@octokit/webhooks-types';
66
import httpError from 'http-errors';
77

8-
import { GitBookAPI } from '@gitbook/api';
98
import { Logger } from '@gitbook/runtime';
109

11-
import { querySpaceInstallations } from './installation';
12-
import { triggerImport } from './sync';
13-
import { queueTaskForImportSpaces } from './tasks';
14-
import { GithubRuntimeContext, IntegrationTaskImportSpaces } from './types';
10+
import { handleImportDispatchForSpaces } from './tasks';
11+
import { GithubRuntimeContext } from './types';
1512
import { arrayToHex, computeConfigQueryKey, safeCompare } from './utils';
1613

1714
const logger = Logger('github:webhooks');
@@ -110,84 +107,3 @@ export async function handlePullRequestEvents(
110107
logger.debug(`${total} space configurations are affected`);
111108
}
112109
}
113-
114-
/**
115-
* This function is used to trigger an import for all the spaces that match the given config query.
116-
* It will handle pagination by queueing itself if there are more spaces to import.
117-
*
118-
* `NOTE`: It is important that the total number of external network calls in this function is less
119-
* than 50 as that is the limit imposed by Cloudflare workers.
120-
*/
121-
export async function handleImportDispatchForSpaces(
122-
context: GithubRuntimeContext,
123-
payload: IntegrationTaskImportSpaces['payload']
124-
): Promise<number | undefined> {
125-
const { configQuery, page, standaloneRef } = payload;
126-
127-
logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`);
128-
129-
const {
130-
data: spaceInstallations,
131-
nextPage,
132-
total,
133-
} = await querySpaceInstallations(context, configQuery, {
134-
limit: 10,
135-
page,
136-
});
137-
138-
await Promise.allSettled(
139-
spaceInstallations.map(async (spaceInstallation) => {
140-
try {
141-
// Obtain the installation API token needed to trigger the import
142-
const { data: installationAPIToken } =
143-
await context.api.integrations.createIntegrationInstallationToken(
144-
spaceInstallation.integration,
145-
spaceInstallation.installation
146-
);
147-
148-
// Set the token in the duplicated context to be used by the API client
149-
const installationContext: GithubRuntimeContext = {
150-
...context,
151-
api: new GitBookAPI({
152-
endpoint: context.environment.apiEndpoint,
153-
authToken: installationAPIToken.token,
154-
}),
155-
environment: {
156-
...context.environment,
157-
authToken: installationAPIToken.token,
158-
},
159-
};
160-
161-
await triggerImport(installationContext, spaceInstallation, {
162-
standalone: standaloneRef
163-
? {
164-
ref: standaloneRef,
165-
}
166-
: undefined,
167-
});
168-
} catch (error) {
169-
logger.error(
170-
`error while triggering ${
171-
standaloneRef ? `standalone (${standaloneRef})` : ''
172-
} import for space ${spaceInstallation.space}`,
173-
error
174-
);
175-
}
176-
})
177-
);
178-
179-
// Queue the next page if there is one
180-
if (nextPage) {
181-
logger.debug(`queueing next page ${nextPage} of import dispatch for spaces`);
182-
await queueTaskForImportSpaces(context, {
183-
type: 'import:spaces',
184-
payload: {
185-
page: nextPage,
186-
configQuery,
187-
standaloneRef,
188-
},
189-
});
190-
}
191-
192-
return total;
193-
}

0 commit comments

Comments
 (0)