@@ -5,13 +5,10 @@ import type {
5
5
} from '@octokit/webhooks-types' ;
6
6
import httpError from 'http-errors' ;
7
7
8
- import { GitBookAPI } from '@gitbook/api' ;
9
8
import { Logger } from '@gitbook/runtime' ;
10
9
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' ;
15
12
import { arrayToHex , computeConfigQueryKey , safeCompare } from './utils' ;
16
13
17
14
const logger = Logger ( 'github:webhooks' ) ;
@@ -110,84 +107,3 @@ export async function handlePullRequestEvents(
110
107
logger . debug ( `${ total } space configurations are affected` ) ;
111
108
}
112
109
}
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