@@ -18,13 +18,14 @@ import { join, dirname, parse } from "path";
18
18
import { tryGetQueryMetadata } from "../codeql-cli/query-metadata" ;
19
19
import { window as Window } from "vscode" ;
20
20
import { pluralize } from "../common/word" ;
21
- import { glob } from "glob" ;
22
21
import { readRepoTask } from "./repo-tasks-store" ;
23
22
import { unlink , mkdtemp , readFile , writeFile } from "fs/promises" ;
24
23
import { tmpdir } from "os" ;
25
24
import { spawn } from "child_process" ;
26
25
import type { execFileSync } from "child_process" ;
27
26
import { tryOpenExternalFile } from "../common/vscode/external-files" ;
27
+ import type { VariantAnalysisManager } from "./variant-analysis-manager" ;
28
+ import type { VariantAnalysisResultsManager } from "./variant-analysis-results-manager" ;
28
29
29
30
// Limit to three repos when generating autofixes so not sending
30
31
// too many requests to autofix. Since we only need to validate
@@ -38,12 +39,12 @@ const MAX_NUM_FIXES: number = 3;
38
39
* Generates autofixes for the results of a variant analysis.
39
40
*/
40
41
export async function viewAutofixesForVariantAnalysisResults (
42
+ variantAnalysisManager : VariantAnalysisManager ,
43
+ variantAnalysisResultsManager : VariantAnalysisResultsManager ,
41
44
variantAnalysisId : number ,
42
45
filterSort : RepositoriesFilterSortStateWithIds = defaultFilterSortState ,
43
- variantAnalyses : Map < number , VariantAnalysis > ,
44
46
credentials : Credentials ,
45
47
logger : NotificationLogger ,
46
- storagePath : string ,
47
48
app : App ,
48
49
cliServer : CodeQLCliServer ,
49
50
) : Promise < void > {
@@ -53,7 +54,8 @@ export async function viewAutofixesForVariantAnalysisResults(
53
54
const localAutofixPath = await findLocalAutofix ( ) ;
54
55
55
56
// Get the variant analysis with the given id.
56
- const variantAnalysis = variantAnalyses . get ( variantAnalysisId ) ;
57
+ const variantAnalysis =
58
+ variantAnalysisManager . tryGetVariantAnalysis ( variantAnalysisId ) ;
57
59
if ( ! variantAnalysis ) {
58
60
throw new Error ( `No variant analysis with id: ${ variantAnalysisId } ` ) ;
59
61
}
@@ -72,7 +74,7 @@ export async function viewAutofixesForVariantAnalysisResults(
72
74
variantAnalysisIdStoragePath,
73
75
sourceRootsStoragePath,
74
76
autofixOutputStoragePath,
75
- } = await getStoragePaths ( variantAnalysisId , storagePath ) ;
77
+ } = await getStoragePaths ( variantAnalysisManager , variantAnalysisId ) ;
76
78
77
79
// Process the selected repositories:
78
80
// Get sarif
@@ -86,6 +88,7 @@ export async function viewAutofixesForVariantAnalysisResults(
86
88
) ,
87
89
) ;
88
90
const outputTextFiles = await processSelectedRepositories (
91
+ variantAnalysisResultsManager ,
89
92
selectedRepoNames ,
90
93
variantAnalysisIdStoragePath ,
91
94
sourceRootsStoragePath ,
@@ -237,21 +240,19 @@ function getSelectedRepositoryNames(
237
240
* Gets the storage paths needed for the autofix results.
238
241
*/
239
242
async function getStoragePaths (
243
+ variantAnalysisManager : VariantAnalysisManager ,
240
244
variantAnalysisId : number ,
241
- storagePath : string ,
242
245
) : Promise < {
243
246
variantAnalysisIdStoragePath : string ;
244
247
sourceRootsStoragePath : string ;
245
248
autofixOutputStoragePath : string ;
246
249
} > {
247
250
// Confirm storage path for the variant analysis ID exists.
248
- const variantAnalysisIdStoragePath = join (
249
- storagePath ,
250
- variantAnalysisId . toString ( ) ,
251
- ) ;
251
+ const variantAnalysisIdStoragePath =
252
+ variantAnalysisManager . getVariantAnalysisStorageLocation ( variantAnalysisId ) ;
252
253
if ( ! ( await pathExists ( variantAnalysisIdStoragePath ) ) ) {
253
254
throw new Error (
254
- `Variant analysis storage path does not exist: ${ variantAnalysisIdStoragePath } ` ,
255
+ `Variant analysis storage location does not exist: ${ variantAnalysisIdStoragePath } ` ,
255
256
) ;
256
257
}
257
258
@@ -286,6 +287,7 @@ async function getStoragePaths(
286
287
* Processes the selected repositories for autofix generation.
287
288
*/
288
289
async function processSelectedRepositories (
290
+ variantAnalysisResultsManager : VariantAnalysisResultsManager ,
289
291
selectedRepoNames : string [ ] ,
290
292
variantAnalysisIdStoragePath : string ,
291
293
sourceRootsStoragePath : string ,
@@ -301,13 +303,20 @@ async function processSelectedRepositories(
301
303
async ( progressForRepo : ProgressCallback ) => {
302
304
// Get the sarif file.
303
305
progressForRepo ( progressUpdate ( 1 , 3 , `Getting sarif` ) ) ;
304
- const repoStoragePath = join ( variantAnalysisIdStoragePath , nwo ) ;
305
- const sarifFile = await getSarifFile ( repoStoragePath , nwo ) ;
306
+ const sarifFile = await getRepoSarifFile (
307
+ variantAnalysisResultsManager ,
308
+ variantAnalysisIdStoragePath ,
309
+ nwo ,
310
+ ) ;
306
311
307
312
// Read the contents of the variant analysis' `repo_task.json` file,
308
313
// and confirm that the `databaseCommitSha` and `resultCount` exist.
309
- const repoTask : VariantAnalysisRepositoryTask =
310
- await readRepoTask ( repoStoragePath ) ;
314
+ const repoTask : VariantAnalysisRepositoryTask = await readRepoTask (
315
+ variantAnalysisResultsManager . getRepoStorageDirectory (
316
+ variantAnalysisIdStoragePath ,
317
+ nwo ,
318
+ ) ,
319
+ ) ;
311
320
if ( ! repoTask . databaseCommitSha ) {
312
321
throw new Error ( `Missing database commit SHA for ${ nwo } ` ) ;
313
322
}
@@ -352,22 +361,30 @@ async function processSelectedRepositories(
352
361
}
353
362
354
363
/**
355
- * Gets the path to a SARIF file in a given `repoStoragePath `.
364
+ * Gets the path to a SARIF file for a given `nwo `.
356
365
*/
357
- async function getSarifFile (
358
- repoStoragePath : string ,
366
+ async function getRepoSarifFile (
367
+ variantAnalysisResultsManager : VariantAnalysisResultsManager ,
368
+ variantAnalysisIdStoragePath : string ,
359
369
nwo : string ,
360
370
) : Promise < string > {
361
- // Get results directory path.
362
- const repoResultsStoragePath = join ( repoStoragePath , "results" ) ;
363
- // Find sarif file.
364
- const sarifFiles = await glob ( `${ repoResultsStoragePath } /**/*.sarif` ) ;
365
- if ( sarifFiles . length !== 1 ) {
366
- throw new Error (
367
- `Expected to find exactly one \`*.sarif\` file for ${ nwo } , but found ${ sarifFiles . length } .` ,
371
+ if (
372
+ ! ( await variantAnalysisResultsManager . isVariantAnalysisRepoDownloaded (
373
+ variantAnalysisIdStoragePath ,
374
+ nwo ,
375
+ ) )
376
+ ) {
377
+ throw new Error ( `Variant analysis results not downloaded for ${ nwo } ` ) ;
378
+ }
379
+ const sarifFile =
380
+ variantAnalysisResultsManager . getRepoResultsSarifStoragePath (
381
+ variantAnalysisIdStoragePath ,
382
+ nwo ,
368
383
) ;
384
+ if ( ! ( await pathExists ( sarifFile ) ) ) {
385
+ throw new Error ( `SARIF file not found for ${ nwo } ` ) ;
369
386
}
370
- return sarifFiles [ 0 ] ;
387
+ return sarifFile ;
371
388
}
372
389
373
390
/**
0 commit comments