|
| 1 | +import { CancellationTokenSource } from "vscode"; |
| 2 | +import { join } from "path"; |
| 3 | +import { runQuery } from "./external-api-usage-query"; |
| 4 | +import { CodeQLCliServer } from "../codeql-cli/cli"; |
| 5 | +import { QueryRunner } from "../query-server"; |
| 6 | +import { DatabaseItem } from "../databases/local-databases"; |
| 7 | +import { interpretResultsSarif } from "../query-results"; |
| 8 | +import { ProgressCallback } from "../common/vscode/progress"; |
| 9 | + |
| 10 | +type Options = { |
| 11 | + cliServer: CodeQLCliServer; |
| 12 | + queryRunner: QueryRunner; |
| 13 | + databaseItem: DatabaseItem; |
| 14 | + queryStorageDir: string; |
| 15 | + |
| 16 | + progress: ProgressCallback; |
| 17 | +}; |
| 18 | + |
| 19 | +export type UsageSnippetsBySignature = Record<string, string[]>; |
| 20 | + |
| 21 | +export async function getAutoModelUsages({ |
| 22 | + cliServer, |
| 23 | + queryRunner, |
| 24 | + databaseItem, |
| 25 | + queryStorageDir, |
| 26 | + progress, |
| 27 | +}: Options): Promise<UsageSnippetsBySignature> { |
| 28 | + const maxStep = 1500; |
| 29 | + |
| 30 | + const cancellationTokenSource = new CancellationTokenSource(); |
| 31 | + |
| 32 | + // This will re-run the query that was already run when opening the data extensions editor. This |
| 33 | + // might be unnecessary, but this makes it really easy to get the path to the BQRS file which we |
| 34 | + // need to interpret the results. |
| 35 | + const queryResult = await runQuery({ |
| 36 | + cliServer, |
| 37 | + queryRunner, |
| 38 | + queryStorageDir, |
| 39 | + databaseItem, |
| 40 | + progress: (update) => |
| 41 | + progress({ |
| 42 | + maxStep, |
| 43 | + step: update.step, |
| 44 | + message: update.message, |
| 45 | + }), |
| 46 | + token: cancellationTokenSource.token, |
| 47 | + }); |
| 48 | + if (!queryResult) { |
| 49 | + throw new Error("Query failed"); |
| 50 | + } |
| 51 | + |
| 52 | + progress({ |
| 53 | + maxStep, |
| 54 | + step: 1100, |
| 55 | + message: "Retrieving source location prefix", |
| 56 | + }); |
| 57 | + |
| 58 | + // CodeQL needs to have access to the database to be able to retrieve the |
| 59 | + // snippets from it. The source location prefix is used to determine the |
| 60 | + // base path of the database. |
| 61 | + const sourceLocationPrefix = await databaseItem.getSourceLocationPrefix( |
| 62 | + cliServer, |
| 63 | + ); |
| 64 | + const sourceArchiveUri = databaseItem.sourceArchive; |
| 65 | + const sourceInfo = |
| 66 | + sourceArchiveUri === undefined |
| 67 | + ? undefined |
| 68 | + : { |
| 69 | + sourceArchive: sourceArchiveUri.fsPath, |
| 70 | + sourceLocationPrefix, |
| 71 | + }; |
| 72 | + |
| 73 | + progress({ |
| 74 | + maxStep, |
| 75 | + step: 1200, |
| 76 | + message: "Interpreting results", |
| 77 | + }); |
| 78 | + |
| 79 | + // Convert the results to SARIF so that Codeql will retrieve the snippets |
| 80 | + // from the datababe. This means we don't need to do that in the extension |
| 81 | + // and everything is handled by the CodeQL CLI. |
| 82 | + const sarif = await interpretResultsSarif( |
| 83 | + cliServer, |
| 84 | + { |
| 85 | + // To interpret the results we need to provide metadata about the query. We could do this using |
| 86 | + // `resolveMetadata` but that would be an extra call to the CodeQL CLI server and would require |
| 87 | + // us to know the path to the query on the filesystem. Since we know what the metadata should |
| 88 | + // look like and the only metadata that the CodeQL CLI requires is an ID and the kind, we can |
| 89 | + // simply use constants here. |
| 90 | + kind: "problem", |
| 91 | + id: "usage", |
| 92 | + }, |
| 93 | + { |
| 94 | + resultsPath: queryResult.outputDir.bqrsPath, |
| 95 | + interpretedResultsPath: join( |
| 96 | + queryStorageDir, |
| 97 | + "interpreted-results.sarif", |
| 98 | + ), |
| 99 | + }, |
| 100 | + sourceInfo, |
| 101 | + ["--sarif-add-snippets"], |
| 102 | + ); |
| 103 | + |
| 104 | + progress({ |
| 105 | + maxStep, |
| 106 | + step: 1400, |
| 107 | + message: "Parsing results", |
| 108 | + }); |
| 109 | + |
| 110 | + const snippets: UsageSnippetsBySignature = {}; |
| 111 | + |
| 112 | + const results = sarif.runs[0]?.results; |
| 113 | + if (!results) { |
| 114 | + throw new Error("No results"); |
| 115 | + } |
| 116 | + |
| 117 | + // This will group the snippets by the method signature. |
| 118 | + for (const result of results) { |
| 119 | + const signature = result.message.text; |
| 120 | + |
| 121 | + const snippet = |
| 122 | + result.locations?.[0]?.physicalLocation?.contextRegion?.snippet?.text; |
| 123 | + |
| 124 | + if (!signature || !snippet) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + if (!(signature in snippets)) { |
| 129 | + snippets[signature] = []; |
| 130 | + } |
| 131 | + |
| 132 | + snippets[signature].push(snippet); |
| 133 | + } |
| 134 | + |
| 135 | + return snippets; |
| 136 | +} |
0 commit comments