Skip to content

Commit 082fea8

Browse files
committed
Reduce nesting in runRemoteQuery
Now that we do not have a dry run mode, we can create and clean up the temporary directory in the same function. This allows us to remove the complete try..finally block inside `runRemoteQuery` and move it to a much more local spot.
1 parent cca13fd commit 082fea8

File tree

1 file changed

+73
-70
lines changed

1 file changed

+73
-70
lines changed

extensions/ql-vscode/src/remote-queries/run-remote-query.ts

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export async function prepareRemoteQueryRun(
181181
cliServer: cli.CodeQLCliServer,
182182
credentials: Credentials,
183183
uri: Uri | undefined,
184-
queryPackDir: string,
185184
progress: ProgressCallback,
186185
token: CancellationToken,
187186
) {
@@ -225,7 +224,17 @@ export async function prepareRemoteQueryRun(
225224
throw new UserCancellationException('Cancelled');
226225
}
227226

228-
const { base64Pack, language } = await generateQueryPack(cliServer, queryFile, queryPackDir);
227+
const { remoteQueryDir, queryPackDir } = await createRemoteQueriesTempDirectory();
228+
229+
let pack: Awaited<ReturnType<typeof generateQueryPack>>;
230+
231+
try {
232+
pack = await generateQueryPack(cliServer, queryFile, queryPackDir);
233+
} finally {
234+
await remoteQueryDir.cleanup();
235+
}
236+
237+
const { base64Pack, language } = pack;
229238

230239
if (token.isCancellationRequested) {
231240
throw new UserCancellationException('Cancelled');
@@ -266,87 +275,81 @@ export async function runRemoteQuery(
266275
} or later.`);
267276
}
268277

269-
const { remoteQueryDir, queryPackDir } = await createRemoteQueriesTempDirectory();
270-
try {
271-
const {
272-
actionBranch,
273-
base64Pack,
274-
repoSelection,
275-
queryFile,
276-
queryMetadata,
277-
controllerRepo,
278-
queryStartTime,
279-
language,
280-
} = await prepareRemoteQueryRun(cliServer, credentials, uri, queryPackDir, progress, token);
278+
const {
279+
actionBranch,
280+
base64Pack,
281+
repoSelection,
282+
queryFile,
283+
queryMetadata,
284+
controllerRepo,
285+
queryStartTime,
286+
language,
287+
} = await prepareRemoteQueryRun(cliServer, credentials, uri, progress, token);
281288

282-
if (isVariantAnalysisLiveResultsEnabled()) {
283-
const queryName = getQueryName(queryMetadata, queryFile);
284-
const variantAnalysisLanguage = parseVariantAnalysisQueryLanguage(language);
285-
if (variantAnalysisLanguage === undefined) {
286-
throw new UserCancellationException(`Found unsupported language: ${language}`);
287-
}
289+
if (isVariantAnalysisLiveResultsEnabled()) {
290+
const queryName = getQueryName(queryMetadata, queryFile);
291+
const variantAnalysisLanguage = parseVariantAnalysisQueryLanguage(language);
292+
if (variantAnalysisLanguage === undefined) {
293+
throw new UserCancellationException(`Found unsupported language: ${language}`);
294+
}
288295

289-
const queryText = await fs.readFile(queryFile, 'utf8');
290-
291-
const variantAnalysisSubmission: VariantAnalysisSubmission = {
292-
startTime: queryStartTime,
293-
actionRepoRef: actionBranch,
294-
controllerRepoId: controllerRepo.id,
295-
query: {
296-
name: queryName,
297-
filePath: queryFile,
298-
pack: base64Pack,
299-
language: variantAnalysisLanguage,
300-
text: queryText,
301-
},
302-
databases: {
303-
repositories: repoSelection.repositories,
304-
repositoryLists: repoSelection.repositoryLists,
305-
repositoryOwners: repoSelection.owners
306-
}
307-
};
296+
const queryText = await fs.readFile(queryFile, 'utf8');
308297

309-
const variantAnalysisResponse = await ghApiClient.submitVariantAnalysis(
310-
credentials,
311-
variantAnalysisSubmission
312-
);
298+
const variantAnalysisSubmission: VariantAnalysisSubmission = {
299+
startTime: queryStartTime,
300+
actionRepoRef: actionBranch,
301+
controllerRepoId: controllerRepo.id,
302+
query: {
303+
name: queryName,
304+
filePath: queryFile,
305+
pack: base64Pack,
306+
language: variantAnalysisLanguage,
307+
text: queryText,
308+
},
309+
databases: {
310+
repositories: repoSelection.repositories,
311+
repositoryLists: repoSelection.repositoryLists,
312+
repositoryOwners: repoSelection.owners
313+
}
314+
};
313315

314-
const processedVariantAnalysis = processVariantAnalysis(variantAnalysisSubmission, variantAnalysisResponse);
316+
const variantAnalysisResponse = await ghApiClient.submitVariantAnalysis(
317+
credentials,
318+
variantAnalysisSubmission
319+
);
315320

316-
await variantAnalysisManager.onVariantAnalysisSubmitted(processedVariantAnalysis);
321+
const processedVariantAnalysis = processVariantAnalysis(variantAnalysisSubmission, variantAnalysisResponse);
317322

318-
void logger.log(`Variant analysis:\n${JSON.stringify(processedVariantAnalysis, null, 2)}`);
323+
await variantAnalysisManager.onVariantAnalysisSubmitted(processedVariantAnalysis);
319324

320-
void showAndLogInformationMessage(`Variant analysis ${processedVariantAnalysis.query.name} submitted for processing`);
325+
void logger.log(`Variant analysis:\n${JSON.stringify(processedVariantAnalysis, null, 2)}`);
321326

322-
void commands.executeCommand('codeQL.openVariantAnalysisView', processedVariantAnalysis.id);
323-
void commands.executeCommand('codeQL.monitorVariantAnalysis', processedVariantAnalysis);
327+
void showAndLogInformationMessage(`Variant analysis ${processedVariantAnalysis.query.name} submitted for processing`);
324328

325-
return { variantAnalysis: processedVariantAnalysis };
326-
} else {
327-
const apiResponse = await runRemoteQueriesApiRequest(credentials, actionBranch, language, repoSelection, controllerRepo, base64Pack);
329+
void commands.executeCommand('codeQL.openVariantAnalysisView', processedVariantAnalysis.id);
330+
void commands.executeCommand('codeQL.monitorVariantAnalysis', processedVariantAnalysis);
328331

329-
if (!apiResponse) {
330-
return;
331-
}
332+
return { variantAnalysis: processedVariantAnalysis };
333+
} else {
334+
const apiResponse = await runRemoteQueriesApiRequest(credentials, actionBranch, language, repoSelection, controllerRepo, base64Pack);
332335

333-
const workflowRunId = apiResponse.workflow_run_id;
334-
const repositoryCount = apiResponse.repositories_queried.length;
335-
const remoteQuery = await buildRemoteQueryEntity(
336-
queryFile,
337-
queryMetadata,
338-
controllerRepo,
339-
queryStartTime,
340-
workflowRunId,
341-
language,
342-
repositoryCount);
343-
344-
// don't return the path because it has been deleted
345-
return { query: remoteQuery };
336+
if (!apiResponse) {
337+
return;
346338
}
347339

348-
} finally {
349-
await remoteQueryDir.cleanup();
340+
const workflowRunId = apiResponse.workflow_run_id;
341+
const repositoryCount = apiResponse.repositories_queried.length;
342+
const remoteQuery = await buildRemoteQueryEntity(
343+
queryFile,
344+
queryMetadata,
345+
controllerRepo,
346+
queryStartTime,
347+
workflowRunId,
348+
language,
349+
repositoryCount);
350+
351+
// don't return the path because it has been deleted
352+
return { query: remoteQuery };
350353
}
351354
}
352355

0 commit comments

Comments
 (0)