@@ -46,17 +46,19 @@ export interface QlPack {
4646 */
4747const QUERY_PACK_NAME = 'codeql-remote/query' ;
4848
49+ export interface GeneratedQueryPack {
50+ base64Pack : string ,
51+ language : string
52+ }
53+
4954/**
5055 * Two possibilities:
5156 * 1. There is no qlpack.yml in this directory. Assume this is a lone query and generate a synthetic qlpack for it.
5257 * 2. There is a qlpack.yml in this directory. Assume this is a query pack and use the yml to pack the query before uploading it.
5358 *
5459 * @returns the entire qlpack as a base64 string.
5560 */
56- async function generateQueryPack ( cliServer : cli . CodeQLCliServer , queryFile : string , queryPackDir : string ) : Promise < {
57- base64Pack : string ,
58- language : string
59- } > {
61+ async function generateQueryPack ( cliServer : cli . CodeQLCliServer , queryFile : string , queryPackDir : string ) : Promise < GeneratedQueryPack > {
6062 const originalPackRoot = await findPackRoot ( queryFile ) ;
6163 const packRelativePath = path . relative ( originalPackRoot , queryFile ) ;
6264 const targetQueryFileName = path . join ( queryPackDir , packRelativePath ) ;
@@ -192,7 +194,6 @@ export async function prepareRemoteQueryRun(
192194 cliServer : cli . CodeQLCliServer ,
193195 credentials : Credentials ,
194196 uri : Uri | undefined ,
195- queryPackDir : string ,
196197 progress : ProgressCallback ,
197198 token : CancellationToken ,
198199) : Promise < PreparedRemoteQuery > {
@@ -236,7 +237,17 @@ export async function prepareRemoteQueryRun(
236237 throw new UserCancellationException ( 'Cancelled' ) ;
237238 }
238239
239- const { base64Pack, language } = await generateQueryPack ( cliServer , queryFile , queryPackDir ) ;
240+ const { remoteQueryDir, queryPackDir } = await createRemoteQueriesTempDirectory ( ) ;
241+
242+ let pack : GeneratedQueryPack ;
243+
244+ try {
245+ pack = await generateQueryPack ( cliServer , queryFile , queryPackDir ) ;
246+ } finally {
247+ await remoteQueryDir . cleanup ( ) ;
248+ }
249+
250+ const { base64Pack, language } = pack ;
240251
241252 if ( token . isCancellationRequested ) {
242253 throw new UserCancellationException ( 'Cancelled' ) ;
@@ -277,87 +288,81 @@ export async function runRemoteQuery(
277288 } or later.`) ;
278289 }
279290
280- const { remoteQueryDir, queryPackDir } = await createRemoteQueriesTempDirectory ( ) ;
281- try {
282- const {
283- actionBranch,
284- base64Pack,
285- repoSelection,
286- queryFile,
287- queryMetadata,
288- controllerRepo,
289- queryStartTime,
290- language,
291- } = await prepareRemoteQueryRun ( cliServer , credentials , uri , queryPackDir , progress , token ) ;
291+ const {
292+ actionBranch,
293+ base64Pack,
294+ repoSelection,
295+ queryFile,
296+ queryMetadata,
297+ controllerRepo,
298+ queryStartTime,
299+ language,
300+ } = await prepareRemoteQueryRun ( cliServer , credentials , uri , progress , token ) ;
292301
293- if ( isVariantAnalysisLiveResultsEnabled ( ) ) {
294- const queryName = getQueryName ( queryMetadata , queryFile ) ;
295- const variantAnalysisLanguage = parseVariantAnalysisQueryLanguage ( language ) ;
296- if ( variantAnalysisLanguage === undefined ) {
297- throw new UserCancellationException ( `Found unsupported language: ${ language } ` ) ;
298- }
302+ if ( isVariantAnalysisLiveResultsEnabled ( ) ) {
303+ const queryName = getQueryName ( queryMetadata , queryFile ) ;
304+ const variantAnalysisLanguage = parseVariantAnalysisQueryLanguage ( language ) ;
305+ if ( variantAnalysisLanguage === undefined ) {
306+ throw new UserCancellationException ( `Found unsupported language: ${ language } ` ) ;
307+ }
299308
300- const queryText = await fs . readFile ( queryFile , 'utf8' ) ;
301-
302- const variantAnalysisSubmission : VariantAnalysisSubmission = {
303- startTime : queryStartTime ,
304- actionRepoRef : actionBranch ,
305- controllerRepoId : controllerRepo . id ,
306- query : {
307- name : queryName ,
308- filePath : queryFile ,
309- pack : base64Pack ,
310- language : variantAnalysisLanguage ,
311- text : queryText ,
312- } ,
313- databases : {
314- repositories : repoSelection . repositories ,
315- repositoryLists : repoSelection . repositoryLists ,
316- repositoryOwners : repoSelection . owners
317- }
318- } ;
309+ const queryText = await fs . readFile ( queryFile , 'utf8' ) ;
319310
320- const variantAnalysisResponse = await ghApiClient . submitVariantAnalysis (
321- credentials ,
322- variantAnalysisSubmission
323- ) ;
311+ const variantAnalysisSubmission : VariantAnalysisSubmission = {
312+ startTime : queryStartTime ,
313+ actionRepoRef : actionBranch ,
314+ controllerRepoId : controllerRepo . id ,
315+ query : {
316+ name : queryName ,
317+ filePath : queryFile ,
318+ pack : base64Pack ,
319+ language : variantAnalysisLanguage ,
320+ text : queryText ,
321+ } ,
322+ databases : {
323+ repositories : repoSelection . repositories ,
324+ repositoryLists : repoSelection . repositoryLists ,
325+ repositoryOwners : repoSelection . owners
326+ }
327+ } ;
324328
325- const processedVariantAnalysis = processVariantAnalysis ( variantAnalysisSubmission , variantAnalysisResponse ) ;
329+ const variantAnalysisResponse = await ghApiClient . submitVariantAnalysis (
330+ credentials ,
331+ variantAnalysisSubmission
332+ ) ;
326333
327- await variantAnalysisManager . onVariantAnalysisSubmitted ( processedVariantAnalysis ) ;
334+ const processedVariantAnalysis = processVariantAnalysis ( variantAnalysisSubmission , variantAnalysisResponse ) ;
328335
329- void logger . log ( `Variant analysis:\n ${ JSON . stringify ( processedVariantAnalysis , null , 2 ) } ` ) ;
336+ await variantAnalysisManager . onVariantAnalysisSubmitted ( processedVariantAnalysis ) ;
330337
331- void showAndLogInformationMessage ( `Variant analysis ${ processedVariantAnalysis . query . name } submitted for processing ` ) ;
338+ void logger . log ( `Variant analysis:\n ${ JSON . stringify ( processedVariantAnalysis , null , 2 ) } ` ) ;
332339
333- void commands . executeCommand ( 'codeQL.openVariantAnalysisView' , processedVariantAnalysis . id ) ;
334- void commands . executeCommand ( 'codeQL.monitorVariantAnalysis' , processedVariantAnalysis ) ;
340+ void showAndLogInformationMessage ( `Variant analysis ${ processedVariantAnalysis . query . name } submitted for processing` ) ;
335341
336- return { variantAnalysis : processedVariantAnalysis } ;
337- } else {
338- const apiResponse = await runRemoteQueriesApiRequest ( credentials , actionBranch , language , repoSelection , controllerRepo , base64Pack ) ;
342+ void commands . executeCommand ( 'codeQL.openVariantAnalysisView' , processedVariantAnalysis . id ) ;
343+ void commands . executeCommand ( 'codeQL.monitorVariantAnalysis' , processedVariantAnalysis ) ;
339344
340- if ( ! apiResponse ) {
341- return ;
342- }
345+ return { variantAnalysis : processedVariantAnalysis } ;
346+ } else {
347+ const apiResponse = await runRemoteQueriesApiRequest ( credentials , actionBranch , language , repoSelection , controllerRepo , base64Pack ) ;
343348
344- const workflowRunId = apiResponse . workflow_run_id ;
345- const repositoryCount = apiResponse . repositories_queried . length ;
346- const remoteQuery = await buildRemoteQueryEntity (
347- queryFile ,
348- queryMetadata ,
349- controllerRepo ,
350- queryStartTime ,
351- workflowRunId ,
352- language ,
353- repositoryCount ) ;
354-
355- // don't return the path because it has been deleted
356- return { query : remoteQuery } ;
349+ if ( ! apiResponse ) {
350+ return ;
357351 }
358352
359- } finally {
360- await remoteQueryDir . cleanup ( ) ;
353+ const workflowRunId = apiResponse . workflow_run_id ;
354+ const repositoryCount = apiResponse . repositories_queried . length ;
355+ const remoteQuery = await buildRemoteQueryEntity (
356+ queryFile ,
357+ queryMetadata ,
358+ controllerRepo ,
359+ queryStartTime ,
360+ workflowRunId ,
361+ language ,
362+ repositoryCount ) ;
363+
364+ // don't return the path because it has been deleted
365+ return { query : remoteQuery } ;
361366 }
362367}
363368
0 commit comments