@@ -85,18 +85,6 @@ export async function cleanupAndUploadDatabases(
8585 await codeql . databaseCleanupCluster ( config , cleanupLevel ) ;
8686 } ) ;
8787
88- const client = getApiClient ( ) ;
89-
90- const uploadsUrl = new URL ( parseGitHubUrl ( apiDetails . url ) ) ;
91- uploadsUrl . hostname = `uploads.${ uploadsUrl . hostname } ` ;
92-
93- // Octokit expects the baseUrl to not have a trailing slash,
94- // but it is included by default in a URL.
95- let uploadsBaseUrl = uploadsUrl . toString ( ) ;
96- if ( uploadsBaseUrl . endsWith ( "/" ) ) {
97- uploadsBaseUrl = uploadsBaseUrl . slice ( 0 , - 1 ) ;
98- }
99-
10088 const reports : DatabaseUploadResult [ ] = [ ] ;
10189 for ( const language of config . languages ) {
10290 let bundledDbSize : number | undefined = undefined ;
@@ -118,30 +106,15 @@ export async function cleanupAndUploadDatabases(
118106 const maxAttempts = 4 ; // 1 initial attempt + 3 retries, identical to the default retry behavior of Octokit
119107 let uploadDurationMs : number | undefined ;
120108 for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
121- const bundledDbReadStream = fs . createReadStream ( bundledDb ) ;
122109 try {
123- const attemptStartTime = performance . now ( ) ;
124- await client . request (
125- `POST /repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid` ,
126- {
127- baseUrl : uploadsBaseUrl ,
128- owner : repositoryNwo . owner ,
129- repo : repositoryNwo . repo ,
130- language,
131- name : `${ language } -database` ,
132- commit_oid : commitOid ,
133- data : bundledDbReadStream ,
134- headers : {
135- authorization : `token ${ apiDetails . auth } ` ,
136- "Content-Type" : "application/zip" ,
137- "Content-Length" : bundledDbSize ,
138- } ,
139- request : {
140- retries : 0 ,
141- } ,
142- } ,
110+ uploadDurationMs = await uploadBundledDatabase (
111+ repositoryNwo ,
112+ language ,
113+ commitOid ,
114+ bundledDb ,
115+ bundledDbSize ,
116+ apiDetails ,
143117 ) ;
144- uploadDurationMs = performance . now ( ) - attemptStartTime ;
145118 break ;
146119 } catch ( e ) {
147120 const httpError = asHTTPError ( e ) ;
@@ -160,8 +133,6 @@ export async function cleanupAndUploadDatabases(
160133 `Database upload attempt ${ attempt } of ${ maxAttempts } failed for ${ language } : ${ util . getErrorMessage ( e ) } . Retrying in ${ backoffMs / 1000 } s...` ,
161134 ) ;
162135 await new Promise ( ( resolve ) => setTimeout ( resolve , backoffMs ) ) ;
163- } finally {
164- bundledDbReadStream . close ( ) ;
165136 }
166137 }
167138 reports . push ( {
@@ -187,3 +158,58 @@ export async function cleanupAndUploadDatabases(
187158 }
188159 return reports ;
189160}
161+
162+ /**
163+ * Uploads a bundled database to the GitHub API.
164+ *
165+ * @returns the duration of the upload in milliseconds
166+ */
167+ async function uploadBundledDatabase (
168+ repositoryNwo : RepositoryNwo ,
169+ language : string ,
170+ commitOid : string ,
171+ bundledDb : string ,
172+ bundledDbSize : number ,
173+ apiDetails : GitHubApiDetails ,
174+ ) : Promise < number > {
175+ const client = getApiClient ( ) ;
176+
177+ const uploadsUrl = new URL ( parseGitHubUrl ( apiDetails . url ) ) ;
178+ uploadsUrl . hostname = `uploads.${ uploadsUrl . hostname } ` ;
179+
180+ // Octokit expects the baseUrl to not have a trailing slash,
181+ // but it is included by default in a URL.
182+ let uploadsBaseUrl = uploadsUrl . toString ( ) ;
183+ if ( uploadsBaseUrl . endsWith ( "/" ) ) {
184+ uploadsBaseUrl = uploadsBaseUrl . slice ( 0 , - 1 ) ;
185+ }
186+
187+ const bundledDbReadStream = fs . createReadStream ( bundledDb ) ;
188+ try {
189+ const startTime = performance . now ( ) ;
190+ await client . request (
191+ `POST /repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid` ,
192+ {
193+ baseUrl : uploadsBaseUrl ,
194+ owner : repositoryNwo . owner ,
195+ repo : repositoryNwo . repo ,
196+ language,
197+ name : `${ language } -database` ,
198+ commit_oid : commitOid ,
199+ data : bundledDbReadStream ,
200+ headers : {
201+ authorization : `token ${ apiDetails . auth } ` ,
202+ "Content-Type" : "application/zip" ,
203+ "Content-Length" : bundledDbSize ,
204+ } ,
205+ // Disable `octokit/plugin-retry.js`, since the request body is a ReadStream which can only be consumed once.
206+ request : {
207+ retries : 0 ,
208+ } ,
209+ } ,
210+ ) ;
211+ return performance . now ( ) - startTime ;
212+ } finally {
213+ bundledDbReadStream . close ( ) ;
214+ }
215+ }
0 commit comments