From 115ff838c53a65cbcb916ebb83e072308002150e Mon Sep 17 00:00:00 2001 From: Charis Kyriakou Date: Thu, 14 Mar 2024 14:39:30 +0000 Subject: [PATCH] Add model packs to variant analyses (#3472) --- .../src/variant-analysis/run-remote-query.ts | 35 ++++++++++++------- .../shared/variant-analysis.ts | 2 ++ .../variant-analysis-manager.ts | 2 ++ .../variant-analysis-mapper.ts | 11 +++++- .../variant-analysis-mapper.test.ts | 2 ++ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts b/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts index 05b99d079..6e6d877eb 100644 --- a/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts +++ b/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts @@ -37,25 +37,31 @@ import { import type { QlPackFile } from "../packaging/qlpack-file"; import { expandShortPaths } from "../common/short-paths"; import type { QlPackDetails } from "./ql-pack-details"; +import type { ModelPackDetails } from "../common/model-pack-details"; /** * Well-known names for the query pack used by the server. */ const QUERY_PACK_NAME = "codeql-remote/query"; +interface GeneratedQlPackDetails { + base64Pack: string; + modelPacks: ModelPackDetails[]; +} + /** * Two possibilities: * 1. There is no qlpack.yml (or codeql-pack.yml) in this directory. Assume this is a lone query and generate a synthetic qlpack for it. * 2. There is a qlpack.yml (or codeql-pack.yml) in this directory. Assume this is a query pack and use the yml to pack the query before uploading it. * - * @returns the entire qlpack as a base64 string. + * @returns details about the generated QL pack. */ async function generateQueryPack( cliServer: CodeQLCliServer, qlPackDetails: QlPackDetails, tmpDir: RemoteQueryTempDir, token: CancellationToken, -): Promise { +): Promise { const workspaceFolders = getOnDiskWorkspaceFolders(); const extensionPacks = await getExtensionPacksToInject( cliServer, @@ -129,7 +135,7 @@ async function generateQueryPack( ...queryOpts, // We need to specify the extension packs as dependencies so that they are included in the MRVA pack. // The version range doesn't matter, since they'll always be found by source lookup. - ...extensionPacks.map((p) => `--extension-pack=${p}@*`), + ...extensionPacks.map((p) => `--extension-pack=${p.name}@*`), ]; } else { precompilationOpts = ["--qlx"]; @@ -152,7 +158,10 @@ async function generateQueryPack( token, ); const base64Pack = (await readFile(bundlePath)).toString("base64"); - return base64Pack; + return { + base64Pack, + modelPacks: extensionPacks, + }; } async function createNewQueryPack( @@ -278,6 +287,7 @@ async function getPackedBundlePath(remoteQueryDir: string): Promise { interface PreparedRemoteQuery { actionBranch: string; base64Pack: string; + modelPacks: ModelPackDetails[]; repoSelection: RepositorySelection; controllerRepo: Repository; queryStartTime: number; @@ -330,10 +340,10 @@ export async function prepareRemoteQueryRun( const tempDir = await createRemoteQueriesTempDirectory(); - let base64Pack: string; + let generatedPack: GeneratedQlPackDetails; try { - base64Pack = await generateQueryPack( + generatedPack = await generateQueryPack( cliServer, qlPackDetails, tempDir, @@ -358,7 +368,8 @@ export async function prepareRemoteQueryRun( return { actionBranch, - base64Pack, + base64Pack: generatedPack.base64Pack, + modelPacks: generatedPack.modelPacks, repoSelection, controllerRepo, queryStartTime, @@ -404,8 +415,8 @@ async function fixPackFile( async function getExtensionPacksToInject( cliServer: CodeQLCliServer, workspaceFolders: string[], -): Promise { - const result: string[] = []; +): Promise { + const result: ModelPackDetails[] = []; if (cliServer.useExtensionPacks()) { const extensionPacks = await cliServer.resolveQlpacks( workspaceFolders, @@ -422,7 +433,7 @@ async function getExtensionPacksToInject( )}`, ); } - result.push(name); + result.push({ name, path: paths[0] }); }); } @@ -431,7 +442,7 @@ async function getExtensionPacksToInject( async function addExtensionPacksAsDependencies( queryPackDir: string, - extensionPacks: string[], + extensionPacks: ModelPackDetails[], ): Promise { const qlpackFile = await getQlPackFilePath(queryPackDir); if (!qlpackFile) { @@ -447,7 +458,7 @@ async function addExtensionPacksAsDependencies( ) as QlPackFile; const dependencies = syntheticQueryPack.dependencies ?? {}; - extensionPacks.forEach((name) => { + extensionPacks.forEach(({ name }) => { // Add this extension pack as a dependency. It doesn't matter which // version we specify, since we are guaranteed that the extension pack // is resolved from source at the given path. diff --git a/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts index 3a698d24c..16da2ecf4 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts @@ -1,6 +1,7 @@ import type { Repository, RepositoryWithMetadata } from "./repository"; import type { AnalysisAlert, AnalysisRawResults } from "./analysis-result"; import { QueryLanguage } from "../../common/query-language"; +import type { ModelPackDetails } from "../../common/model-pack-details"; export interface VariantAnalysis { id: number; @@ -13,6 +14,7 @@ export interface VariantAnalysis { kind?: string; }; queries?: VariantAnalysisQueries; + modelPacks?: ModelPackDetails[]; databases: { repositories?: string[]; repositoryLists?: string[]; diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts index 4a39ac1b1..989e94344 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -350,6 +350,7 @@ export class VariantAnalysisManager const { actionBranch, base64Pack, + modelPacks, repoSelection, controllerRepo, queryStartTime, @@ -410,6 +411,7 @@ export class VariantAnalysisManager const mappedVariantAnalysis = mapVariantAnalysisFromSubmission( variantAnalysisSubmission, variantAnalysisResponse, + modelPacks, ); await this.onVariantAnalysisSubmitted(mappedVariantAnalysis); diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts index a40f3b57d..c3a72a267 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts @@ -1,3 +1,4 @@ +import type { ModelPackDetails } from "../common/model-pack-details"; import type { VariantAnalysis as ApiVariantAnalysis, VariantAnalysisScannedRepository as ApiVariantAnalysisScannedRepository, @@ -26,6 +27,7 @@ import { export function mapVariantAnalysisFromSubmission( submission: VariantAnalysisSubmission, apiVariantAnalysis: ApiVariantAnalysis, + modelPacks: ModelPackDetails[], ): VariantAnalysis { return mapVariantAnalysis( { @@ -37,6 +39,7 @@ export function mapVariantAnalysisFromSubmission( kind: submission.query.kind, }, queries: submission.queries, + modelPacks, databases: submission.databases, executionStartTime: submission.startTime, }, @@ -59,7 +62,12 @@ export function mapUpdatedVariantAnalysis( function mapVariantAnalysis( currentVariantAnalysis: Pick< VariantAnalysis, - "language" | "query" | "queries" | "databases" | "executionStartTime" + | "language" + | "query" + | "queries" + | "databases" + | "executionStartTime" + | "modelPacks" >, currentStatus: VariantAnalysisStatus | undefined, response: ApiVariantAnalysis, @@ -96,6 +104,7 @@ function mapVariantAnalysis( language: currentVariantAnalysis.language, query: currentVariantAnalysis.query, queries: currentVariantAnalysis.queries, + modelPacks: currentVariantAnalysis.modelPacks, databases: currentVariantAnalysis.databases, executionStartTime: currentVariantAnalysis.executionStartTime, createdAt: response.created_at, diff --git a/extensions/ql-vscode/test/unit-tests/variant-analysis/variant-analysis-mapper.test.ts b/extensions/ql-vscode/test/unit-tests/variant-analysis/variant-analysis-mapper.test.ts index c1b38ccde..c033d28d8 100644 --- a/extensions/ql-vscode/test/unit-tests/variant-analysis/variant-analysis-mapper.test.ts +++ b/extensions/ql-vscode/test/unit-tests/variant-analysis/variant-analysis-mapper.test.ts @@ -31,6 +31,7 @@ describe(mapVariantAnalysisFromSubmission.name, () => { const result = mapVariantAnalysisFromSubmission( mockSubmission, mockApiResponse, + [], ); const { @@ -54,6 +55,7 @@ describe(mapVariantAnalysisFromSubmission.name, () => { text: mockSubmission.query.text, kind: "table", }, + modelPacks: [], databases: { repositories: ["1", "2", "3"], },