From 35ab6e259cb6b8bc44a15238ae64131d52356838 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:37 +0100 Subject: [PATCH 01/18] Refactoring: Extract the submission request mapper Move the code that builds the variant analysis submission request body from gh-api-client into variant-analysis-mapper, as mapVariantAnalysisSubmission, so that the custom-server client can reuse it. Category: Refactoring. submitVariantAnalysis sends the same request body, built by the same code in another module. The existing gh-api-client tests pass unchanged. --- .../src/variant-analysis/gh-api/gh-api-client.ts | 14 +++----------- .../variant-analysis/variant-analysis-mapper.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts b/extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts index be8d323c7c4..30d7447d21d 100644 --- a/extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts +++ b/extensions/ql-vscode/src/variant-analysis/gh-api/gh-api-client.ts @@ -3,9 +3,9 @@ import type { VariantAnalysisSubmission } from "../shared/variant-analysis"; import type { VariantAnalysis, VariantAnalysisRepoTask, - VariantAnalysisSubmissionRequest, } from "./variant-analysis"; import type { Repository } from "./repository"; +import { mapVariantAnalysisSubmission } from "../variant-analysis-mapper"; export async function submitVariantAnalysis( credentials: Credentials, @@ -13,17 +13,9 @@ export async function submitVariantAnalysis( ): Promise { const octokit = await credentials.getOctokit(); - const { actionRepoRef, language, pack, databases, controllerRepoId } = - submissionDetails; + const { controllerRepoId } = submissionDetails; - const data: VariantAnalysisSubmissionRequest = { - action_repo_ref: actionRepoRef, - language, - query_pack: pack, - repositories: databases.repositories, - repository_lists: databases.repositoryLists, - repository_owners: databases.repositoryOwners, - }; + const data = mapVariantAnalysisSubmission(submissionDetails); const response = await octokit.request({ method: "POST", 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 2b8858035a2..daf93bb8b98 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts @@ -9,6 +9,7 @@ import type { VariantAnalysisSkippedRepositoryGroup as ApiVariantAnalysisSkippedRepositoryGroup, VariantAnalysisNotFoundRepositoryGroup as ApiVariantAnalysisNotFoundRepositoryGroup, VariantAnalysisRepoTask as ApiVariantAnalysisRepoTask, + VariantAnalysisSubmissionRequest, } from "./gh-api/variant-analysis"; import type { VariantAnalysis, @@ -24,6 +25,19 @@ import { VariantAnalysisRepoStatus, } from "./shared/variant-analysis"; +export function mapVariantAnalysisSubmission( + submission: VariantAnalysisSubmission, +): VariantAnalysisSubmissionRequest { + return { + action_repo_ref: submission.actionRepoRef, + language: submission.language, + query_pack: submission.pack, + repositories: submission.databases.repositories, + repository_lists: submission.databases.repositoryLists, + repository_owners: submission.databases.repositoryOwners, + }; +} + export function mapVariantAnalysisFromSubmission( submission: VariantAnalysisSubmission, apiVariantAnalysis: ApiVariantAnalysis, From 328c08bad28278961e0c584edddbf37b494ef9c6 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:37 +0100 Subject: [PATCH 02/18] Refactoring: Route variant analysis API calls through one module Add variant-analysis-api.ts with submitVariantAnalysis, getVariantAnalysis, getVariantAnalysisRepo and cancelVariantAnalysis. Each one calls the existing GitHub function with the same arguments. The manager and the monitor import them instead of the GitHub clients, so that a later commit can choose the server in one place. Category: Refactoring. The new functions only forward to the unchanged GitHub clients, and the call sites change only their imports. The existing manager, monitor and CLI tests, which spy on the GitHub clients, pass unchanged. --- .../variant-analysis/variant-analysis-api.ts | 55 +++++++++++++++++++ .../variant-analysis-manager.ts | 4 +- .../variant-analysis-monitor.ts | 2 +- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts new file mode 100644 index 00000000000..08861c4c7b4 --- /dev/null +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -0,0 +1,55 @@ +import type { Credentials } from "../common/authentication"; +import { cancelVariantAnalysis as cancelGitHubVariantAnalysis } from "./gh-api/gh-actions-api-client"; +import { + getVariantAnalysis as getGitHubVariantAnalysis, + getVariantAnalysisRepo as getGitHubVariantAnalysisRepo, + submitVariantAnalysis as submitGitHubVariantAnalysis, +} from "./gh-api/gh-api-client"; +import type { + VariantAnalysis as ApiVariantAnalysis, + VariantAnalysisRepoTask as ApiVariantAnalysisRepoTask, +} from "./gh-api/variant-analysis"; +import type { + VariantAnalysis, + VariantAnalysisSubmission, +} from "./shared/variant-analysis"; + +export async function submitVariantAnalysis( + credentials: Credentials, + submission: VariantAnalysisSubmission, +): Promise { + return submitGitHubVariantAnalysis(credentials, submission); +} + +export async function getVariantAnalysis( + credentials: Credentials, + controllerRepoId: number, + variantAnalysisId: number, +): Promise { + return getGitHubVariantAnalysis( + credentials, + controllerRepoId, + variantAnalysisId, + ); +} + +export async function getVariantAnalysisRepo( + credentials: Credentials, + controllerRepoId: number, + variantAnalysisId: number, + repoId: number, +): Promise { + return getGitHubVariantAnalysisRepo( + credentials, + controllerRepoId, + variantAnalysisId, + repoId, + ); +} + +export async function cancelVariantAnalysis( + credentials: Credentials, + variantAnalysis: VariantAnalysis, +): Promise { + return cancelGitHubVariantAnalysis(credentials, variantAnalysis); +} 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 ad5dee850bf..b42dca25f99 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -1,9 +1,10 @@ import { join } from "path"; import { + cancelVariantAnalysis, submitVariantAnalysis, getVariantAnalysisRepo, -} from "./gh-api/gh-api-client"; +} from "./variant-analysis-api"; import type { VariantAnalysis as ApiVariantAnalysis } from "./gh-api/variant-analysis"; import type { AuthenticationSessionsChangeEvent, @@ -52,7 +53,6 @@ import PQueue from "p-queue"; import { createTimestampFile, saveBeforeStart } from "../run-queries-shared"; import { readFile, remove, pathExists } from "fs-extra"; import { EOL } from "os"; -import { cancelVariantAnalysis } from "./gh-api/gh-actions-api-client"; import type { ProgressCallback } from "../common/vscode/progress"; import { UserCancellationException, diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts index 8592f84353d..ed25228c431 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts @@ -1,5 +1,5 @@ import { env, EventEmitter } from "vscode"; -import { getVariantAnalysis } from "./gh-api/gh-api-client"; +import { getVariantAnalysis } from "./variant-analysis-api"; import { RequestError } from "@octokit/request-error"; import type { From dd4e18d61d08c97d1ba43426c13f07935282357f Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:37 +0100 Subject: [PATCH 03/18] Refactoring: Allow analysis results without file links Results from a custom server may have no repository URL to link to. Allow fileLinkPrefix to be undefined, and in that case show file locations as plain text instead of links, both in the results view and in exported Markdown. Category: Refactoring. The changes widen the type of fileLinkPrefix and add branches for an undefined prefix. The results manager always builds a prefix for GitHub results, from the GitHub host and the repository name, so those branches never run, and GitHub results render and export as before. New tests cover linked and plain-text output. --- extensions/ql-vscode/src/common/bqrs-utils.ts | 4 +-- .../src/common/location-link-utils.ts | 5 ++- .../src/variant-analysis/bqrs-processing.ts | 2 +- .../variant-analysis/markdown-generation.ts | 11 +++--- .../src/variant-analysis/sarif-processing.ts | 16 ++++++--- .../shared/analysis-result.ts | 4 +-- .../FileCodeSnippet/CodeSnippetMessage.tsx | 3 ++ .../FileCodeSnippet/FileCodeSnippet.tsx | 19 +++++----- .../view/variant-analysis/RawResultCell.tsx | 2 +- .../view/variant-analysis/RawResultRow.tsx | 2 +- .../view/variant-analysis/RawResultsTable.tsx | 2 +- .../AnalyzedRepoItemContent.spec.tsx | 36 +++++++++++++++++++ .../test/unit-tests/common/bqrs-utils.test.ts | 22 ++++++++++++ .../markdown-generation.test.ts | 36 +++++++++++++++++++ 14 files changed, 134 insertions(+), 30 deletions(-) create mode 100644 extensions/ql-vscode/test/unit-tests/common/bqrs-utils.test.ts diff --git a/extensions/ql-vscode/src/common/bqrs-utils.ts b/extensions/ql-vscode/src/common/bqrs-utils.ts index 1c0ec8e2834..f598fe97261 100644 --- a/extensions/ql-vscode/src/common/bqrs-utils.ts +++ b/extensions/ql-vscode/src/common/bqrs-utils.ts @@ -12,10 +12,10 @@ export function isEmptyPath(uriStr: string) { export function tryGetRemoteLocation( loc: UrlValue | undefined, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, sourceLocationPrefix: string | undefined, ): string | undefined { - if (!loc || !isUrlValueResolvable(loc)) { + if (!fileLinkPrefix || !loc || !isUrlValueResolvable(loc)) { return undefined; } diff --git a/extensions/ql-vscode/src/common/location-link-utils.ts b/extensions/ql-vscode/src/common/location-link-utils.ts index 6ed58336e7a..c8ff9a94e10 100644 --- a/extensions/ql-vscode/src/common/location-link-utils.ts +++ b/extensions/ql-vscode/src/common/location-link-utils.ts @@ -6,7 +6,10 @@ export function createRemoteFileRef( endLine?: number, startColumn?: number, endColumn?: number, -): string { +): string | undefined { + if (!fileLink.fileLinkPrefix) { + return undefined; + } if ( startColumn && endColumn && diff --git a/extensions/ql-vscode/src/variant-analysis/bqrs-processing.ts b/extensions/ql-vscode/src/variant-analysis/bqrs-processing.ts index 0f0506b7784..2ef0da44081 100644 --- a/extensions/ql-vscode/src/variant-analysis/bqrs-processing.ts +++ b/extensions/ql-vscode/src/variant-analysis/bqrs-processing.ts @@ -9,7 +9,7 @@ export async function extractRawResults( cliServer: CodeQLCliServer, logger: Logger, filePath: string, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, sourceLocationPrefix: string, ): Promise { const bqrsInfo = await cliServer.bqrsInfo(filePath); diff --git a/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts b/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts index e295282d343..ade753ee4f9 100644 --- a/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts +++ b/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts @@ -323,7 +323,7 @@ function generateMarkdownForRawResults( function generateMarkdownForRawTableCell( value: CellValue, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, sourceLocationPrefix: string, ) { let cellValue: string; @@ -363,16 +363,15 @@ function createMarkdownRemoteFileRef( region?: HighlightedRegion, linkText?: string, ): string { - const markdownLink = `[${ - linkText || fileLink.filePath - }](${createRemoteFileRef( + const url = createRemoteFileRef( fileLink, region?.startLine, region?.endLine, region?.startColumn, region?.endColumn, - )})`; - return markdownLink; + ); + const text = linkText || fileLink.filePath; + return url ? `[${text}](${url})` : text; } /** diff --git a/extensions/ql-vscode/src/variant-analysis/sarif-processing.ts b/extensions/ql-vscode/src/variant-analysis/sarif-processing.ts index f998f228e19..5995d737558 100644 --- a/extensions/ql-vscode/src/variant-analysis/sarif-processing.ts +++ b/extensions/ql-vscode/src/variant-analysis/sarif-processing.ts @@ -35,7 +35,7 @@ const defaultSeverity = "Warning"; export function extractAnalysisAlerts( sarifLog: Log, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, ): { alerts: AnalysisAlert[]; errors: string[]; @@ -62,7 +62,7 @@ export function extractAnalysisAlerts( function extractResultAlerts( run: Run, result: Result, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, ): AnalysisAlert[] { const alerts: AnalysisAlert[] = []; @@ -259,7 +259,10 @@ function getHighlightedRegion(region: Region): HighlightedRegion { }; } -function getCodeFlows(result: Result, fileLinkPrefix: string): CodeFlow[] { +function getCodeFlows( + result: Result, + fileLinkPrefix: string | undefined, +): CodeFlow[] { const codeFlows = []; if (result.codeFlows) { @@ -299,7 +302,10 @@ function getCodeFlows(result: Result, fileLinkPrefix: string): CodeFlow[] { return codeFlows; } -function getMessage(result: Result, fileLinkPrefix: string): AnalysisMessage { +function getMessage( + result: Result, + fileLinkPrefix: string | undefined, +): AnalysisMessage { const tokens: AnalysisMessageToken[] = []; const messageText = result.message.text!; @@ -328,7 +334,7 @@ function getMessage(result: Result, fileLinkPrefix: string): AnalysisMessage { function getRelatedLocation( messagePart: SarifLink, result: Result, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, ): AnalysisMessageLocationTokenLocation | undefined { const relatedLocation = result.relatedLocations!.find( (rl) => rl.id === messagePart.dest, diff --git a/extensions/ql-vscode/src/variant-analysis/shared/analysis-result.ts b/extensions/ql-vscode/src/variant-analysis/shared/analysis-result.ts index 91851c373aa..9cf725b9791 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/analysis-result.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/analysis-result.ts @@ -2,7 +2,7 @@ import type { RawResultSet } from "../../common/raw-result-types"; export interface AnalysisRawResults { resultSet: RawResultSet; - fileLinkPrefix: string; + fileLinkPrefix: string | undefined; sourceLocationPrefix: string; capped: boolean; } @@ -18,7 +18,7 @@ export interface AnalysisAlert { } export interface FileLink { - fileLinkPrefix: string; + fileLinkPrefix: string | undefined; filePath: string; } diff --git a/extensions/ql-vscode/src/view/common/FileCodeSnippet/CodeSnippetMessage.tsx b/extensions/ql-vscode/src/view/common/FileCodeSnippet/CodeSnippetMessage.tsx index 09ec16d4288..12c06e2f741 100644 --- a/extensions/ql-vscode/src/view/common/FileCodeSnippet/CodeSnippetMessage.tsx +++ b/extensions/ql-vscode/src/view/common/FileCodeSnippet/CodeSnippetMessage.tsx @@ -64,6 +64,9 @@ export const CodeSnippetMessage = ({ case "text": return {token.text}; case "location": + if (!token.location.fileLink.fileLinkPrefix) { + return {token.text}; + } return ( + {fileLink.filePath} + + ) : ( + fileLink.filePath + ); if (!codeSnippet) { return ( - - - {fileLink.filePath} - - + {title} {message && severity && ( {messageChildren} @@ -89,11 +92,7 @@ export const FileCodeSnippet = ({ return ( - - - {fileLink.filePath} - - + {title} {code.map((line, index) => ( ` type RawResultsTableProps = { resultSet: RawResultSet; - fileLinkPrefix: string; + fileLinkPrefix: string | undefined; sourceLocationPrefix: string; }; diff --git a/extensions/ql-vscode/src/view/variant-analysis/__tests__/AnalyzedRepoItemContent.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/AnalyzedRepoItemContent.spec.tsx index 1749fdd5bd4..b8d2da995f4 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/__tests__/AnalyzedRepoItemContent.spec.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/__tests__/AnalyzedRepoItemContent.spec.tsx @@ -19,6 +19,42 @@ describe(AnalyzedRepoItemContent.name, () => { ); }; + it.each([undefined, { startLine: 1, endLine: 1, text: "select 1" }])( + "renders file paths and message locations as text when repository links are unavailable", + (codeSnippet) => { + render({ + interpretedResults: [ + { + message: { + tokens: [ + { + t: "location", + text: "related location", + location: { + fileLink: { + fileLinkPrefix: undefined, + filePath: "src/related.ts", + }, + }, + }, + ], + }, + shortDescription: "A finding", + fileLink: { fileLinkPrefix: undefined, filePath: "src/query.ts" }, + severity: "Warning", + codeSnippet, + codeFlows: [], + }, + ], + }); + expect(screen.getByText("src/query.ts")).toBeInTheDocument(); + if (!codeSnippet) { + expect(screen.getByText("related location")).toBeInTheDocument(); + } + expect(screen.queryAllByRole("link")).toHaveLength(0); + }, + ); + it("renders the succeeded state with interpreted results", () => { render({ status: VariantAnalysisRepoStatus.Succeeded, diff --git a/extensions/ql-vscode/test/unit-tests/common/bqrs-utils.test.ts b/extensions/ql-vscode/test/unit-tests/common/bqrs-utils.test.ts new file mode 100644 index 00000000000..b2041b7e87c --- /dev/null +++ b/extensions/ql-vscode/test/unit-tests/common/bqrs-utils.test.ts @@ -0,0 +1,22 @@ +import { tryGetRemoteLocation } from "../../../src/common/bqrs-utils"; + +describe(tryGetRemoteLocation.name, () => { + it.each([ + [undefined, undefined], + [ + "https://source.example.com/org/repo/blob/abc123", + "https://source.example.com/org/repo/blob/abc123/src/query.ts", + ], + ])( + "only links raw results when a repository URL is available: %s", + (prefix, expected) => { + expect( + tryGetRemoteLocation( + { type: "wholeFileLocation", uri: "file:/sources/src/query.ts" }, + prefix, + "/sources", + ), + ).toBe(expected); + }, + ); +}); diff --git a/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts b/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts index 4b7601273bb..e22265de820 100644 --- a/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts +++ b/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts @@ -17,10 +17,46 @@ import type { import pathProblemAnalysesResults from "../data/markdown-generation/interpreted-results/path-problem/analyses-results.json"; import problemAnalysesResults from "../data/markdown-generation/interpreted-results/problem/analyses-results.json"; import rawResultsAnalysesResults from "../data/markdown-generation/raw-results/analyses-results.json"; +import { createMockVariantAnalysis } from "../../factories/variant-analysis/shared/variant-analysis"; +import { createMockScannedRepo } from "../../factories/variant-analysis/shared/scanned-repositories"; const dataPath = resolve(__dirname, "../data/markdown-generation"); describe(generateVariantAnalysisMarkdown.name, () => { + it("exports locations as text when repository links are unavailable", async () => { + const run = createMockVariantAnalysis({}); + const repo = createMockScannedRepo(); + repo.resultCount = 1; + const results: VariantAnalysisScannedRepositoryResult = { + variantAnalysisId: run.id, + repositoryId: repo.repository.id, + interpretedResults: [ + { + message: { tokens: [{ t: "text", text: "A finding" }] }, + shortDescription: "A finding", + severity: "Warning", + fileLink: { fileLinkPrefix: undefined, filePath: "src/query.ts" }, + codeFlows: [], + }, + ], + }; + async function* getUnlinkedResults(): AsyncIterable< + [VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult] + > { + yield [repo, results]; + } + const actual = await generateVariantAnalysisMarkdown( + run, + getUnlinkedResults(), + 1, + "local", + ); + const content = actual.markdownFiles[1].content.join("\n"); + expect(content).toContain("src/query.ts"); + expect(content).not.toContain("[src/query.ts]("); + expect(content).not.toContain("undefined"); + }); + describe("for path-problem query", () => { it("should generate markdown file for each repo with results", async () => { const actualFiles = await generateVariantAnalysisMarkdown( From 8ca3e12d413a18253d02b6789e64af320ef422a6 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:37 +0100 Subject: [PATCH 04/18] Refactoring: Record the originating server on runs Add an optional serverUrl to variant analyses and submissions. The mappers copy it from the submission to the run and keep it across status updates, and query history saves and restores it. Later commits use it to keep each run bound to the server that ran it. Category: Refactoring. Nothing sets serverUrl yet, so it stays undefined: requests, runs and views are unchanged. JSON omits undefined fields, so saved query history is unchanged as well. --- ...-history-variant-analysis-domain-mapper.ts | 1 + ...ery-history-variant-analysis-dto-mapper.ts | 1 + .../query-history-variant-analysis-dto.ts | 1 + .../shared/variant-analysis.ts | 3 +++ .../variant-analysis-mapper.ts | 3 +++ .../variant-analysis-mapper.test.ts | 21 +++++++++++++++++++ .../store/query-history-store.test.ts | 10 +++++++++ 7 files changed, 40 insertions(+) diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts index 6ca7c63132e..a6e9fbd017c 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts @@ -48,6 +48,7 @@ function mapVariantAnalysisDtoToDto( ): VariantAnalysisDto { return { id: variantAnalysis.id, + serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, fullName: variantAnalysis.controllerRepo.fullName, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts index 7f81ed91821..6d1af88f36e 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts @@ -48,6 +48,7 @@ function mapVariantAnalysisToDomainModel( ): VariantAnalysis { return { id: variantAnalysis.id, + serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, fullName: variantAnalysis.controllerRepo.fullName, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts index dfe07af1de3..1e65dcd7012 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts @@ -15,6 +15,7 @@ export interface QueryHistoryVariantAnalysisDto { export interface VariantAnalysisDto { id: number; + serverUrl?: string; controllerRepo: { id: number; fullName: string; 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 a83a5f2bbf6..5632591bc62 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts @@ -5,6 +5,8 @@ import type { ModelPackDetails } from "../../common/model-pack-details"; export interface VariantAnalysis { id: number; + /** Custom server that created this run; absent for GitHub and legacy runs. */ + serverUrl?: string; controllerRepo: Repository; language: QueryLanguage; query: { @@ -136,6 +138,7 @@ export interface VariantAnalysisScannedRepositoryResult { */ export interface VariantAnalysisSubmission { startTime: number; + serverUrl?: string; controllerRepoId: number; actionRepoRef: string; language: QueryLanguage; 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 daf93bb8b98..f5d4fedc1fd 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts @@ -45,6 +45,7 @@ export function mapVariantAnalysisFromSubmission( ): VariantAnalysis { return mapVariantAnalysis( { + serverUrl: submission.serverUrl, language: submission.language, query: { name: submission.query.name, @@ -77,6 +78,7 @@ function mapVariantAnalysis( currentVariantAnalysis: Pick< VariantAnalysis, | "language" + | "serverUrl" | "query" | "queries" | "databases" @@ -106,6 +108,7 @@ function mapVariantAnalysis( const variantAnalysis: VariantAnalysis = { id: response.id, + serverUrl: currentVariantAnalysis.serverUrl, controllerRepo: { id: response.controller_repo.id, fullName: response.controller_repo.full_name, 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 c033d28d8bd..44fce971ad7 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 @@ -4,6 +4,7 @@ import type { VariantAnalysisScannedRepository } from "../../../src/variant-anal import { VariantAnalysisRepoStatus } from "../../../src/variant-analysis/shared/variant-analysis"; import { mapScannedRepository, + mapUpdatedVariantAnalysis, mapVariantAnalysisFromSubmission, mapVariantAnalysisRepositoryTask, } from "../../../src/variant-analysis/variant-analysis-mapper"; @@ -27,6 +28,26 @@ describe(mapVariantAnalysisFromSubmission.name, () => { ); const mockSubmission = createMockSubmission(); + it("preserves the server origin through submission and polling updates", () => { + const submission = { + ...mockSubmission, + serverUrl: "https://mrva.example.com", + }; + const run = mapVariantAnalysisFromSubmission( + submission, + mockApiResponse, + [], + ); + expect(run.serverUrl).toBe(submission.serverUrl); + expect(mapUpdatedVariantAnalysis(run, mockApiResponse).serverUrl).toBe( + submission.serverUrl, + ); + expect( + mapVariantAnalysisFromSubmission(mockSubmission, mockApiResponse, []) + .serverUrl, + ).toBeUndefined(); + }); + it("should map an API response and return a variant analysis", () => { const result = mapVariantAnalysisFromSubmission( mockSubmission, diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts index c0428ec2963..07f98d191b5 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts @@ -108,6 +108,16 @@ describe("write and read", () => { expect(allHistoryActual.length).toEqual(expectedHistory.length); }); + it("round-trips custom-server run data without changing legacy history", async () => { + const serverRun = createMockVariantAnalysisHistoryItem({}); + serverRun.variantAnalysis.serverUrl = "https://mrva.example.com"; + const legacyRun = createMockVariantAnalysisHistoryItem({}); + const historyPath = join(tmpDir.name, "server-and-legacy-history.json"); + await writeQueryHistoryToFile([serverRun, legacyRun], historyPath); + const actual = await readQueryHistoryFromFile(historyPath); + expect(actual).toEqual([serverRun, legacyRun]); + }); + it("should read query output dir from completed query if not present", async () => { const historyPath = join(tmpDir.name, "workspace-query-history.json"); From 5f830285fe9936635f4320bd092fae80ad6afed2 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 05/18] Adding custom-server support: Link results via repository URLs Repositories analyzed by a custom server are not hosted on the GitHub instance, so their results cannot be linked through the GitHub host and the repository name. Map html_url from the API into repositories and repository tasks, store it in query history and in the repository task store, and pass the run's serverUrl to the results manager. For server runs, file links are built from html_url and point to HEAD when the task has no commit SHA. Without a usable html_url, locations are shown as plain text. Category: Adding custom-server support. Link creation changes only inside `if (serverUrl)`, which only server runs reach. GitHub runs keep the missing-SHA error and their existing link prefix. GitHub's responses also include html_url, so GitHub runs now store it too, but nothing reads it for them. --- ...-history-variant-analysis-domain-mapper.ts | 1 + ...ery-history-variant-analysis-dto-mapper.ts | 1 + .../query-history-variant-analysis-dto.ts | 1 + .../src/variant-analysis/gh-api/repository.ts | 1 + .../repo-tasks-domain-mapper.ts | 1 + .../repo-tasks-store/repo-tasks-dto-mapper.ts | 1 + .../repo-tasks-store/repo-tasks-dto.ts | 1 + .../src/variant-analysis/shared/repository.ts | 1 + .../variant-analysis-manager.ts | 3 +- .../variant-analysis-mapper.ts | 2 + .../variant-analysis-results-manager.ts | 65 ++++++++++++++++--- .../variant-analysis-mapper.test.ts | 15 +++++ .../variant-analysis-results-manager.test.ts | 48 ++++++++++++++ .../store/query-history-store.test.ts | 2 + 14 files changed, 132 insertions(+), 11 deletions(-) diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts index a6e9fbd017c..73719a21e0e 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts @@ -97,6 +97,7 @@ function mapVariantAnalysisScannedRepositoryToDto( repository: { id: repo.repository.id, fullName: repo.repository.fullName, + htmlUrl: repo.repository.htmlUrl, private: repo.repository.private, stargazersCount: repo.repository.stargazersCount, updatedAt: repo.repository.updatedAt, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts index 6d1af88f36e..e2775b7b389 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts @@ -103,6 +103,7 @@ function mapVariantAnalysisScannedRepositoryToDomainModel( repository: { id: repo.repository.id, fullName: repo.repository.fullName, + htmlUrl: repo.repository.htmlUrl, private: repo.repository.private, stargazersCount: repo.repository.stargazersCount, updatedAt: repo.repository.updatedAt, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts index 1e65dcd7012..e4550cdc639 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts @@ -48,6 +48,7 @@ export interface VariantAnalysisScannedRepositoryDto { repository: { id: number; fullName: string; + htmlUrl?: string; private: boolean; stargazersCount: number; updatedAt: string | null; diff --git a/extensions/ql-vscode/src/variant-analysis/gh-api/repository.ts b/extensions/ql-vscode/src/variant-analysis/gh-api/repository.ts index 08cfefa7546..24794a9a85e 100644 --- a/extensions/ql-vscode/src/variant-analysis/gh-api/repository.ts +++ b/extensions/ql-vscode/src/variant-analysis/gh-api/repository.ts @@ -9,6 +9,7 @@ export interface Repository { id: number; name: string; full_name: string; + html_url?: string; private: boolean; } diff --git a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-domain-mapper.ts b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-domain-mapper.ts index df5f3b09cad..24d53ba8cd7 100644 --- a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-domain-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-domain-mapper.ts @@ -11,6 +11,7 @@ export function mapRepoTaskToDomainModel( repository: { id: repoTask.repository.id, fullName: repoTask.repository.fullName, + htmlUrl: repoTask.repository.htmlUrl, private: repoTask.repository.private, }, analysisStatus: mapRepoTaskAnalysisStatusToDomainModel( diff --git a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto-mapper.ts b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto-mapper.ts index f5b23663352..10165ea078c 100644 --- a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto-mapper.ts @@ -11,6 +11,7 @@ export function mapRepoTaskToDto( repository: { id: repoTask.repository.id, fullName: repoTask.repository.fullName, + htmlUrl: repoTask.repository.htmlUrl, private: repoTask.repository.private, }, analysisStatus: mapRepoTaskAnalysisStatusToDto(repoTask.analysisStatus), diff --git a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto.ts b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto.ts index 4bfe618a17e..09ced6e2aea 100644 --- a/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto.ts +++ b/extensions/ql-vscode/src/variant-analysis/repo-tasks-store/repo-tasks-dto.ts @@ -12,6 +12,7 @@ export interface VariantAnalysisRepositoryTaskDto { interface RepositoryDto { id: number; fullName: string; + htmlUrl?: string; private: boolean; } diff --git a/extensions/ql-vscode/src/variant-analysis/shared/repository.ts b/extensions/ql-vscode/src/variant-analysis/shared/repository.ts index cf6b91e45f6..a5fbd5d1528 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/repository.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/repository.ts @@ -1,6 +1,7 @@ export interface Repository { id: number; fullName: string; + htmlUrl?: string; private: boolean; } 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 b42dca25f99..03f1675bb9b 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -643,7 +643,7 @@ export class VariantAnalysisManager variantAnalysisId, this.getVariantAnalysisStorageLocation(variantAnalysisId), repositoryFullName, - options, + { ...options, serverUrl: variantAnalysis.serverUrl }, ); } @@ -813,6 +813,7 @@ export class VariantAnalysisManager ); repoTask = mapVariantAnalysisRepositoryTask(repoTaskResponse); + repoTask.repository.htmlUrl ??= scannedRepo.repository.htmlUrl; } catch (e) { repoState.downloadStatus = VariantAnalysisScannedRepositoryDownloadStatus.Failed; 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 f5d4fedc1fd..cca834a797e 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts @@ -143,6 +143,7 @@ export function mapVariantAnalysisRepositoryTask( repository: { id: response.repository.id, fullName: response.repository.full_name, + htmlUrl: response.repository.html_url, private: response.repository.private, }, analysisStatus: mapApiRepoStatus(response.analysis_status), @@ -162,6 +163,7 @@ export function mapScannedRepository( repository: { id: scannedRepo.repository.id, fullName: scannedRepo.repository.full_name, + htmlUrl: scannedRepo.repository.html_url, private: scannedRepo.repository.private, stargazersCount: scannedRepo.repository.stargazers_count, updatedAt: scannedRepo.repository.updated_at, diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-results-manager.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-results-manager.ts index 01e27410ff0..beed270b62a 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-results-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-results-manager.ts @@ -37,6 +37,8 @@ type ResultDownloadedEvent = { }; export type LoadResultsOptions = { + // The custom variant analysis server that produced the results, if any. + serverUrl?: string; // If true, when results are loaded from storage, they will not be stored in the cache. This reduces memory usage if // results are only needed temporarily (e.g. for exporting results to a different format). skipCacheStore?: boolean; @@ -150,6 +152,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { variantAnalysisId, variantAnalysisStoragePath, repositoryFullName, + options.serverUrl, ); } @@ -157,6 +160,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { variantAnalysisId, variantAnalysisStoragePath, repositoryFullName, + options?.serverUrl, ); } @@ -164,11 +168,13 @@ export class VariantAnalysisResultsManager extends DisposableObject { variantAnalysisId: number, variantAnalysisStoragePath: string, repositoryFullName: string, + serverUrl?: string, ): Promise { const result = await this.loadResultsFromStorage( variantAnalysisId, variantAnalysisStoragePath, repositoryFullName, + serverUrl, ); this.cachedResults.set( createCacheKey(variantAnalysisId, repositoryFullName), @@ -182,6 +188,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { variantAnalysisId: number, variantAnalysisStoragePath: string, repositoryFullName: string, + serverUrl?: string, ): Promise { if ( !(await this.isVariantAnalysisRepoDownloaded( @@ -200,14 +207,24 @@ export class VariantAnalysisResultsManager extends DisposableObject { const repoTask: VariantAnalysisRepositoryTask = await readRepoTask(storageDirectory); - if (!repoTask.databaseCommitSha || !repoTask.sourceLocationPrefix) { - throw new Error("Missing database commit SHA"); - } + let fileLinkPrefix: string | undefined; + if (serverUrl) { + // Servers may omit the commit and source location prefix, so link to + // HEAD and fall back to the legacy source location handling. + fileLinkPrefix = createServerFileLinkPrefix( + repoTask.repository.htmlUrl, + repoTask.databaseCommitSha || "HEAD", + ); + } else { + if (!repoTask.databaseCommitSha || !repoTask.sourceLocationPrefix) { + throw new Error("Missing database commit SHA"); + } - const fileLinkPrefix = this.createGitHubFileLinkPrefix( - repoTask.repository.fullName, - repoTask.databaseCommitSha, - ); + fileLinkPrefix = this.createGitHubFileLinkPrefix( + repoTask.repository.fullName, + repoTask.databaseCommitSha, + ); + } const resultsDirectory = join( storageDirectory, @@ -233,7 +250,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { rawResults = await this.readBqrsResults( bqrsPath, fileLinkPrefix, - repoTask.sourceLocationPrefix, + repoTask.sourceLocationPrefix ?? "", ); } @@ -263,7 +280,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { private async readBqrsResults( filePath: string, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, sourceLocationPrefix: string, ): Promise { return await extractRawResults( @@ -277,7 +294,7 @@ export class VariantAnalysisResultsManager extends DisposableObject { private async readSarifResults( filePath: string, - fileLinkPrefix: string, + fileLinkPrefix: string | undefined, ): Promise { const sarifLog = await sarifParser(filePath); @@ -360,3 +377,31 @@ export class VariantAnalysisResultsManager extends DisposableObject { this.cachedResults.clear(); } } + +/** + * Builds a file link prefix from a repository URL supplied by a custom server. + * Returns `undefined`, so that locations are shown as plain text, if the URL + * is missing or is not an HTTP(S) URL without credentials. + */ +function createServerFileLinkPrefix( + repositoryUrl: string | undefined, + sha: string, +): string | undefined { + let url: URL; + try { + url = new URL(repositoryUrl ?? ""); + } catch { + return undefined; + } + if ( + !["http:", "https:"].includes(url.protocol) || + url.username || + url.password + ) { + return undefined; + } + url.search = ""; + url.hash = ""; + url.pathname = `${url.pathname.replace(/\/+$/, "")}/blob/${encodeURIComponent(sha)}`; + return url.toString(); +} 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 44fce971ad7..7ef2f3d5945 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 @@ -48,6 +48,21 @@ describe(mapVariantAnalysisFromSubmission.name, () => { ).toBeUndefined(); }); + it("preserves repository source URLs", () => { + const response = createMockApiResponse("succeeded"); + response.scanned_repositories![0].repository.html_url = + "https://source.example.com/org/repo"; + const run = mapVariantAnalysisFromSubmission(mockSubmission, response, []); + expect(run.scannedRepos?.[0].repository.htmlUrl).toBe( + response.scanned_repositories![0].repository.html_url, + ); + const task = createMockVariantAnalysisRepoTask(); + task.repository.html_url = "https://source.example.com/org/repo"; + expect(mapVariantAnalysisRepositoryTask(task).repository.htmlUrl).toBe( + task.repository.html_url, + ); + }); + it("should map an API response and return a variant analysis", () => { const result = mapVariantAnalysisFromSubmission( mockSubmission, diff --git a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-results-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-results-manager.test.ts index 842dbaa29e0..97754310c85 100644 --- a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-results-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-results-manager.test.ts @@ -303,6 +303,54 @@ describe(VariantAnalysisResultsManager.name, () => { ); }); + it("links GitHub results to the analyzed commit", async () => { + const result = await variantAnalysisResultsManager.loadResults( + variantAnalysisId, + variantAnalysisStoragePath, + dummyRepoTask.repository.fullName, + ); + expect(result.interpretedResults?.[0].fileLink.fileLinkPrefix).toBe( + `https://github.com/${dummyRepoTask.repository.fullName}/blob/${dummyRepoTask.databaseCommitSha}`, + ); + }); + + it.each([ + [ + "https://source.example.com/scm/org/repo", + "abc123", + "https://source.example.com/scm/org/repo/blob/abc123", + ], + [ + "https://source.example.com/scm/org/repo/", + undefined, + "https://source.example.com/scm/org/repo/blob/HEAD", + ], + [undefined, "abc123", undefined], + ["javascript:alert(1)", "abc123", undefined], + ["not a URL", "abc123", undefined], + ])( + "links server results through the repository URL %s at %s", + async (htmlUrl, databaseCommitSha, expectedPrefix) => { + dummyRepoTask.repository.htmlUrl = htmlUrl; + dummyRepoTask.databaseCommitSha = databaseCommitSha; + dummyRepoTask.sourceLocationPrefix = undefined; + await outputJson( + join(repoTaskStorageDirectory, "repo_task.json"), + dummyRepoTask, + ); + const result = await variantAnalysisResultsManager.loadResults( + variantAnalysisId, + variantAnalysisStoragePath, + dummyRepoTask.repository.fullName, + { serverUrl: "https://mrva.example.com" }, + ); + expect(result.interpretedResults?.length).toBeGreaterThan(0); + for (const alert of result.interpretedResults ?? []) { + expect(alert.fileLink.fileLinkPrefix).toBe(expectedPrefix); + } + }, + ); + it("should return the results when not cached", async () => { await expect( variantAnalysisResultsManager.loadResults( diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts index 07f98d191b5..bbcaebe86e6 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts @@ -111,6 +111,8 @@ describe("write and read", () => { it("round-trips custom-server run data without changing legacy history", async () => { const serverRun = createMockVariantAnalysisHistoryItem({}); serverRun.variantAnalysis.serverUrl = "https://mrva.example.com"; + serverRun.variantAnalysis.scannedRepos![0].repository.htmlUrl = + "https://source.example.com/org/repo"; const legacyRun = createMockVariantAnalysisHistoryItem({}); const historyPath = join(tmpDir.name, "server-and-legacy-history.json"); await writeQueryHistoryToFile([serverRun, legacyRun], historyPath); From 3d429eb523e9d529e6816ac950b51f757a991c2f Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 06/18] Adding custom-server support: Export results only as Markdown Results from a custom server must not be uploaded to GitHub, so the export picker offers only local Markdown for server runs. Category: Adding custom-server support. The options change only for runs with a serverUrl; GitHub runs get the same two options as before. --- .../src/variant-analysis/export-results.ts | 11 ++++-- .../variant-analysis/custom-server.test.ts | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts diff --git a/extensions/ql-vscode/src/variant-analysis/export-results.ts b/extensions/ql-vscode/src/variant-analysis/export-results.ts index 17d1997a47d..4096c3513d0 100644 --- a/extensions/ql-vscode/src/variant-analysis/export-results.ts +++ b/extensions/ql-vscode/src/variant-analysis/export-results.ts @@ -69,7 +69,7 @@ export async function exportVariantAnalysisResults( message: "Determining export format", }); - const exportFormat = await determineExportFormat(); + const exportFormat = await determineExportFormat(variantAnalysis); if (!exportFormat) { return; } @@ -200,7 +200,9 @@ async function exportVariantAnalysisAnalysisResults( /** * Determines the format in which to export the results, from the given export options. */ -async function determineExportFormat(): Promise<"gist" | "local" | undefined> { +async function determineExportFormat( + variantAnalysis: VariantAnalysis, +): Promise<"gist" | "local" | undefined> { const gistOption = { label: "$(ports-open-browser-icon) Create Gist (GitHub)", }; @@ -209,7 +211,10 @@ async function determineExportFormat(): Promise<"gist" | "local" | undefined> { }; const exportFormat = await window.showQuickPick( - [gistOption, localMarkdownOption], + // Results from a custom server must not be uploaded to GitHub. + variantAnalysis.serverUrl + ? [localMarkdownOption] + : [gistOption, localMarkdownOption], { placeHolder: "Select export format", canPickMany: false, diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts new file mode 100644 index 00000000000..29f9bb318af --- /dev/null +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -0,0 +1,39 @@ +import { window } from "vscode"; +import type { AppCommandManager } from "../../../../src/common/commands"; +import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; +import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; +import { testCredentialsWithStub } from "../../../factories/authentication"; +import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis"; +import { mockedObject } from "../../utils/mocking.helpers"; + +const SERVER_URL = "https://mrva.example.com"; + +describe("exporting custom-server results", () => { + it.each([undefined, SERVER_URL])( + "only offers a gist for GitHub runs (server: %s)", + async (serverUrl) => { + const showQuickPick = jest + .spyOn(window, "showQuickPick") + .mockResolvedValue(undefined); + const run = { ...createMockVariantAnalysis({}), serverUrl }; + + await expect( + exportVariantAnalysisResults( + mockedObject({ + tryGetVariantAnalysis: () => run, + getRepoStates: () => [], + }), + run.id, + undefined, + mockedObject({}), + testCredentialsWithStub(), + ), + ).rejects.toThrow("No export format selected"); + + const [items] = showQuickPick.mock.calls[0] as [Array<{ label: string }>]; + expect(items.some((item) => item.label.includes("Gist"))).toBe( + !serverUrl, + ); + }, + ); +}); From 06f27cef68ca5c9b5a13f45f801f29e9c3b3172b Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 07/18] Adding custom-server support: Stop runs without a workflow Server runs have no GitHub Actions workflow run. For them, allow cancellation without a workflow run ID, enable Stop query, show the results instead of the loading screen, and, when a running analysis is removed from query history, ask in a modal dialog whether to stop it on the server instead of linking to GitHub Actions. Category: Adding custom-server support. Each change is an early branch or an added `!serverUrl &&` condition, so runs without a serverUrl take the original code paths. --- .../query-history/query-history-manager.ts | 19 ++++++++- .../variant-analysis-manager.ts | 2 +- .../view/variant-analysis/VariantAnalysis.tsx | 1 + .../VariantAnalysisHeader.tsx | 4 +- .../__tests__/VariantAnalysis.spec.tsx | 17 ++++++++ .../variant-analysis-manager.test.ts | 16 +++++++ .../query-history-manager.test.ts | 42 +++++++++++++++++++ 7 files changed, 98 insertions(+), 3 deletions(-) diff --git a/extensions/ql-vscode/src/query-history/query-history-manager.ts b/extensions/ql-vscode/src/query-history/query-history-manager.ts index 6087816778f..ffd39e1275d 100644 --- a/extensions/ql-vscode/src/query-history/query-history-manager.ts +++ b/extensions/ql-vscode/src/query-history/query-history-manager.ts @@ -623,12 +623,29 @@ export class QueryHistoryManager extends DisposableObject { if (!response) { return; } + // Runs on a custom server can be stopped from here, so offer to do that. + if ( + item.variantAnalysis.serverUrl && + (await showBinaryChoiceDialog( + "This variant analysis is still running on the server. Do you also want to stop it?", + true, + "Stop", + "Keep running", + )) + ) { + await this.variantAnalysisManager.cancelVariantAnalysis( + item.variantAnalysis.id, + ); + } } this.treeDataProvider.remove(item); void this.app.logger.log(`Deleted ${this.labelProvider.getLabel(item)}.`); - if (item.status === QueryStatus.InProgress) { + if ( + item.status === QueryStatus.InProgress && + !item.variantAnalysis.serverUrl + ) { await this.showToastWithWorkflowRunLink(item); } 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 03f1675bb9b..963a55d1dd4 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -928,7 +928,7 @@ export class VariantAnalysisManager throw new Error(`No variant analysis with id: ${variantAnalysisId}`); } - if (!variantAnalysis.actionsWorkflowRunId) { + if (!variantAnalysis.serverUrl && !variantAnalysis.actionsWorkflowRunId) { throw new Error( `No workflow run id for variant analysis with id: ${variantAnalysis.id}`, ); diff --git a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx index 3ac0599ae6c..41c11ab26d9 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx @@ -151,6 +151,7 @@ export function VariantAnalysis({ if ( variantAnalysis === undefined || (variantAnalysis.status === VariantAnalysisStatus.InProgress && + !variantAnalysis.serverUrl && variantAnalysis.actionsWorkflowRunId === undefined) ) { return ; diff --git a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx index 6f9ad00ad33..1c75c8806a4 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx @@ -159,7 +159,9 @@ export const VariantAnalysisHeader = ({ onViewAutofixesClick={onViewAutofixesClick} onCopyRepositoryListClick={onCopyRepositoryListClick} onExportResultsClick={onExportResultsClick} - stopQueryDisabled={!variantAnalysis.actionsWorkflowRunId} + stopQueryDisabled={ + !variantAnalysis.serverUrl && !variantAnalysis.actionsWorkflowRunId + } exportResultsDisabled={!hasDownloadedRepos} copyRepositoryListDisabled={!hasReposWithResults} viewAutofixesDisabled={!hasReposWithResults} diff --git a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx index 3279ed446d2..4a37a2aea3a 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx @@ -34,6 +34,23 @@ describe(VariantAnalysis.name, () => { ).toBeInTheDocument(); }); + it("renders a server run without a workflow ID, with Stop enabled", () => { + render({ + variantAnalysis: { + ...createMockVariantAnalysis({ + status: VariantAnalysisStatus.InProgress, + }), + serverUrl: "https://mrva.example.com", + actionsWorkflowRunId: undefined, + }, + }); + + expect( + screen.queryByText("We are getting everything ready"), + ).not.toBeInTheDocument(); + expect(screen.getByText("Stop query")).toBeEnabled(); + }); + it("renders an analysis where there were no repos to analyse", () => { const variantAnalysis = createMockVariantAnalysis({ status: VariantAnalysisStatus.Failed, diff --git a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts index 850e221d84f..98c3e0341e5 100644 --- a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts @@ -3,6 +3,7 @@ import { commands, env, window, workspace } from "vscode"; import { extLogger } from "../../../../src/common/logging/vscode"; import * as ghApiClient from "../../../../src/variant-analysis/gh-api/gh-api-client"; import * as ghActionsApiClient from "../../../../src/variant-analysis/gh-api/gh-actions-api-client"; +import * as variantAnalysisApi from "../../../../src/variant-analysis/variant-analysis-api"; import { ensureDir, outputJson, @@ -654,6 +655,21 @@ describe("Variant Analysis Manager", () => { ); }); + it("allows custom-server cancellation without an Actions workflow run ID", async () => { + const cancel = jest + .spyOn(variantAnalysisApi, "cancelVariantAnalysis") + .mockResolvedValue(undefined); + const serverRun = { + ...variantAnalysis, + serverUrl: "https://mrva.example.com", + actionsWorkflowRunId: undefined, + }; + await variantAnalysisManager.onVariantAnalysisUpdated(serverRun); + await variantAnalysisManager.cancelVariantAnalysis(serverRun.id); + expect(cancel).toHaveBeenCalledWith(app.credentials, serverRun); + expect(mockCancelVariantAnalysis).not.toHaveBeenCalled(); + }); + it("should set the status to canceling", async () => { await variantAnalysisManager.cancelVariantAnalysis(variantAnalysis.id); diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts index 38eb471c918..99dd7a1f375 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts @@ -408,6 +408,48 @@ describe("QueryHistoryManager", () => { expect(showInformationMessageWithActionSpy).toHaveBeenCalled(); }); + it.each([true, false])( + "asks whether to stop a server run before removing it (stop: %s)", + async (shouldStop) => { + toDelete.variantAnalysis.serverUrl = "https://mrva.example.com"; + showBinaryChoiceDialogSpy + .mockResolvedValueOnce(true) + .mockResolvedValueOnce(shouldStop); + + await queryHistoryManager.handleRemoveHistoryItem([toDelete]); + + expect(showBinaryChoiceDialogSpy).toHaveBeenLastCalledWith( + "This variant analysis is still running on the server. Do you also want to stop it?", + true, + "Stop", + "Keep running", + ); + expect(cancelVariantAnalysisSpy).toHaveBeenCalledTimes( + shouldStop ? 1 : 0, + ); + expect( + showInformationMessageWithActionSpy, + ).not.toHaveBeenCalled(); + expect( + queryHistoryManager.treeDataProvider.allHistory, + ).not.toContain(toDelete); + }, + ); + + it("keeps a server run in history if stopping it fails", async () => { + toDelete.variantAnalysis.serverUrl = "https://mrva.example.com"; + cancelVariantAnalysisSpy.mockRejectedValue( + new Error("Server unavailable"), + ); + + await expect( + queryHistoryManager.handleRemoveHistoryItem([toDelete]), + ).rejects.toThrow("Server unavailable"); + expect(queryHistoryManager.treeDataProvider.allHistory).toContain( + toDelete, + ); + }); + describe("when you choose 'No' in the 'Are you sure?' modal", () => { beforeEach(async () => { showBinaryChoiceDialogSpy.mockResolvedValue(false); From 3a589b81a2f1a7feed29c51f3127ea45c322a8e8 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 08/18] Adding custom-server support: Read server settings Add the hidden codeQL.variantAnalysis.serverUrl and serverTokenProvider settings. They are read only from user settings, so a workspace cannot redirect queries or tokens, and an invalid server URL throws instead of falling back to GitHub. The test configuration mock gains inspect(), which reading user settings needs. Category: Adding custom-server support. Only new functions are added, and no production code calls them until the switching commits. --- extensions/ql-vscode/src/config.ts | 102 ++++++++++++++++++ .../variant-analysis/custom-server.test.ts | 69 +++++++++++- .../test/vscode-tests/test-config.ts | 14 ++- 3 files changed, 182 insertions(+), 3 deletions(-) diff --git a/extensions/ql-vscode/src/config.ts b/extensions/ql-vscode/src/config.ts index aabd1061b92..69f2eac3fce 100644 --- a/extensions/ql-vscode/src/config.ts +++ b/extensions/ql-vscode/src/config.ts @@ -54,6 +54,15 @@ export class Setting { .get(this.name)!; } + getGlobalValue(): T | undefined { + if (this.parent === undefined) { + throw new Error("Cannot get the value of a root setting."); + } + return workspace + .getConfiguration(this.parent.qualifiedName) + .inspect(this.name)?.globalValue; + } + updateValue(value: T, target: ConfigurationTarget): Thenable { if (this.parent === undefined) { throw new Error("Cannot update the value of a root setting."); @@ -605,6 +614,99 @@ export async function setRemoteControllerRepo(repo: string | undefined) { await REMOTE_CONTROLLER_REPO.updateValue(repo, ConfigurationTarget.Global); } +/** + * INTERNAL AND UNSUPPORTED: these settings are intentionally not declared in + * package.json and are not documented publicly. See + * docs/variant-analysis-custom-server.md. + * + * `serverUrl` points variant analysis at a self-hosted server that implements + * the GitHub variant analysis REST endpoints, instead of GitHub. + * `serverTokenProvider` is the ID of an installed extension that supplies the + * bearer token for that server. + * + * Both are only read from user or machine (global) settings, so a workspace + * cannot redirect queries or tokens. + */ +const VARIANT_ANALYSIS_SERVER_URL = new Setting( + "serverUrl", + VARIANT_ANALYSIS_SETTING, +); +const VARIANT_ANALYSIS_SERVER_TOKEN_PROVIDER = new Setting( + "serverTokenProvider", + VARIANT_ANALYSIS_SETTING, +); + +export interface VariantAnalysisServerConfig { + /** The server's base URL, always `https:` and without a trailing slash. */ + url: string; + /** ID of the extension that provides the bearer token. */ + tokenProviderExtensionId: string | undefined; +} + +/** + * Reads a hidden setting from user settings only. Returns `undefined` if it is + * unset or blank, and throws if it is not a string. + */ +function getGlobalStringSetting(setting: Setting): string | undefined { + const value = setting.getGlobalValue(); + if (value === undefined || value === null) { + return undefined; + } + if (typeof value !== "string") { + throw new Error(`${setting.qualifiedName} must be a string.`); + } + return value.trim() || undefined; +} + +/** + * Returns the custom variant analysis server configuration, or `undefined` + * if variant analysis should use GitHub. Throws if the server URL is invalid, + * rather than silently falling back to GitHub. + */ +export function getVariantAnalysisServerConfig(): + VariantAnalysisServerConfig | undefined { + const rawUrl = getGlobalStringSetting(VARIANT_ANALYSIS_SERVER_URL); + if (!rawUrl) { + return undefined; + } + + let url: URL; + try { + url = new URL(rawUrl); + } catch { + throw new Error( + `Invalid ${VARIANT_ANALYSIS_SERVER_URL.qualifiedName} setting: ${rawUrl}`, + ); + } + // A bare "?" or "#" leaves `search` and `hash` empty, so check the raw value. + if ( + url.protocol !== "https:" || + url.username || + url.password || + /[?#]/.test(rawUrl) + ) { + throw new Error( + `${VARIANT_ANALYSIS_SERVER_URL.qualifiedName} must be an https URL without credentials, a query string, or a fragment.`, + ); + } + + return { + url: url.toString().replace(/\/+$/, ""), + tokenProviderExtensionId: getGlobalStringSetting( + VARIANT_ANALYSIS_SERVER_TOKEN_PROVIDER, + ), + }; +} + +/** Whether a server URL is set. A malformed value counts, so it fails closed. */ +export function isVariantAnalysisServerConfigured(): boolean { + try { + return getGlobalStringSetting(VARIANT_ANALYSIS_SERVER_URL) !== undefined; + } catch { + return true; + } +} + export interface VariantAnalysisConfig { controllerRepo: string | undefined; showSystemDefinedRepositoryLists: boolean; diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 29f9bb318af..9ffbbc62478 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -1,5 +1,9 @@ -import { window } from "vscode"; +import { ConfigurationTarget, window, workspace } from "vscode"; import type { AppCommandManager } from "../../../../src/common/commands"; +import { + getVariantAnalysisServerConfig, + isVariantAnalysisServerConfigured, +} from "../../../../src/config"; import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; import { testCredentialsWithStub } from "../../../factories/authentication"; @@ -7,6 +11,17 @@ import { createMockVariantAnalysis } from "../../../factories/variant-analysis/s import { mockedObject } from "../../utils/mocking.helpers"; const SERVER_URL = "https://mrva.example.com"; +const PROVIDER_ID = "example.token-provider"; + +async function setSetting( + name: string, + value: unknown, + target = ConfigurationTarget.Global, +) { + await workspace + .getConfiguration("codeQL.variantAnalysis") + .update(name, value, target); +} describe("exporting custom-server results", () => { it.each([undefined, SERVER_URL])( @@ -37,3 +52,55 @@ describe("exporting custom-server results", () => { }, ); }); + +describe("variant analysis server settings", () => { + it.each([undefined, null, " "])( + "is not configured when the URL is %p", + async (url) => { + await setSetting("serverUrl", url); + expect(getVariantAnalysisServerConfig()).toBeUndefined(); + expect(isVariantAnalysisServerConfigured()).toBe(false); + }, + ); + + it("reads the server from user settings, keeping a base path", async () => { + await setSetting("serverUrl", `${SERVER_URL}/api/`); + await setSetting("serverTokenProvider", PROVIDER_ID); + expect(getVariantAnalysisServerConfig()).toEqual({ + url: `${SERVER_URL}/api`, + tokenProviderExtensionId: PROVIDER_ID, + }); + }); + + it("ignores workspace settings", async () => { + for (const target of [ + ConfigurationTarget.Workspace, + ConfigurationTarget.WorkspaceFolder, + ]) { + await setSetting("serverUrl", SERVER_URL, target); + await setSetting("serverTokenProvider", PROVIDER_ID, target); + } + expect(isVariantAnalysisServerConfigured()).toBe(false); + + await setSetting("serverUrl", SERVER_URL); + expect(getVariantAnalysisServerConfig()).toEqual({ + url: SERVER_URL, + tokenProviderExtensionId: undefined, + }); + }); + + it.each([ + "http://mrva.example.com", + "https://user:pw@mrva.example.com", + `${SERVER_URL}/?environment=test`, + `${SERVER_URL}/#fragment`, + `${SERVER_URL}/?`, + `${SERVER_URL}/#`, + "not a URL", + 42, + ])("rejects %p but still counts it as configured", async (url) => { + await setSetting("serverUrl", url); + expect(() => getVariantAnalysisServerConfig()).toThrow(); + expect(isVariantAnalysisServerConfigured()).toBe(true); + }); +}); diff --git a/extensions/ql-vscode/test/vscode-tests/test-config.ts b/extensions/ql-vscode/test/vscode-tests/test-config.ts index 04b2517c429..3d1f685af15 100644 --- a/extensions/ql-vscode/test/vscode-tests/test-config.ts +++ b/extensions/ql-vscode/test/vscode-tests/test-config.ts @@ -227,8 +227,18 @@ export const beforeEachAction = async () => { has(key: string) { return configuration.has(section, key); }, - inspect(_key: string) { - throw new Error("inspect is not supported in tests"); + inspect(key: string) { + const valueFor = (target: ConfigurationTarget) => + configuration.getConfiguration(target).get(section, key); + return { + key: section ? `${section}.${key}` : key, + defaultValue: defaultConfiguration.get(section, key), + globalValue: valueFor(ConfigurationTarget.Global), + workspaceValue: valueFor(ConfigurationTarget.Workspace), + workspaceFolderValue: valueFor( + ConfigurationTarget.WorkspaceFolder, + ), + }; }, async update( key: string, From 3d66e1ae975079601e7d6fae4c0a867c5ca366d8 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 09/18] Adding custom-server support: Add the server client Add requestVariantAnalysisServer, which sends variant analysis requests to the configured server. It gets a bearer token from the configured token provider extension, waits at most 30 seconds for the provider, refuses redirects, and reports HTTP errors as RequestError, so callers can handle statuses as they do for GitHub. Category: Adding custom-server support. The module is new, and no production code calls it yet. --- .../gh-api/variant-analysis-server-client.ts | 158 ++++++++++++++++++ .../variant-analysis/custom-server.test.ts | 129 +++++++++++++- 2 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts diff --git a/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts b/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts new file mode 100644 index 00000000000..e987e501c5d --- /dev/null +++ b/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts @@ -0,0 +1,158 @@ +import { extensions } from "vscode"; +import { RequestError } from "@octokit/request-error"; +import type { VariantAnalysisServerConfig } from "../../config"; + +/** + * INTERNAL AND UNSUPPORTED: client for a self-hosted variant analysis server + * that implements the GitHub variant analysis REST endpoints. See + * docs/variant-analysis-custom-server.md. + * + * The export that the configured token provider extension must implement. + */ +export interface VariantAnalysisServerTokenProvider { + getVariantAnalysisServerToken(serverUrl: string): Promise; +} + +const MAX_ERROR_BODY_LENGTH = 500; +const TOKEN_PROVIDER_TIMEOUT_MS = 30_000; + +/** + * Activates the configured token provider extension and runs `action` with its + * exports, failing if the provider does not respond within 30 seconds. + */ +async function withVariantAnalysisServerProvider( + server: VariantAnalysisServerConfig, + action: (provider: Partial) => Promise, +): Promise { + let timeout: ReturnType | undefined; + try { + return await Promise.race([ + activateProvider(server).then(action), + new Promise((_, reject) => { + timeout = setTimeout(() => { + reject( + new Error( + `The variant analysis token provider '${server.tokenProviderExtensionId}' did not respond within 30 seconds. Complete sign-in in the provider extension and retry.`, + ), + ); + }, TOKEN_PROVIDER_TIMEOUT_MS); + }), + ]); + } finally { + clearTimeout(timeout); + } +} + +async function activateProvider( + server: VariantAnalysisServerConfig, +): Promise> { + const extensionId = server.tokenProviderExtensionId; + if (!extensionId) { + throw new Error( + "No token provider is configured for the variant analysis server. Set codeQL.variantAnalysis.serverTokenProvider in your user settings.", + ); + } + + const extension = + extensions.getExtension>( + extensionId, + ); + if (!extension) { + throw new Error( + `The variant analysis server token provider extension '${extensionId}' is not installed.`, + ); + } + + return extension.isActive ? extension.exports : await extension.activate(); +} + +async function getToken(server: VariantAnalysisServerConfig): Promise { + return withVariantAnalysisServerProvider(server, async (provider) => { + if (typeof provider?.getVariantAnalysisServerToken !== "function") { + throw new Error( + `The extension '${server.tokenProviderExtensionId}' does not provide a variant analysis server token.`, + ); + } + + const token = await provider.getVariantAnalysisServerToken(server.url); + if (typeof token !== "string" || !token) { + throw new Error( + `The extension '${server.tokenProviderExtensionId}' did not return a variant analysis server token. Check that you are signed in.`, + ); + } + return token; + }); +} + +/** + * Sends a request to the variant analysis server. Neither the request body + * nor the token is logged or included in errors. Errors are `RequestError`s, + * so callers can handle HTTP statuses the same way as for GitHub. + */ +export async function requestVariantAnalysisServer( + server: VariantAnalysisServerConfig, + method: "GET" | "POST", + path: string, + body?: unknown, + allowEmptyResponse = false, +): Promise { + const token = await getToken(server); + const url = `${server.url}${path}`; + + const response = await fetch(url, { + redirect: "error", + method, + headers: { + Accept: "application/json", + Authorization: `Bearer ${token}`, + ...(body === undefined ? {} : { "Content-Type": "application/json" }), + }, + body: body === undefined ? undefined : JSON.stringify(body), + }); + + const text = await response.text(); + let data: unknown = undefined; + if (text) { + try { + data = JSON.parse(text); + } catch (cause) { + if (response.ok) { + throw new Error("Variant analysis server returned invalid JSON.", { + cause, + }); + } + data = text; + } + } + + if (!response.ok) { + const authenticationMessage = + response.status === 401 || response.status === 403 + ? ` Re-authenticate using the token provider '${server.tokenProviderExtensionId}' and retry.` + : ""; + throw new RequestError( + `Variant analysis server request failed: ${method} ${path}: ${response.status} ${response.statusText}${describeErrorBody(data)}${authenticationMessage}`, + response.status, + { request: { method, url, headers: {} } }, + ); + } + + if (!text && !allowEmptyResponse) { + throw new Error("Variant analysis server returned an empty response."); + } + + return data as T; +} + +function describeErrorBody(data: unknown): string { + const message = + typeof data === "object" && + data !== null && + "message" in data && + typeof data.message === "string" + ? data.message + : typeof data === "string" + ? data + : undefined; + return message ? `: ${message.slice(0, MAX_ERROR_BODY_LENGTH)}` : ""; +} diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 9ffbbc62478..da50f8c2b05 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -1,13 +1,17 @@ -import { ConfigurationTarget, window, workspace } from "vscode"; +import { ConfigurationTarget, extensions, window, workspace } from "vscode"; +import type { Extension } from "vscode"; import type { AppCommandManager } from "../../../../src/common/commands"; import { getVariantAnalysisServerConfig, isVariantAnalysisServerConfigured, } from "../../../../src/config"; import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; +import type { VariantAnalysisServerTokenProvider } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; +import { requestVariantAnalysisServer } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; import { testCredentialsWithStub } from "../../../factories/authentication"; import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis"; +import type { DeepPartial } from "../../utils/mocking.helpers"; import { mockedObject } from "../../utils/mocking.helpers"; const SERVER_URL = "https://mrva.example.com"; @@ -23,6 +27,30 @@ async function setSetting( .update(name, value, target); } +function mockProvider( + extension: DeepPartial>, +) { + return jest + .spyOn(extensions, "getExtension") + .mockReturnValue( + mockedObject>(extension), + ); +} + +let fetchSpy: jest.SpiedFunction; +let getVariantAnalysisServerToken: jest.Mock< + Promise, + [string] +>; + +beforeEach(() => { + fetchSpy = jest + .spyOn(global, "fetch") + .mockImplementation(async () => new Response(JSON.stringify({ id: 123 }))); + getVariantAnalysisServerToken = jest.fn().mockResolvedValue("the-token"); + mockProvider({ isActive: true, exports: { getVariantAnalysisServerToken } }); +}); + describe("exporting custom-server results", () => { it.each([undefined, SERVER_URL])( "only offers a gist for GitHub runs (server: %s)", @@ -104,3 +132,102 @@ describe("variant analysis server settings", () => { expect(isVariantAnalysisServerConfigured()).toBe(true); }); }); + +describe("variant analysis server client", () => { + const server = { url: SERVER_URL, tokenProviderExtensionId: PROVIDER_ID }; + + it("sends JSON with the provider's token and refuses redirects", async () => { + await expect( + requestVariantAnalysisServer(server, "POST", "/analyses", { + language: "java", + }), + ).resolves.toEqual({ id: 123 }); + + expect(getVariantAnalysisServerToken).toHaveBeenCalledWith(SERVER_URL); + expect(fetchSpy).toHaveBeenCalledWith(`${SERVER_URL}/analyses`, { + redirect: "error", + method: "POST", + headers: { + Accept: "application/json", + Authorization: "Bearer the-token", + "Content-Type": "application/json", + }, + body: '{"language":"java"}', + }); + }); + + it("fails without a usable token, without sending a request", async () => { + const request = (tokenProviderExtensionId = PROVIDER_ID) => + requestVariantAnalysisServer( + { ...server, tokenProviderExtensionId }, + "GET", + "/", + ); + + await expect(request("")).rejects.toThrow(/No token provider/); + getVariantAnalysisServerToken.mockResolvedValue(undefined); + await expect(request()).rejects.toThrow(/did not return .* token/); + mockProvider({ isActive: true, exports: {} }); + await expect(request()).rejects.toThrow(/does not provide .* token/); + jest.spyOn(extensions, "getExtension").mockReturnValue(undefined); + await expect(request()).rejects.toThrow(/not installed/); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it.each(["activation", "token"])( + "gives up on a stalled provider %s after 30 seconds", + async (stage) => { + jest.useFakeTimers(); + try { + if (stage === "activation") { + mockProvider({ + isActive: false, + activate: () => new Promise(() => {}), + }); + } else { + getVariantAnalysisServerToken.mockReturnValue(new Promise(() => {})); + } + const rejected = expect( + requestVariantAnalysisServer(server, "GET", "/"), + ).rejects.toThrow(/did not respond within 30 seconds/); + await jest.advanceTimersByTimeAsync(30_000); + await rejected; + expect(fetchSpy).not.toHaveBeenCalled(); + expect(jest.getTimerCount()).toBe(0); + } finally { + jest.useRealTimers(); + } + }, + ); + + it.each([401, 403])( + "asks to re-authenticate on HTTP %s without retrying", + async (status) => { + fetchSpy.mockResolvedValue( + new Response(JSON.stringify({ message: "Token expired" }), { + status, + statusText: "Denied", + }), + ); + await expect( + requestVariantAnalysisServer(server, "GET", "/"), + ).rejects.toMatchObject({ + status, + message: expect.stringContaining( + `${status} Denied: Token expired Re-authenticate using the token provider '${PROVIDER_ID}'`, + ), + }); + expect(fetchSpy).toHaveBeenCalledTimes(1); + }, + ); + + it.each(["not JSON", ""])( + "rejects a malformed successful response: %p", + async (body) => { + fetchSpy.mockResolvedValue(new Response(body)); + await expect( + requestVariantAnalysisServer(server, "GET", "/"), + ).rejects.toThrow(/invalid JSON|empty response/); + }, + ); +}); From 95887fad255a1ac6aea5ffc143d397e3df8c4cc8 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:38 +0100 Subject: [PATCH 10/18] Adding custom-server support: Open logs through the companion Server runs have no GitHub Actions logs. A token provider can now implement an optional openVariantAnalysisLogs(serverUrl, analysisId) hook. For server runs, View logs in the results view and in query history call that hook, after checking that the run belongs to the configured server. Category: Adding custom-server support. The manager and query history only add early branches for runs with a serverUrl. GitHub runs still open their workflow run URL. --- .../query-history/query-history-manager.ts | 7 +++ .../gh-api/variant-analysis-server-client.ts | 6 ++- .../variant-analysis/variant-analysis-api.ts | 45 ++++++++++++++++ .../variant-analysis-manager.ts | 6 +++ .../variant-analysis-manager.test.ts | 27 ++++++++++ .../query-history-manager.test.ts | 17 ++++++ .../variant-analysis/custom-server.test.ts | 54 +++++++++++++++++++ 7 files changed, 161 insertions(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/query-history/query-history-manager.ts b/extensions/ql-vscode/src/query-history/query-history-manager.ts index ffd39e1275d..dbed31d73e7 100644 --- a/extensions/ql-vscode/src/query-history/query-history-manager.ts +++ b/extensions/ql-vscode/src/query-history/query-history-manager.ts @@ -1061,6 +1061,13 @@ export class QueryHistoryManager extends DisposableObject { return; } + if (item.variantAnalysis.serverUrl) { + await this.variantAnalysisManager.openVariantAnalysisLogs( + item.variantAnalysis.id, + ); + return; + } + const actionsWorkflowRunUrl = getActionsWorkflowRunUrl(item); await this.app.commands.execute( diff --git a/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts b/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts index e987e501c5d..8da01612f1e 100644 --- a/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts +++ b/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts @@ -11,6 +11,10 @@ import type { VariantAnalysisServerConfig } from "../../config"; */ export interface VariantAnalysisServerTokenProvider { getVariantAnalysisServerToken(serverUrl: string): Promise; + openVariantAnalysisLogs?( + serverUrl: string, + analysisId: number, + ): Promise; } const MAX_ERROR_BODY_LENGTH = 500; @@ -20,7 +24,7 @@ const TOKEN_PROVIDER_TIMEOUT_MS = 30_000; * Activates the configured token provider extension and runs `action` with its * exports, failing if the provider does not respond within 30 seconds. */ -async function withVariantAnalysisServerProvider( +export async function withVariantAnalysisServerProvider( server: VariantAnalysisServerConfig, action: (provider: Partial) => Promise, ): Promise { diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts index 08861c4c7b4..c42a589cac4 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -1,4 +1,6 @@ import type { Credentials } from "../common/authentication"; +import type { VariantAnalysisServerConfig } from "../config"; +import { getVariantAnalysisServerConfig } from "../config"; import { cancelVariantAnalysis as cancelGitHubVariantAnalysis } from "./gh-api/gh-actions-api-client"; import { getVariantAnalysis as getGitHubVariantAnalysis, @@ -9,6 +11,7 @@ import type { VariantAnalysis as ApiVariantAnalysis, VariantAnalysisRepoTask as ApiVariantAnalysisRepoTask, } from "./gh-api/variant-analysis"; +import { withVariantAnalysisServerProvider } from "./gh-api/variant-analysis-server-client"; import type { VariantAnalysis, VariantAnalysisSubmission, @@ -53,3 +56,45 @@ export async function cancelVariantAnalysis( ): Promise { return cancelGitHubVariantAnalysis(credentials, variantAnalysis); } + +/** + * Returns the configured server for a run submitted to `serverUrl`, or + * `undefined` for a GitHub run while no server is configured. Throws if the + * run belongs to a different server than the configured one. + */ +function getServerForVariantAnalysis( + serverUrl: string | undefined, +): VariantAnalysisServerConfig | undefined { + const server = getVariantAnalysisServerConfig(); + if (server?.url !== serverUrl) { + throw new Error( + "This variant analysis belongs to a different server. Restore the server setting used to submit it before retrying.", + ); + } + return server; +} + +export async function openServerVariantAnalysisLogs( + run: VariantAnalysis, +): Promise { + const server = getServerForVariantAnalysis(run.serverUrl); + const openLogs = server && (await getServerLogOpener(server, run.id)); + if (!openLogs) { + throw new Error( + "The configured variant analysis provider does not support viewing logs.", + ); + } + await openLogs(); +} + +async function getServerLogOpener( + server: VariantAnalysisServerConfig, + analysisId: number, +): Promise<(() => Promise) | undefined> { + return withVariantAnalysisServerProvider(server, async (provider) => { + const openLogs = provider?.openVariantAnalysisLogs; + return typeof openLogs === "function" + ? () => openLogs.call(provider, server.url, analysisId) + : undefined; + }); +} 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 963a55d1dd4..b1954829116 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -4,6 +4,7 @@ import { cancelVariantAnalysis, submitVariantAnalysis, getVariantAnalysisRepo, + openServerVariantAnalysisLogs, } from "./variant-analysis-api"; import type { VariantAnalysis as ApiVariantAnalysis } from "./gh-api/variant-analysis"; import type { @@ -960,6 +961,11 @@ export class VariantAnalysisManager throw new Error(`No variant analysis with id: ${variantAnalysisId}`); } + if (variantAnalysis.serverUrl) { + await openServerVariantAnalysisLogs(variantAnalysis); + return; + } + const actionsWorkflowRunUrl = getActionsWorkflowRunUrl( variantAnalysis, this.config.githubUrl, diff --git a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts index 98c3e0341e5..ea06f904c17 100644 --- a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts @@ -655,6 +655,33 @@ describe("Variant Analysis Manager", () => { ); }); + it("delegates custom-server logs without requiring a workflow ID", async () => { + const openLogs = jest + .spyOn(variantAnalysisApi, "openServerVariantAnalysisLogs") + .mockResolvedValue(undefined); + const serverRun = { + ...variantAnalysis, + serverUrl: "https://mrva.example.com", + actionsWorkflowRunId: undefined, + }; + await variantAnalysisManager.onVariantAnalysisUpdated(serverRun); + await variantAnalysisManager.openVariantAnalysisLogs(serverRun.id); + expect(openLogs).toHaveBeenCalledWith(serverRun); + }); + + it("opens the GitHub workflow URL for GitHub logs", async () => { + const open = jest + .spyOn(commands, "executeCommand") + .mockResolvedValue(undefined); + await variantAnalysisManager.openVariantAnalysisLogs(variantAnalysis.id); + expect(open).toHaveBeenCalledWith( + "vscode.open", + expect.objectContaining({ + path: `/${variantAnalysis.controllerRepo.fullName}/actions/runs/${variantAnalysis.actionsWorkflowRunId}`, + }), + ); + }); + it("allows custom-server cancellation without an Actions workflow run ID", async () => { const cancel = jest .spyOn(variantAnalysisApi, "cancelVariantAnalysis") diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts index 99dd7a1f375..88eb14b2edc 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts @@ -79,6 +79,7 @@ describe("QueryHistoryManager", () => { onVariantAnalysisRemoved: jest.fn(), removeVariantAnalysis: jest.fn(), cancelVariantAnalysis: jest.fn(), + openVariantAnalysisLogs: jest.fn(), exportResults: jest.fn(), showView: jest.fn(), } as any as VariantAnalysisManager; @@ -148,6 +149,22 @@ describe("QueryHistoryManager", () => { } }); + describe("handleOpenOnGithub", () => { + it("delegates server runs to the variant analysis manager", async () => { + queryHistoryManager = await createMockQueryHistory(allHistory); + const item = variantAnalysisHistory[0]; + item.variantAnalysis.serverUrl = "https://mrva.example.com"; + await queryHistoryManager.handleOpenOnGithub(item); + expect( + variantAnalysisManagerStub.openVariantAnalysisLogs, + ).toHaveBeenCalledWith(item.variantAnalysis.id); + expect(executeCommand).not.toHaveBeenCalledWith( + "vscode.open", + expect.anything(), + ); + }); + }); + describe("handleItemClicked", () => { describe("single click", () => { describe("local query", () => { diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index da50f8c2b05..4b11e7a6df7 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -8,6 +8,7 @@ import { import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; import type { VariantAnalysisServerTokenProvider } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; import { requestVariantAnalysisServer } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; +import { openServerVariantAnalysisLogs } from "../../../../src/variant-analysis/variant-analysis-api"; import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; import { testCredentialsWithStub } from "../../../factories/authentication"; import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis"; @@ -27,6 +28,11 @@ async function setSetting( .update(name, value, target); } +async function configureServer() { + await setSetting("serverUrl", SERVER_URL); + await setSetting("serverTokenProvider", PROVIDER_ID); +} + function mockProvider( extension: DeepPartial>, ) { @@ -231,3 +237,51 @@ describe("variant analysis server client", () => { }, ); }); + +describe("opening server logs", () => { + let run: ReturnType; + let openVariantAnalysisLogs: jest.Mock; + + beforeEach(async () => { + await configureServer(); + run = { ...createMockVariantAnalysis({}), serverUrl: SERVER_URL }; + openVariantAnalysisLogs = jest.fn().mockResolvedValue(undefined); + mockProvider({ + isActive: false, + activate: jest.fn().mockResolvedValue({ + getVariantAnalysisServerToken, + openVariantAnalysisLogs, + }), + }); + }); + + it("activates the companion and delegates without requesting a token", async () => { + await openServerVariantAnalysisLogs(run); + expect(openVariantAnalysisLogs).toHaveBeenCalledWith(SERVER_URL, run.id); + expect(getVariantAnalysisServerToken).not.toHaveBeenCalled(); + expect(fetchSpy).not.toHaveBeenCalled(); + + openVariantAnalysisLogs.mockRejectedValue(new Error("Cannot open logs")); + await expect(openServerVariantAnalysisLogs(run)).rejects.toThrow( + "Cannot open logs", + ); + }); + + it("refuses runs from another server", async () => { + await setSetting("serverUrl", "https://other.example.com"); + await expect(openServerVariantAnalysisLogs(run)).rejects.toThrow( + "different server", + ); + expect(openVariantAnalysisLogs).not.toHaveBeenCalled(); + }); + + it("fails if the companion cannot open logs", async () => { + mockProvider({ + isActive: true, + exports: { getVariantAnalysisServerToken }, + }); + await expect(openServerVariantAnalysisLogs(run)).rejects.toThrow( + "does not support viewing logs", + ); + }); +}); From dc096ac01e2f45464212b33f43467f76e7dd463f Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:39 +0100 Subject: [PATCH 11/18] Adding custom-server support: Show log actions and failures Whether a server run has logs depends on the provider implementing the log hook. Discover that in the background, cache the result per server and provider, clear the cache when either setting changes, and refresh query history and the webviews when discovery finishes, so drawing never waits for the provider. Failed server runs show a failure message without the GitHub Actions wording, with a View logs link if supported. Category: Adding custom-server support. History context values, webview messages and failure messages change only for runs with a serverUrl. The new history context values are only used for server runs, and the menus list them next to the existing ones. The view-logs check moves into hasVariantAnalysisLogs, which gives the same result as before for runs without a serverUrl. The configuration watcher only reacts to the two hidden settings. --- extensions/ql-vscode/package.json | 12 +- extensions/ql-vscode/src/extension.ts | 2 + .../model-alerts/model-alerts-view.ts | 15 +- .../history-tree-data-provider.ts | 37 ++++ .../shared/variant-analysis.ts | 12 ++ .../variant-analysis/variant-analysis-api.ts | 78 +++++++++ .../variant-analysis/variant-analysis-view.ts | 16 +- .../src/view/model-alerts/ModelAlerts.tsx | 6 +- .../variant-analysis/FailureReasonAlert.tsx | 32 +++- .../view/variant-analysis/VariantAnalysis.tsx | 10 +- .../VariantAnalysisOutcomePanels.tsx | 6 +- .../__tests__/VariantAnalysis.spec.tsx | 18 ++ .../VariantAnalysisOutcomePanels.spec.tsx | 16 ++ .../history-tree-data-provider.test.ts | 44 +++++ .../variant-analysis/custom-server.test.ts | 160 +++++++++++++++++- 15 files changed, 445 insertions(+), 19 deletions(-) diff --git a/extensions/ql-vscode/package.json b/extensions/ql-vscode/package.json index 9bc4b725f66..2b13926e581 100644 --- a/extensions/ql-vscode/package.json +++ b/extensions/ql-vscode/package.json @@ -1245,12 +1245,12 @@ { "command": "codeQLQueryHistory.removeHistoryItemContextMenu", "group": "7_queryHistory@0", - "when": "viewItem == interpretedResultsItem || viewItem == rawResultsItem || viewItem == remoteResultsItem || viewItem == cancelledRemoteResultsItemWithoutLogs || viewItem == cancelledResultsItem || viewItem == cancelledRemoteResultsItem" + "when": "viewItem == interpretedResultsItem || viewItem == rawResultsItem || viewItem == remoteResultsItem || viewItem == remoteResultsItemWithoutLogs || viewItem == cancelledRemoteResultsItemWithoutLogs || viewItem == cancelledResultsItem || viewItem == cancelledRemoteResultsItem" }, { "command": "codeQLQueryHistory.removeHistoryItemContextInline", "group": "inline", - "when": "viewItem == interpretedResultsItem || viewItem == rawResultsItem || viewItem == remoteResultsItem || viewItem == cancelledRemoteResultsItemWithoutLogs || viewItem == cancelledResultsItem || viewItem == cancelledRemoteResultsItem" + "when": "viewItem == interpretedResultsItem || viewItem == rawResultsItem || viewItem == remoteResultsItem || viewItem == remoteResultsItemWithoutLogs || viewItem == cancelledRemoteResultsItemWithoutLogs || viewItem == cancelledResultsItem || viewItem == cancelledRemoteResultsItem" }, { "command": "codeQLQueryHistory.renameItem", @@ -1300,7 +1300,7 @@ { "command": "codeQLQueryHistory.exportResults", "group": "1_queryHistory@0", - "when": "view == codeQLQueryHistory && viewItem == remoteResultsItem" + "when": "view == codeQLQueryHistory && (viewItem == remoteResultsItem || viewItem == remoteResultsItemWithoutLogs)" }, { "command": "codeQLQueryHistory.viewCsvResults", @@ -1325,7 +1325,7 @@ { "command": "codeQLQueryHistory.cancel", "group": "9_qlCommands", - "when": "viewItem == inProgressResultsItem || viewItem == inProgressRemoteResultsItem" + "when": "viewItem == inProgressResultsItem || viewItem == inProgressRemoteResultsItem || viewItem == inProgressRemoteResultsItemWithoutLogs" }, { "command": "codeQLQueryHistory.openOnGithub", @@ -1335,12 +1335,12 @@ { "command": "codeQLQueryHistory.copyRepoList", "group": "1_queryHistory@1", - "when": "viewItem == remoteResultsItem" + "when": "viewItem == remoteResultsItem || viewItem == remoteResultsItemWithoutLogs" }, { "command": "codeQLQueryHistory.viewAutofixes", "group": "1_queryHistory@2", - "when": "viewItem == remoteResultsItem && config.codeQL.canary" + "when": "(viewItem == remoteResultsItem || viewItem == remoteResultsItemWithoutLogs) && config.codeQL.canary" }, { "command": "codeQLQueries.runLocalQueryFromQueriesPanel", diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index c72833f3e40..d5bd5ff8aae 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -110,6 +110,7 @@ import { LogScannerService } from "./log-insights/log-scanner-service"; import { VariantAnalysisView } from "./variant-analysis/variant-analysis-view"; import { VariantAnalysisViewSerializer } from "./variant-analysis/variant-analysis-view-serializer"; import { VariantAnalysisManager } from "./variant-analysis/variant-analysis-manager"; +import { watchVariantAnalysisLogCapabilities } from "./variant-analysis/variant-analysis-api"; import { createVariantAnalysisContentProvider } from "./variant-analysis/variant-analysis-content-provider"; import { VSCodeMockGitHubApiServer } from "./common/mock-gh-api/vscode/vscode-mock-gh-api-server"; import { VariantAnalysisResultsManager } from "./variant-analysis/variant-analysis-results-manager"; @@ -920,6 +921,7 @@ async function activateWithInstalledDistribution( ); await ensureDir(variantAnalysisStorageDir); const variantAnalysisConfig = new VariantAnalysisConfigListener(); + ctx.subscriptions.push(watchVariantAnalysisLogCapabilities()); const variantAnalysisResultsManager = new VariantAnalysisResultsManager( cliServer, variantAnalysisConfig, diff --git a/extensions/ql-vscode/src/model-editor/model-alerts/model-alerts-view.ts b/extensions/ql-vscode/src/model-editor/model-alerts/model-alerts-view.ts index beb08e1e83c..9a5eb89e366 100644 --- a/extensions/ql-vscode/src/model-editor/model-alerts/model-alerts-view.ts +++ b/extensions/ql-vscode/src/model-editor/model-alerts/model-alerts-view.ts @@ -22,6 +22,10 @@ import type { import type { AppEvent, AppEventEmitter } from "../../common/events"; import type { ModeledMethod } from "../modeled-method"; import type { MethodSignature } from "../method"; +import { + onDidChangeVariantAnalysisLogCapabilities, + withVariantAnalysisLogCapability, +} from "../../variant-analysis/variant-analysis-api"; export class ModelAlertsView extends AbstractWebview< ToModelAlertsMessage, @@ -31,6 +35,7 @@ export class ModelAlertsView extends AbstractWebview< public readonly onEvaluationRunStopClicked: AppEvent; private readonly onEvaluationRunStopClickedEventEmitter: AppEventEmitter; + private variantAnalysis?: VariantAnalysis; public constructor( app: App, @@ -42,6 +47,13 @@ export class ModelAlertsView extends AbstractWebview< super(app); this.registerToModelingEvents(); + this.push( + onDidChangeVariantAnalysisLogCapabilities(() => { + if (this.variantAnalysis?.serverUrl) { + void this.setVariantAnalysis(this.variantAnalysis); + } + }), + ); this.onEvaluationRunStopClickedEventEmitter = this.push( app.createEventEmitter(), @@ -124,13 +136,14 @@ export class ModelAlertsView extends AbstractWebview< public async setVariantAnalysis( variantAnalysis: VariantAnalysis, ): Promise { + this.variantAnalysis = variantAnalysis; if (!this.isShowingPanel) { return; } await this.postMessage({ t: "setVariantAnalysis", - variantAnalysis, + variantAnalysis: withVariantAnalysisLogCapability(variantAnalysis), }); } diff --git a/extensions/ql-vscode/src/query-history/history-tree-data-provider.ts b/extensions/ql-vscode/src/query-history/history-tree-data-provider.ts index 8d1f43dd62f..0a078fc32dc 100644 --- a/extensions/ql-vscode/src/query-history/history-tree-data-provider.ts +++ b/extensions/ql-vscode/src/query-history/history-tree-data-provider.ts @@ -7,6 +7,11 @@ import { getLanguage, getQueryId } from "./query-history-info"; import { QueryStatus } from "./query-status"; import type { HistoryItemLabelProvider } from "./history-item-label-provider"; import type { LanguageContextStore } from "../language-context-store"; +import type { VariantAnalysisHistoryItem } from "./variant-analysis-history-item"; +import { + canViewServerVariantAnalysisLogs, + onDidChangeVariantAnalysisLogCapabilities, +} from "../variant-analysis/variant-analysis-api"; export enum SortOrder { NameAsc = "NameAsc", @@ -68,6 +73,11 @@ export class HistoryTreeDataProvider private readonly languageContext: LanguageContextStore, ) { super(); + this.push( + onDidChangeVariantAnalysisLogCapabilities(() => + this._onDidChangeTreeData.fire(undefined), + ), + ); } async getTreeItem(element: QueryHistoryInfo): Promise { @@ -122,6 +132,9 @@ export class HistoryTreeDataProvider } private async getContextValue(element: QueryHistoryInfo): Promise { + if (element.t === "variant-analysis" && element.variantAnalysis.serverUrl) { + return getServerVariantAnalysisContextValue(element); + } switch (element.status) { case QueryStatus.InProgress: if (element.t === "local") { @@ -284,3 +297,27 @@ export class HistoryTreeDataProvider } } } + +/** + * Context values for runs on a custom server. Their logs come from the + * companion extension, so the "View logs" action depends on its support. + */ +function getServerVariantAnalysisContextValue( + element: VariantAnalysisHistoryItem, +): string { + const hasLogs = canViewServerVariantAnalysisLogs(element.variantAnalysis); + switch (element.status) { + case QueryStatus.InProgress: + return hasLogs + ? "inProgressRemoteResultsItem" + : "inProgressRemoteResultsItemWithoutLogs"; + case QueryStatus.Completed: + return hasLogs ? "remoteResultsItem" : "remoteResultsItemWithoutLogs"; + case QueryStatus.Failed: + return hasLogs + ? "cancelledRemoteResultsItem" + : "cancelledRemoteResultsItemWithoutLogs"; + default: + assertNever(element.status); + } +} 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 5632591bc62..f62238b4240 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts @@ -7,6 +7,8 @@ export interface VariantAnalysis { id: number; /** Custom server that created this run; absent for GitHub and legacy runs. */ serverUrl?: string; + /** Runtime capability supplied to views, not persisted in query history. */ + canViewLogs?: boolean; controllerRepo: Repository; language: QueryLanguage; query: { @@ -33,6 +35,16 @@ export interface VariantAnalysis { skippedRepos?: VariantAnalysisSkippedRepositories; } +/** Whether the "View logs" action is available for a run. */ +export function hasVariantAnalysisLogs( + variantAnalysis: VariantAnalysis, +): boolean { + if (variantAnalysis.serverUrl) { + return variantAnalysis.canViewLogs === true; + } + return variantAnalysis.actionsWorkflowRunId !== undefined; +} + export function parseVariantAnalysisQueryLanguage( language: string, ): QueryLanguage | undefined { diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts index c42a589cac4..667edc47d1e 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -1,4 +1,7 @@ +import { Disposable, EventEmitter, workspace } from "vscode"; import type { Credentials } from "../common/authentication"; +import { getErrorMessage } from "../common/helpers-pure"; +import { extLogger } from "../common/logging/vscode"; import type { VariantAnalysisServerConfig } from "../config"; import { getVariantAnalysisServerConfig } from "../config"; import { cancelVariantAnalysis as cancelGitHubVariantAnalysis } from "./gh-api/gh-actions-api-client"; @@ -98,3 +101,78 @@ async function getServerLogOpener( : undefined; }); } + +const logCapabilities = new Map(); +const logCapabilitiesChanged = new EventEmitter(); +export const onDidChangeVariantAnalysisLogCapabilities = + logCapabilitiesChanged.event; + +export function watchVariantAnalysisLogCapabilities(): Disposable { + logCapabilities.clear(); + return Disposable.from( + workspace.onDidChangeConfiguration((event) => { + if ( + event.affectsConfiguration("codeQL.variantAnalysis.serverUrl") || + event.affectsConfiguration("codeQL.variantAnalysis.serverTokenProvider") + ) { + logCapabilities.clear(); + logCapabilitiesChanged.fire(); + } + }), + new Disposable(() => logCapabilities.clear()), + ); +} + +/** + * Whether the companion extension can open logs for a server run. Discovery + * runs in the background: this returns `false` until it finishes, then + * `onDidChangeVariantAnalysisLogCapabilities` fires. + */ +export function canViewServerVariantAnalysisLogs( + run: VariantAnalysis, +): boolean { + let server: VariantAnalysisServerConfig | undefined; + try { + server = getVariantAnalysisServerConfig(); + } catch { + return false; + } + if ( + !run.serverUrl || + server?.url !== run.serverUrl || + !server.tokenProviderExtensionId + ) { + return false; + } + const key = JSON.stringify([server.url, server.tokenProviderExtensionId]); + const cached = logCapabilities.get(key); + if (cached) { + return cached.available; + } + const capability = { available: false }; + logCapabilities.set(key, capability); + void getServerLogOpener(server, run.id) + .then((openLogs) => { + capability.available = openLogs !== undefined; + }) + .catch((error: unknown) => { + void extLogger.log( + `Unable to discover variant analysis logs: ${getErrorMessage(error)}`, + ); + }) + .then(() => { + if (logCapabilities.get(key) === capability) { + logCapabilitiesChanged.fire(); + } + }); + return false; +} + +/** Adds the log capability to a server run before it is sent to a webview. */ +export function withVariantAnalysisLogCapability( + run: VariantAnalysis, +): VariantAnalysis { + return run.serverUrl + ? { ...run, canViewLogs: canViewServerVariantAnalysisLogs(run) } + : run; +} diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-view.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-view.ts index 555a3686e68..07a628664c5 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-view.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-view.ts @@ -22,6 +22,10 @@ import type { import { telemetryListener } from "../common/vscode/telemetry"; import { redactableError } from "../common/errors"; import { DataFlowPathsView } from "./data-flow-paths-view"; +import { + onDidChangeVariantAnalysisLogCapabilities, + withVariantAnalysisLogCapability, +} from "./variant-analysis-api"; import type { DataFlowPaths } from "./shared/data-flow-paths"; import type { App } from "../common/app"; import { @@ -47,6 +51,14 @@ export class VariantAnalysisView manager.registerView(this); this.dataFlowPathsView = new DataFlowPathsView(app); + this.push( + onDidChangeVariantAnalysisLogCapabilities(() => { + const run = this.manager.tryGetVariantAnalysis(this.variantAnalysisId); + if (run?.serverUrl) { + void this.updateView(run); + } + }), + ); } public async openView() { @@ -70,7 +82,7 @@ export class VariantAnalysisView await this.postMessage({ t: "setVariantAnalysis", - variantAnalysis, + variantAnalysis: withVariantAnalysisLogCapability(variantAnalysis), }); const panel = await this.getPanel(); @@ -212,7 +224,7 @@ export class VariantAnalysisView await this.postMessage({ t: "setVariantAnalysis", - variantAnalysis, + variantAnalysis: withVariantAnalysisLogCapability(variantAnalysis), }); await this.postMessage({ diff --git a/extensions/ql-vscode/src/view/model-alerts/ModelAlerts.tsx b/extensions/ql-vscode/src/view/model-alerts/ModelAlerts.tsx index 70169f0a9f4..58c68aef566 100644 --- a/extensions/ql-vscode/src/view/model-alerts/ModelAlerts.tsx +++ b/extensions/ql-vscode/src/view/model-alerts/ModelAlerts.tsx @@ -19,6 +19,7 @@ import { import type { ModelAlertsFilterSortState } from "../../model-editor/shared/model-alerts-filter-sort"; import type { ModeledMethod } from "../../model-editor/modeled-method"; import { useMessageFromExtension } from "../common/useMessageFromExtension"; +import { hasVariantAnalysisLogs } from "../../variant-analysis/shared/variant-analysis"; type Props = { initialViewState?: ModelAlertsViewState; @@ -118,8 +119,9 @@ export function ModelAlerts({ }); }; - const onViewLogsClick = - variantAnalysis.actionsWorkflowRunId === undefined ? undefined : openLogs; + const onViewLogsClick = hasVariantAnalysisLogs(variantAnalysis) + ? openLogs + : undefined; return ( <> diff --git a/extensions/ql-vscode/src/view/variant-analysis/FailureReasonAlert.tsx b/extensions/ql-vscode/src/view/variant-analysis/FailureReasonAlert.tsx index 1b6c631dbb7..5518d15228a 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/FailureReasonAlert.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/FailureReasonAlert.tsx @@ -6,6 +6,9 @@ import { Link } from "../common/Link"; type Props = { failureReason: VariantAnalysisFailureReason; + // Set for runs on a custom server, which do not use GitHub Actions. + serverUrl?: string; + canViewLogs?: boolean; }; const openLogs = () => { @@ -42,7 +45,34 @@ const getMessage = (failureReason: VariantAnalysisFailureReason): ReactNode => { } }; -export const FailureReasonAlert = ({ failureReason }: Props) => { +export const FailureReasonAlert = ({ + failureReason, + serverUrl, + canViewLogs, +}: Props) => { + if ( + serverUrl && + failureReason === VariantAnalysisFailureReason.ActionsWorkflowRunFailed + ) { + return ( + + The variant analysis run has failed.{" "} + {canViewLogs && ( + <> + View logs and{" "} + + )} + try running this query again. + + } + /> + ); + } + return ( ; } - const onViewLogsClick = - variantAnalysis.actionsWorkflowRunId === undefined ? undefined : openLogs; + const onViewLogsClick = hasVariantAnalysisLogs(variantAnalysis) + ? openLogs + : undefined; return ( <> diff --git a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisOutcomePanels.tsx b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisOutcomePanels.tsx index 1f138ffd8cd..8f766aedb27 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisOutcomePanels.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisOutcomePanels.tsx @@ -102,7 +102,11 @@ export const VariantAnalysisOutcomePanels = ({ )} {variantAnalysis.status === VariantAnalysisStatus.Failed && variantAnalysis.failureReason && ( - + )} {overLimitRepositoryCount > 0 && ( { expect(screen.getByText("Stop query")).toBeEnabled(); }); + it.each([true, false])( + "offers logs for a server run only if the companion supports them: %s", + (canViewLogs) => { + render({ + variantAnalysis: { + ...createMockVariantAnalysis({}), + serverUrl: "https://mrva.example.com", + actionsWorkflowRunId: undefined, + canViewLogs, + }, + }); + + expect(screen.queryByText("View actions logs") !== null).toBe( + canViewLogs, + ); + }, + ); + it("renders an analysis where there were no repos to analyse", () => { const variantAnalysis = createMockVariantAnalysis({ status: VariantAnalysisStatus.Failed, diff --git a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisOutcomePanels.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisOutcomePanels.spec.tsx index 0ac4ab23df3..c3b54636c4b 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisOutcomePanels.spec.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisOutcomePanels.spec.tsx @@ -95,6 +95,22 @@ describe(VariantAnalysisOutcomePanels.name, () => { expect(screen.getByText("Analyzed")).toBeInTheDocument(); }); + it.each([true, false])( + "shows a server failure without GitHub Actions (logs: %s)", + (canViewLogs) => { + render({ + serverUrl: "https://mrva.example.com", + canViewLogs, + status: VariantAnalysisStatus.Failed, + failureReason: VariantAnalysisFailureReason.ActionsWorkflowRunFailed, + }); + + expect(screen.getByText(/Variant analysis failed/)).toBeInTheDocument(); + expect(screen.queryByText("View actions logs")).not.toBeInTheDocument(); + expect(screen.queryByText("View logs") !== null).toBe(canViewLogs); + }, + ); + it("does not render panels without skipped repos", () => { render({ skippedRepos: undefined, diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts index a0305761e91..ee27748df4d 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts @@ -32,6 +32,7 @@ import { createMockApp } from "../../../__mocks__/appMock"; import { LanguageContextStore } from "../../../../src/language-context-store"; import type { App } from "../../../../src/common/app"; import { QueryLanguage } from "../../../../src/common/query-language"; +import * as variantAnalysisApi from "../../../../src/variant-analysis/variant-analysis-api"; describe("HistoryTreeDataProvider", () => { const mockExtensionLocation = join(tmpDir.name, "mock-extension-location"); @@ -144,6 +145,49 @@ describe("HistoryTreeDataProvider", () => { }); describe("getTreeItem", () => { + describe.each([ + [ + QueryStatus.InProgress, + VariantAnalysisStatus.InProgress, + "inProgressRemoteResultsItem", + ], + [ + QueryStatus.Completed, + VariantAnalysisStatus.Succeeded, + "remoteResultsItem", + ], + [ + QueryStatus.Failed, + VariantAnalysisStatus.Failed, + "cancelledRemoteResultsItem", + ], + ])( + "server run with status %s", + (historyItemStatus, variantAnalysisStatus, context) => { + it.each([ + [true, undefined], + [false, undefined], + [false, 123], + ] as const)( + "uses log capability %s rather than workflow ID %s", + async (canViewLogs, workflowId) => { + jest + .spyOn(variantAnalysisApi, "canViewServerVariantAnalysisLogs") + .mockReturnValue(canViewLogs); + const item = createMockVariantAnalysisHistoryItem({ + historyItemStatus, + variantAnalysisStatus, + }); + item.variantAnalysis.serverUrl = "https://mrva.example.com"; + item.variantAnalysis.actionsWorkflowRunId = workflowId; + expect( + (await historyTreeDataProvider.getTreeItem(item)).contextValue, + ).toBe(`${context}${canViewLogs ? "" : "WithoutLogs"}`); + }, + ); + }, + ); + it("should get a tree item with raw results", async () => { const mockQueryWithRawResults = createMockLocalQueryInfo({ dbName: "a", diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 4b11e7a6df7..92d2e3a225f 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -1,6 +1,14 @@ -import { ConfigurationTarget, extensions, window, workspace } from "vscode"; -import type { Extension } from "vscode"; +import { + ConfigurationTarget, + EventEmitter, + extensions, + window, + workspace, +} from "vscode"; +import type { ConfigurationChangeEvent, Disposable, Extension } from "vscode"; +import { setImmediate } from "timers/promises"; import type { AppCommandManager } from "../../../../src/common/commands"; +import { extLogger } from "../../../../src/common/logging/vscode"; import { getVariantAnalysisServerConfig, isVariantAnalysisServerConfigured, @@ -8,7 +16,13 @@ import { import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; import type { VariantAnalysisServerTokenProvider } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; import { requestVariantAnalysisServer } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; -import { openServerVariantAnalysisLogs } from "../../../../src/variant-analysis/variant-analysis-api"; +import { + canViewServerVariantAnalysisLogs, + onDidChangeVariantAnalysisLogCapabilities, + openServerVariantAnalysisLogs, + watchVariantAnalysisLogCapabilities, + withVariantAnalysisLogCapability, +} from "../../../../src/variant-analysis/variant-analysis-api"; import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; import { testCredentialsWithStub } from "../../../factories/authentication"; import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis"; @@ -285,3 +299,143 @@ describe("opening server logs", () => { ); }); }); + +describe("server log capabilities", () => { + let run: ReturnType; + let configurationChanged: EventEmitter; + let watcher: Disposable; + + beforeEach(async () => { + await configureServer(); + run = { ...createMockVariantAnalysis({}), serverUrl: SERVER_URL }; + configurationChanged = new EventEmitter(); + jest + .spyOn(workspace, "onDidChangeConfiguration") + .mockImplementation(configurationChanged.event); + watcher = watchVariantAnalysisLogCapabilities(); + }); + + afterEach(() => { + watcher.dispose(); + configurationChanged.dispose(); + }); + + function discoveryFinished(): Promise { + return new Promise((resolve) => { + const subscription = onDidChangeVariantAnalysisLogCapabilities(() => { + subscription.dispose(); + resolve(); + }); + }); + } + + it("discovers log support in the background, once per server and provider", async () => { + const activate = jest.fn().mockResolvedValue({ + getVariantAnalysisServerToken, + openVariantAnalysisLogs: jest.fn(), + }); + mockProvider({ isActive: false, activate }); + + const finished = discoveryFinished(); + expect(withVariantAnalysisLogCapability(run)).toEqual({ + ...run, + canViewLogs: false, + }); + expect(canViewServerVariantAnalysisLogs({ ...run, id: run.id + 1 })).toBe( + false, + ); + await finished; + expect(withVariantAnalysisLogCapability(run)).toEqual({ + ...run, + canViewLogs: true, + }); + expect(activate).toHaveBeenCalledTimes(1); + expect(getVariantAnalysisServerToken).not.toHaveBeenCalled(); + + const githubRun = createMockVariantAnalysis({}); + expect(withVariantAnalysisLogCapability(githubRun)).toBe(githubRun); + await setSetting("serverUrl", "https://other.example.com"); + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + }); + + it.each(["missing", "failed", "stalled"])( + "hides logs, without retrying, when the provider is %s", + async (state) => { + const log = jest.spyOn(extLogger, "log").mockResolvedValue(undefined); + const lookup = jest.spyOn(extensions, "getExtension").mockReturnValue( + state === "missing" + ? undefined + : mockedObject>({ + isActive: false, + activate: () => + state === "failed" + ? Promise.reject(new Error("Activation failed")) + : new Promise(() => {}), + }), + ); + if (state === "stalled") { + jest.useFakeTimers(); + } + try { + const finished = discoveryFinished(); + for (let id = 0; id < 10; id++) { + expect(canViewServerVariantAnalysisLogs({ ...run, id })).toBe(false); + } + if (state === "stalled") { + await jest.advanceTimersByTimeAsync(30_000); + expect(jest.getTimerCount()).toBe(0); + } + await finished; + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + expect(lookup).toHaveBeenCalledTimes(1); + expect(log).toHaveBeenCalledWith( + expect.stringContaining("Unable to discover variant analysis logs"), + ); + } finally { + jest.useRealTimers(); + } + }, + ); + + it.each(["serverUrl", "serverTokenProvider"])( + "invalidates discovery on %s changes and ignores stale completions", + async (setting) => { + let finishActivation!: ( + provider: Partial, + ) => void; + const activate = jest.fn().mockImplementation( + () => + new Promise((resolve) => { + finishActivation = resolve; + }), + ); + mockProvider({ isActive: false, activate }); + const changed = jest.fn(); + const subscription = onDidChangeVariantAnalysisLogCapabilities(changed); + try { + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + const oldActivation = finishActivation; + configurationChanged.fire({ + affectsConfiguration: (key) => + key === `codeQL.variantAnalysis.${setting}`, + }); + expect(changed).toHaveBeenCalledTimes(1); + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + expect(activate).toHaveBeenCalledTimes(2); + + const finished = discoveryFinished(); + finishActivation({}); + await finished; + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + oldActivation({ openVariantAnalysisLogs: jest.fn() }); + // Drain the obsolete discovery's continuation without a real timer. + await setImmediate(); + expect(canViewServerVariantAnalysisLogs(run)).toBe(false); + expect(changed).toHaveBeenCalledTimes(2); + expect(activate).toHaveBeenCalledTimes(2); + } finally { + subscription.dispose(); + } + }, + ); +}); From d59f5bce554bd1a40373ca657f60bfa8557bf9fa Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:39 +0100 Subject: [PATCH 12/18] Switching to custom-server support: Route requests to the server When codeQL.variantAnalysis.serverUrl is set, submit, poll, fetch repository tasks and cancel through the server client; stamp new runs with the server URL; use controller repository 0; and enable variant analysis regardless of github-enterprise.uri. Each run stays bound to the server that created it: its requests fail while another server, or none, is configured. Category: Switching to custom-server support. Without the setting, getVariantAnalysisServerConfig() returns undefined, so new runs get no serverUrl, runs without a serverUrl are routed to GitHub, and the other added conditions are false. Requests and error handling are then the same as before. --- .../ql-vscode/src/variant-analysis/ghec-dr.ts | 11 ++- .../src/variant-analysis/run-remote-query.ts | 7 ++ .../variant-analysis/variant-analysis-api.ts | 48 ++++++++++- .../variant-analysis-manager.ts | 6 +- .../variant-analysis-monitor.ts | 3 + .../variant-analysis/ghec-dr.test.ts | 17 +++- .../variant-analysis/custom-server.test.ts | 82 +++++++++++++++++++ 7 files changed, 169 insertions(+), 5 deletions(-) diff --git a/extensions/ql-vscode/src/variant-analysis/ghec-dr.ts b/extensions/ql-vscode/src/variant-analysis/ghec-dr.ts index c5a8ed06c62..131a2df72c9 100644 --- a/extensions/ql-vscode/src/variant-analysis/ghec-dr.ts +++ b/extensions/ql-vscode/src/variant-analysis/ghec-dr.ts @@ -1,10 +1,17 @@ -import { hasEnterpriseUri, hasGhecDrUri } from "../config"; +import { + hasEnterpriseUri, + hasGhecDrUri, + isVariantAnalysisServerConfigured, +} from "../config"; /** * Determines whether MRVA should be enabled or not for the current GitHub host. * MRVA is enabled on github.com and GHEC-DR. * This is based on the `github-enterprise.uri` setting. + * It is also enabled when an internal custom variant analysis server is configured. */ export function isVariantAnalysisEnabledForGitHubHost(): boolean { - return !hasEnterpriseUri() || hasGhecDrUri(); + return ( + !hasEnterpriseUri() || hasGhecDrUri() || isVariantAnalysisServerConfigured() + ); } 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 0ab0e3eb2f9..33ebe1b38fc 100644 --- a/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts +++ b/extensions/ql-vscode/src/variant-analysis/run-remote-query.ts @@ -12,6 +12,7 @@ import { extLogger } from "../common/logging/vscode"; import { getActionBranch, getRemoteControllerRepo, + isVariantAnalysisServerConfigured, setRemoteControllerRepo, } from "../config"; import type { ProgressCallback } from "../common/vscode/progress"; @@ -329,6 +330,12 @@ export function getQueryName( export async function getControllerRepo( credentials: Credentials, ): Promise { + // A custom variant analysis server does not use controller repositories, so + // its requests use repository ID 0. + if (isVariantAnalysisServerConfigured()) { + return { id: 0, fullName: "", private: true }; + } + // Get the controller repo from the config, if it exists. // If it doesn't exist, prompt the user to enter it, check // whether the repo exists, and save the nwo to the config. diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts index 667edc47d1e..f4a95e66f9a 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -14,16 +14,29 @@ import type { VariantAnalysis as ApiVariantAnalysis, VariantAnalysisRepoTask as ApiVariantAnalysisRepoTask, } from "./gh-api/variant-analysis"; -import { withVariantAnalysisServerProvider } from "./gh-api/variant-analysis-server-client"; +import { + requestVariantAnalysisServer, + withVariantAnalysisServerProvider, +} from "./gh-api/variant-analysis-server-client"; import type { VariantAnalysis, VariantAnalysisSubmission, } from "./shared/variant-analysis"; +import { mapVariantAnalysisSubmission } from "./variant-analysis-mapper"; export async function submitVariantAnalysis( credentials: Credentials, submission: VariantAnalysisSubmission, ): Promise { + const server = getServerForVariantAnalysis(submission.serverUrl); + if (server) { + return requestVariantAnalysisServer( + server, + "POST", + variantAnalysesPath(submission.controllerRepoId), + mapVariantAnalysisSubmission(submission), + ); + } return submitGitHubVariantAnalysis(credentials, submission); } @@ -31,7 +44,16 @@ export async function getVariantAnalysis( credentials: Credentials, controllerRepoId: number, variantAnalysisId: number, + serverUrl?: string, ): Promise { + const server = getServerForVariantAnalysis(serverUrl); + if (server) { + return requestVariantAnalysisServer( + server, + "GET", + `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}`, + ); + } return getGitHubVariantAnalysis( credentials, controllerRepoId, @@ -44,7 +66,16 @@ export async function getVariantAnalysisRepo( controllerRepoId: number, variantAnalysisId: number, repoId: number, + serverUrl?: string, ): Promise { + const server = getServerForVariantAnalysis(serverUrl); + if (server) { + return requestVariantAnalysisServer( + server, + "GET", + `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}/repositories/${repoId}`, + ); + } return getGitHubVariantAnalysisRepo( credentials, controllerRepoId, @@ -57,9 +88,24 @@ export async function cancelVariantAnalysis( credentials: Credentials, variantAnalysis: VariantAnalysis, ): Promise { + const server = getServerForVariantAnalysis(variantAnalysis.serverUrl); + if (server) { + await requestVariantAnalysisServer( + server, + "POST", + `${variantAnalysesPath(variantAnalysis.controllerRepo.id)}/${variantAnalysis.id}/cancel`, + undefined, + true, + ); + return; + } return cancelGitHubVariantAnalysis(credentials, variantAnalysis); } +function variantAnalysesPath(controllerRepoId: number): string { + return `/repositories/${controllerRepoId}/code-scanning/codeql/variant-analyses`; +} + /** * Returns the configured server for a run submitted to `serverUrl`, or * `undefined` for a GitHub run while no server is configured. Throws if the 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 b1954829116..75921103a8d 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -99,7 +99,7 @@ import { resolveCodeScanningQueryPack } from "./code-scanning-pack"; import { isSarifResultsQueryKind } from "../common/query-metadata"; import { isVariantAnalysisEnabledForGitHubHost } from "./ghec-dr"; import type { VariantAnalysisConfig } from "../config"; -import { getEnterpriseUri } from "../config"; +import { getEnterpriseUri, getVariantAnalysisServerConfig } from "../config"; const maxRetryCount = 3; @@ -338,6 +338,7 @@ export class VariantAnalysisManager } await saveBeforeStart(); + const serverUrl = getVariantAnalysisServerConfig()?.url; progress({ maxStep: 5, @@ -401,6 +402,7 @@ export class VariantAnalysisManager }; const variantAnalysisSubmission: VariantAnalysisSubmission = { + serverUrl, startTime: queryStartTime, actionRepoRef: actionBranch, controllerRepoId: controllerRepo.id, @@ -429,6 +431,7 @@ export class VariantAnalysisManager } catch (e) { // If the error is handled by the handleRequestError function, we don't need to throw if ( + !serverUrl && e instanceof RequestError && handleRequestError(e, this.config.githubUrl, this.app.logger) ) { @@ -811,6 +814,7 @@ export class VariantAnalysisManager variantAnalysis.controllerRepo.id, variantAnalysis.id, scannedRepo.repository.id, + variantAnalysis.serverUrl, ); repoTask = mapVariantAnalysisRepositoryTask(repoTaskResponse); diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts index ed25228c431..5b72f888d0c 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts @@ -66,6 +66,7 @@ export class VariantAnalysisMonitor extends DisposableObject { variantAnalysis.executionStartTime, variantAnalysis.query.name, variantAnalysis.language, + variantAnalysis.serverUrl, ); } finally { this.monitoringVariantAnalyses.delete(variantAnalysis.id); @@ -78,6 +79,7 @@ export class VariantAnalysisMonitor extends DisposableObject { executionStartTime: number, queryName: string, language: QueryLanguage, + serverUrl: string | undefined, ): Promise { const variantAnalysisLabel = `${queryName} (${language}) [${new Date( executionStartTime, @@ -101,6 +103,7 @@ export class VariantAnalysisMonitor extends DisposableObject { this.app.credentials, controllerRepoId, variantAnalysisId, + serverUrl, ); } catch (e) { const errorMessage = getErrorMessage(e); diff --git a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/variant-analysis/ghec-dr.test.ts b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/variant-analysis/ghec-dr.test.ts index ae857e3005a..4305409cecf 100644 --- a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/variant-analysis/ghec-dr.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/variant-analysis/ghec-dr.test.ts @@ -1,4 +1,4 @@ -import { ConfigurationTarget } from "vscode"; +import { ConfigurationTarget, workspace } from "vscode"; import { VSCODE_GITHUB_ENTERPRISE_URI_SETTING } from "../../../../src/config"; import { isVariantAnalysisEnabledForGitHubHost } from "../../../../src/variant-analysis/ghec-dr"; @@ -22,4 +22,19 @@ describe("checkVariantAnalysisEnabled", () => { ); expect(isVariantAnalysisEnabledForGitHubHost()).toBe(true); }); + + it("returns true when a custom server is configured", async () => { + await VSCODE_GITHUB_ENTERPRISE_URI_SETTING.updateValue( + "https://github.example.com", + ConfigurationTarget.Global, + ); + await workspace + .getConfiguration("codeQL.variantAnalysis") + .update( + "serverUrl", + "https://mrva.example.com", + ConfigurationTarget.Global, + ); + expect(isVariantAnalysisEnabledForGitHubHost()).toBe(true); + }); }); diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 92d2e3a225f..95fd6b84169 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -17,15 +17,20 @@ import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/e import type { VariantAnalysisServerTokenProvider } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; import { requestVariantAnalysisServer } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; import { + cancelVariantAnalysis, canViewServerVariantAnalysisLogs, + getVariantAnalysis, + getVariantAnalysisRepo, onDidChangeVariantAnalysisLogCapabilities, openServerVariantAnalysisLogs, + submitVariantAnalysis, watchVariantAnalysisLogCapabilities, withVariantAnalysisLogCapability, } from "../../../../src/variant-analysis/variant-analysis-api"; import type { VariantAnalysisManager } from "../../../../src/variant-analysis/variant-analysis-manager"; import { testCredentialsWithStub } from "../../../factories/authentication"; import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis"; +import { createMockSubmission } from "../../../factories/variant-analysis/shared/variant-analysis-submission"; import type { DeepPartial } from "../../utils/mocking.helpers"; import { mockedObject } from "../../utils/mocking.helpers"; @@ -439,3 +444,80 @@ describe("server log capabilities", () => { }, ); }); + +describe("variant analysis routing", () => { + let credentials: ReturnType; + + beforeEach(() => { + credentials = testCredentialsWithStub(); + }); + + it("uses GitHub when no server is configured", async () => { + await expect( + submitVariantAnalysis(credentials, createMockSubmission()), + ).rejects.toThrow("no stub was provided"); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it("submits, polls, and cancels on the configured server", async () => { + await configureServer(); + const submission = { + ...createMockSubmission(), + controllerRepoId: 0, + serverUrl: SERVER_URL, + }; + const run = { + ...createMockVariantAnalysis({}), + id: 5, + controllerRepo: { id: 0, fullName: "", private: true }, + serverUrl: SERVER_URL, + }; + + await expect( + submitVariantAnalysis(credentials, submission), + ).resolves.toEqual({ id: 123 }); + await getVariantAnalysis(credentials, 0, 5, SERVER_URL); + await getVariantAnalysisRepo(credentials, 0, 5, 7, SERVER_URL); + fetchSpy.mockResolvedValue(new Response(null, { status: 202 })); + await cancelVariantAnalysis(credentials, run); + + const analyses = `${SERVER_URL}/repositories/0/code-scanning/codeql/variant-analyses`; + expect( + fetchSpy.mock.calls.map(([url, init]) => [init?.method, url]), + ).toEqual([ + ["POST", analyses], + ["GET", `${analyses}/5`], + ["GET", `${analyses}/5/repositories/7`], + ["POST", `${analyses}/5/cancel`], + ]); + expect(JSON.parse(fetchSpy.mock.calls[0][1]?.body as string)).toMatchObject( + { + action_repo_ref: submission.actionRepoRef, + language: submission.language, + query_pack: submission.pack, + }, + ); + }); + + it("only sends a run to the server that ran it", async () => { + const githubRun = createMockVariantAnalysis({}); + const serverRun = { ...githubRun, serverUrl: SERVER_URL }; + + await configureServer(); + await expect(getVariantAnalysis(credentials, 0, 5)).rejects.toThrow( + /different server/, + ); + await expect(cancelVariantAnalysis(credentials, githubRun)).rejects.toThrow( + /different server/, + ); + + await setSetting("serverUrl", "https://other.example.com"); + await expect( + getVariantAnalysis(credentials, 0, 5, SERVER_URL), + ).rejects.toThrow(/different server/); + await expect(cancelVariantAnalysis(credentials, serverRun)).rejects.toThrow( + /different server/, + ); + expect(fetchSpy).not.toHaveBeenCalled(); + }); +}); From 086e975f707ca3dda9e342e6b6f033466e5111da Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:39 +0100 Subject: [PATCH 13/18] Switching to custom-server support: Adapt the repositories panel With a server configured, the repositories panel does not ask for a controller repository, and GitHub's built-in repository lists and the Open on GitHub action are hidden. Category: Switching to custom-server support. Each change adds a condition on the server setting. Without the setting, the when-clauses and isVariantAnalysisServerConfigured() see no server, and the panel behaves as before. --- extensions/ql-vscode/package.json | 4 ++-- extensions/ql-vscode/src/config.ts | 2 +- .../src/databases/ui/db-tree-data-provider.ts | 10 ++++++-- .../databases/db-panel.test.ts | 23 ++++++++++++++++++- .../variant-analysis/custom-server.test.ts | 14 +++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/extensions/ql-vscode/package.json b/extensions/ql-vscode/package.json index 2b13926e581..2835357f3b2 100644 --- a/extensions/ql-vscode/package.json +++ b/extensions/ql-vscode/package.json @@ -1194,7 +1194,7 @@ }, { "command": "codeQLVariantAnalysisRepositories.openOnGitHubContextMenu", - "when": "view == codeQLVariantAnalysisRepositories && viewItem =~ /canBeOpenedOnGitHub/", + "when": "view == codeQLVariantAnalysisRepositories && viewItem =~ /canBeOpenedOnGitHub/ && !config.codeQL.variantAnalysis.serverUrl", "group": "2_qlContextMenu@1" }, { @@ -2034,7 +2034,7 @@ { "view": "codeQLVariantAnalysisRepositories", "contents": "Set up a controller repository to start using variant analysis. [Learn more](https://codeql.github.com/docs/codeql-for-visual-studio-code/running-codeql-queries-at-scale-with-mrva#controller-repository) about controller repositories. \n[Set up controller repository](command:codeQLVariantAnalysisRepositories.setupControllerRepository)", - "when": "!config.codeQL.variantAnalysis.controllerRepo" + "when": "!config.codeQL.variantAnalysis.controllerRepo && !config.codeQL.variantAnalysis.serverUrl" } ] }, diff --git a/extensions/ql-vscode/src/config.ts b/extensions/ql-vscode/src/config.ts index 69f2eac3fce..03bc9566b78 100644 --- a/extensions/ql-vscode/src/config.ts +++ b/extensions/ql-vscode/src/config.ts @@ -734,7 +734,7 @@ export class VariantAnalysisConfigListener } public get showSystemDefinedRepositoryLists(): boolean { - return !hasEnterpriseUri(); + return !hasEnterpriseUri() && !isVariantAnalysisServerConfigured(); } public get githubUrl(): URL { diff --git a/extensions/ql-vscode/src/databases/ui/db-tree-data-provider.ts b/extensions/ql-vscode/src/databases/ui/db-tree-data-provider.ts index 8249af56472..8fcbc4bd647 100644 --- a/extensions/ql-vscode/src/databases/ui/db-tree-data-provider.ts +++ b/extensions/ql-vscode/src/databases/ui/db-tree-data-provider.ts @@ -7,7 +7,10 @@ import { mapDbItemToTreeViewItem } from "./db-item-mapper"; import { DisposableObject } from "../../common/disposable-object"; import type { DbConfigValidationError } from "../db-validation-errors"; import { DbConfigValidationErrorKind } from "../db-validation-errors"; -import { VariantAnalysisConfigListener } from "../../config"; +import { + isVariantAnalysisServerConfigured, + VariantAnalysisConfigListener, +} from "../../config"; export class DbTreeDataProvider extends DisposableObject @@ -84,7 +87,10 @@ export class DbTreeDataProvider private createTree(): DbTreeViewItem[] { // Returning an empty tree here will show the welcome view - if (!this.variantAnalysisConfig.controllerRepo) { + if ( + !this.variantAnalysisConfig.controllerRepo && + !isVariantAnalysisServerConfigured() + ) { return []; } diff --git a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts index 2670c70d65f..515f74a92e8 100644 --- a/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts @@ -1,4 +1,10 @@ -import { TreeItemCollapsibleState, ThemeIcon, ThemeColor } from "vscode"; +import { + ConfigurationTarget, + TreeItemCollapsibleState, + ThemeIcon, + ThemeColor, + workspace, +} from "vscode"; import { join } from "path"; import { ensureDir, remove, writeJson } from "fs-extra"; import type { DbConfig } from "../../../../src/databases/config/db-config"; @@ -111,6 +117,21 @@ describe("db panel", () => { }); }); + it("shows repositories without a controller repository when a server is configured", async () => { + await setRemoteControllerRepo(undefined); + await workspace + .getConfiguration("codeQL.variantAnalysis") + .update( + "serverUrl", + "https://mrva.example.com", + ConfigurationTarget.Global, + ); + + await saveDbConfig(createDbConfig({ remoteRepos: ["owner1/repo1"] })); + + expect(await dbTreeDataProvider.getChildren()).not.toHaveLength(0); + }); + async function saveDbConfig(dbConfig: DbConfig): Promise { await writeJson(dbConfigFilePath, dbConfig); diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 95fd6b84169..1f734bddf76 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -12,6 +12,7 @@ import { extLogger } from "../../../../src/common/logging/vscode"; import { getVariantAnalysisServerConfig, isVariantAnalysisServerConfigured, + VariantAnalysisConfigListener, } from "../../../../src/config"; import { exportVariantAnalysisResults } from "../../../../src/variant-analysis/export-results"; import type { VariantAnalysisServerTokenProvider } from "../../../../src/variant-analysis/gh-api/variant-analysis-server-client"; @@ -521,3 +522,16 @@ describe("variant analysis routing", () => { expect(fetchSpy).not.toHaveBeenCalled(); }); }); + +describe("repository lists with a custom server", () => { + it("hides system-defined repository lists", async () => { + const config = new VariantAnalysisConfigListener(); + try { + expect(config.showSystemDefinedRepositoryLists).toBe(true); + await setSetting("serverUrl", SERVER_URL); + expect(config.showSystemDefinedRepositoryLists).toBe(false); + } finally { + config.dispose(); + } + }); +}); From 2507e368ce023318f0870d376f05aa0b5bff3790 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:39 +0100 Subject: [PATCH 14/18] Non-production-code changes: Test the custom-server lifecycle Add an activated-extension test that submits a variant analysis to a mocked server, monitors it, downloads its results and checks their file links. It also checks that the server token is only sent to the server and that GitHub authentication is never used. Category: Non-production-code changes. Only a test is added; production code is unchanged. --- .../variant-analysis-manager.test.ts | 156 +++++++++++++++++- 1 file changed, 154 insertions(+), 2 deletions(-) diff --git a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts index ea06f904c17..834e1038d7e 100644 --- a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts @@ -1,5 +1,12 @@ -import type { TextDocument, TextEditor, Uri } from "vscode"; -import { commands, env, window, workspace } from "vscode"; +import type { Extension, TextDocument, TextEditor, Uri } from "vscode"; +import { + commands, + ConfigurationTarget, + env, + extensions, + window, + workspace, +} from "vscode"; import { extLogger } from "../../../../src/common/logging/vscode"; import * as ghApiClient from "../../../../src/variant-analysis/gh-api/gh-api-client"; import * as ghActionsApiClient from "../../../../src/variant-analysis/gh-api/gh-actions-api-client"; @@ -52,6 +59,10 @@ import { setupServer } from "msw/node"; import type { RequestHandler } from "msw"; import { http } from "msw"; import { getErrorMessage } from "../../../../src/common/helpers-pure"; +import { createMockSubmission } from "../../../factories/variant-analysis/shared/variant-analysis-submission"; +import { createMockApiResponse } from "../../../factories/variant-analysis/gh-api/variant-analysis-api-response"; +import { mapVariantAnalysisFromSubmission } from "../../../../src/variant-analysis/variant-analysis-mapper"; +import { VariantAnalysisMonitor } from "../../../../src/variant-analysis/variant-analysis-monitor"; // up to 3 minutes per test jest.setTimeout(3 * 60 * 1000); @@ -118,6 +129,147 @@ describe("Variant Analysis Manager", () => { ); }); + it("runs a custom-server analysis from submission to downloaded results", async () => { + const serverUrl = "https://mrva.example.com"; + const artifactUrl = "https://artifacts.example.com/results.zip"; + const sourceUrl = "https://source.example.com/example/repo"; + const config = workspace.getConfiguration("codeQL.variantAnalysis"); + await config.update("serverUrl", serverUrl, ConfigurationTarget.Global); + await config.update( + "serverTokenProvider", + "example.provider", + ConfigurationTarget.Global, + ); + const token = jest.fn().mockResolvedValue("server-token"); + const activate = jest.fn().mockResolvedValue({ + getVariantAnalysisServerToken: token, + }); + const extensionLookup = extensions.getExtension.bind(extensions); + jest + .spyOn(extensions, "getExtension") + .mockImplementation((id) => + id === "example.provider" + ? mockedObject>({ isActive: false, activate }) + : extensionLookup(id), + ); + const github = jest + .spyOn(app.credentials, "getOctokit") + .mockRejectedValue( + new Error("Custom-server requests must not use GitHub authentication"), + ); + const response = createMockApiResponse("in_progress", []); + response.controller_repo.id = 0; + delete response.actions_workflow_run_id; + const task = createMockVariantAnalysisRepoTask(); + task.artifact_url = artifactUrl; + task.repository.html_url = sourceUrl; + const apiPath = `${serverUrl}/repositories/0/code-scanning/codeql/variant-analyses`; + const runPath = `${apiPath}/${response.id}`; + let polls = 0; + server.resetHandlers( + http.post(apiPath, () => Response.json(response)), + http.get(runPath, () => + Response.json( + ++polls === 1 + ? response + : { + ...response, + status: "succeeded", + scanned_repositories: [ + { + repository: task.repository, + analysis_status: "succeeded", + result_count: 1, + }, + ], + }, + ), + ), + http.get(`${runPath}/repositories/${task.repository.id}`, () => + Response.json(task), + ), + http.get( + artifactUrl, + async () => + new Response( + await readFile( + join(__dirname, "data/variant-analysis-results.zip"), + ), + ), + ), + ); + const submission = { + ...createMockSubmission(), + serverUrl, + controllerRepoId: 0, + }; + const storage = variantAnalysisManager.getVariantAnalysisStorageLocation( + response.id, + ); + const sleepTime = VariantAnalysisMonitor.sleepTime; + VariantAnalysisMonitor.sleepTime = 1; + const downloads: Array> = []; + jest + .spyOn(app.commands, "execute") + .mockImplementation(async (command, ...args) => { + if (command === "codeQL.autoDownloadVariantAnalysisResult") { + const [repo, run] = args as [ + VariantAnalysisScannedRepository, + VariantAnalysis, + ]; + const download = + variantAnalysisManager.autoDownloadVariantAnalysisResult(repo, run); + downloads.push(download); + await download; + } + }); + try { + const submitted = await variantAnalysisApi.submitVariantAnalysis( + app.credentials, + submission, + ); + const run = mapVariantAnalysisFromSubmission(submission, submitted, []); + await createTimestampFile(storage); + await variantAnalysisManager.rehydrateVariantAnalysis(run); + await variantAnalysisManager.monitorVariantAnalysis(run); + await Promise.all(downloads); + expect(downloads).toHaveLength(1); + expect(variantAnalysisManager.tryGetVariantAnalysis(run.id)?.status).toBe( + VariantAnalysisStatus.Succeeded, + ); + const results = await variantAnalysisManager.loadResults( + run.id, + task.repository.full_name, + ); + expect(results.interpretedResults?.length).toBeGreaterThan(0); + expect(results.interpretedResults?.[0].fileLink.fileLinkPrefix).toBe( + `${sourceUrl}/blob/HEAD`, + ); + expect( + requests.map((request) => `${request.method} ${request.url}`), + ).toEqual([ + `POST ${apiPath}`, + `GET ${runPath}`, + `GET ${runPath}`, + `GET ${runPath}/repositories/${task.repository.id}`, + `GET ${artifactUrl}`, + ]); + for (const request of requests) { + expect(request.headers.get("authorization")).toBe( + request.url === artifactUrl ? null : "Bearer server-token", + ); + } + expect(activate).toHaveBeenCalled(); + expect(github).not.toHaveBeenCalled(); + } finally { + VariantAnalysisMonitor.sleepTime = sleepTime; + await Promise.allSettled(downloads); + await remove(storage); + variantAnalysisManager.dispose(); + variantAnalysisResultsManager.dispose(); + } + }); + describe("rehydrateVariantAnalysis", () => { const variantAnalysis = createMockVariantAnalysis({}); From 05880216b9034d5747a956ef688120553ff29ed5 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Tue, 17 Nov 2026 16:20:39 +0100 Subject: [PATCH 15/18] Non-production-code changes: Document the custom-server integration Document the hidden settings, the token provider contract, the server's REST contract, and how CodeQL behaves when a server is configured. Category: Non-production-code changes. Only documentation is added; production code is unchanged. --- docs/variant-analysis-custom-server.md | 134 +++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 docs/variant-analysis-custom-server.md diff --git a/docs/variant-analysis-custom-server.md b/docs/variant-analysis-custom-server.md new file mode 100644 index 00000000000..a8cd62d63d2 --- /dev/null +++ b/docs/variant-analysis-custom-server.md @@ -0,0 +1,134 @@ +# Custom variant analysis server (internal) + +> [!WARNING] +> **Internal and unsupported.** This is not a public feature. The settings below are not declared in +> `package.json`, do not appear in the Settings UI, and may change or be removed without notice. +> Do not document them in user-facing docs or the changelog. + +By default, multi-repository variant analysis (MRVA) uses the GitHub REST API and the user's GitHub sign-in. +An organization can instead send variant analyses to its own server. That server must implement the +GitHub variant analysis REST endpoints and authenticate requests with a bearer token. A separate +extension, owned by the organization, supplies the token. + +## Settings + +Both settings are only read from **local or remote user** settings. Workspace and folder values are ignored, +so a repository cannot redirect queries or tokens. +`inspect().globalValue` is VS Code's merged user value, including remote user settings; +the remote value takes precedence over the local value. These are not OS-wide policy settings. +Administrators can provision the remote user settings file directly. A companion extension writing +with `ConfigurationTarget.Global` should not assume that the write always targets the remote file: +VS Code's choice depends on the setting's scope and whether a remote value already exists. + +| Setting | Value | +| --- | --- | +| `codeQL.variantAnalysis.serverUrl` | Base URL of the server. Must be `https:` and must not contain credentials. A trailing `/` is removed. | +| `codeQL.variantAnalysis.serverTokenProvider` | ID (`publisher.name`) of the installed extension that provides the token. | + +If `serverUrl` is set but invalid, variant analysis fails instead of falling back to GitHub. +Non-string values, query strings and fragments (including a bare `?` or `#`) are rejected; +a blank value counts as unset. Base path prefixes are supported. +The hidden server setting is the opt-in switch; there is no additional canary gate. + +## Token provider extension + +The extension named in `serverTokenProvider` must return the following from `activate()`: + +```ts +export interface VariantAnalysisServerTokenProvider { + getVariantAnalysisServerToken(serverUrl: string): Promise; + openVariantAnalysisLogs?(serverUrl: string, analysisId: number): Promise; +} +``` + +CodeQL activates the extension if it isn't already running, and calls `getVariantAnalysisServerToken` +before every request. The provider is responsible for caching and refreshing the token. It should return +`undefined` if no token is available, so that CodeQL can show a clear error instead of sending an +unauthenticated request. The token is never logged. +CodeQL waits at most 30 seconds for activation and token acquisition combined. On timeout, no API +request is sent; the provider's own work is not forcibly stopped. Finish sign-in in the provider and retry. +HTTP 401/403 errors identify the provider and ask the user to re-authenticate; there is no automatic retry +or token-refresh argument. + +Only the extension that the user or administrator names is called. Extensions cannot register +themselves as providers. + +The optional `openVariantAnalysisLogs` hook enables the existing log actions for server runs: +**View Logs** in query history, **View actions logs** in the results view, and **View logs** in the +failure message. +The provider owns log retrieval, authentication, and presentation (for example, opening a browser +or an output channel). CodeQL passes the originating server URL and analysis/session ID, not an +Actions workflow ID. Without the hook, or while the server setting is invalid, the actions are hidden. Capability discovery activates the +provider in the background with the same 30-second activation bound. History and result views render +immediately without the action, then refresh when discovery finishes. Discovery is shared and cached +per server URL and provider ID, including failures; changing either setting clears the cache. +If discovery fails, CodeQL logs the error and leaves the action hidden without delaying saved results. +Opening logs does not request a token from CodeQL. +The hook should resolve once its viewer is opened and reject on failure. Runs from another configured +server cannot invoke it. No log endpoint or GitHub Actions compatibility is required. + +Providers should publish their exports immediately from `activate()`. Catalog downloads, sign-in +prompts, and other potentially unbounded work must not block activation; perform them lazily in the +operation that needs them. + +## Behavior when a server is configured + +- MRVA is enabled regardless of `github-enterprise.uri`. The GitHub sign-in, GitHub database + downloads and other extensions that read `github-enterprise.uri` are not affected. +- No controller repository is needed. Requests use controller repository ID `0`. +- Runs retain their originating server URL. Polling, task retrieval and cancellation fail if + the current server setting differs. Restore the original setting to resume them. Legacy runs + without a server URL are treated as GitHub runs. +- The GitHub token is never sent to the server, and results are never uploaded to GitHub: + "Export to gist" is unavailable; "View logs" uses the companion's optional hook, not GitHub Actions. +- Removing a running server analysis from history asks, in a modal dialog, whether to stop it. + **Stop** cancels it on the server first and keeps the history entry if that fails. + **Keep running** removes only the local history entry; the server run continues. +- Result artifacts are downloaded from `artifact_url` without authentication, as on GitHub. +- If a repository task has no `database_commit_sha`, file links point to `HEAD`. + These links may not match the analyzed revision. +- Custom-server file links use `repository.html_url` from the task or analysis response. + If it is absent or not an HTTP(S) URL, results still display, but file locations are plain text. + GitHub run links retain their existing host fallback. +- The repositories panel does not ask for a controller repository. GitHub's built-in repository + lists and the repository tree's "Open on GitHub" action are hidden. + The welcome view and "Open on GitHub" use `when` clauses, which read VS Code's merged setting, + including workspace values. A workspace `serverUrl` therefore hides them without enabling the + server, and the repositories panel can appear empty. +- The canary-only autofix action still downloads repository source through GitHub sign-in. + It requires the analyzed repository and revision to be available on the configured GitHub host. + Custom source retrieval is not supported. + +## Server contract + +All paths are relative to `serverUrl`. Requests send `Authorization: Bearer ` and JSON bodies. +Redirects are rejected rather than forwarding query packs to another endpoint. +Request and response shapes match `src/variant-analysis/gh-api/variant-analysis.ts`. + +| Operation | Request | +| --- | --- | +| Submit | `POST /repositories/0/code-scanning/codeql/variant-analyses` | +| Get status | `GET /repositories/0/code-scanning/codeql/variant-analyses/{id}` | +| Get repository task | `GET /repositories/0/code-scanning/codeql/variant-analyses/{id}/repositories/{repoId}` | +| Cancel | `POST /repositories/0/code-scanning/codeql/variant-analyses/{id}/cancel` | + +Variant analysis responses must include `controller_repo`; subsequent requests use its `id` as the +repository path parameter (normally `0`). The analysis `id` is the server's run/session identifier. +Custom-server cancellation does not require `actions_workflow_run_id`; GitHub cancellation still uses +the GitHub Actions route and requires that field. Errors should be returned as non-2xx responses with a JSON body +`{ "message": "..." }`. CodeQL shows the status and that message to the user. + +## Running CodeQL in a remote workspace + +CodeQL declares a Node `main` entry point, so VS Code normally classifies it as a workspace extension +without an explicit `extensionKind`. Install both CodeQL and its token provider in the same remote +extension host. No override should normally be necessary. + +If a deployment has overridden the placement, inspect `remote.extensionKind` and the editor's +product overrides. The user setting `{ "github.vscode-codeql": ["workspace"] }` under +`remote.extensionKind` can override placement, but should not be installed as a blanket workaround. + +VS Code implementation references: +[configuration inspection](https://github.com/microsoft/vscode/blob/main/src/vs/workbench/api/common/extHostConfiguration.ts), +[local/remote user configuration merging](https://github.com/microsoft/vscode/blob/main/src/vs/platform/configuration/common/configurationModels.ts), +and [extension kind inference](https://github.com/microsoft/vscode/blob/main/src/vs/workbench/services/extensions/common/extensionManifestPropertiesService.ts). From 77fd897b80ef74f001b45c5caad5317a07d686a0 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Wed, 18 Nov 2026 15:32:37 +0100 Subject: [PATCH 16/18] Adding custom-server support: Validate IDs and repository names The server's IDs and repository names become request URLs and file paths. A server that returned a repository name such as "../x" could make CodeQL read or write files outside its storage directory. Reject server responses whose IDs are not integers, or whose repository names have an empty or dots-only part, a backslash or a colon. Other names, including ones with more than two parts, are accepted. Category: Adding custom-server support. Only the server branches of the variant analysis API check responses, and only runs with a serverUrl reach them. GitHub responses are not checked, so GitHub runs are unchanged. --- docs/variant-analysis-custom-server.md | 3 ++ .../variant-analysis/variant-analysis-api.ts | 45 +++++++++++++++++-- .../variant-analysis/custom-server.test.ts | 35 ++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/docs/variant-analysis-custom-server.md b/docs/variant-analysis-custom-server.md index a8cd62d63d2..1b33c83a719 100644 --- a/docs/variant-analysis-custom-server.md +++ b/docs/variant-analysis-custom-server.md @@ -114,6 +114,9 @@ Request and response shapes match `src/variant-analysis/gh-api/variant-analysis. Variant analysis responses must include `controller_repo`; subsequent requests use its `id` as the repository path parameter (normally `0`). The analysis `id` is the server's run/session identifier. +IDs must be integers. Repository `full_name` values become folder names, so they must not contain empty +or dots-only `/`-separated parts, backslashes or colons; CodeQL rejects responses that do. Names with +more than two parts are accepted, but Markdown export names files after the first two parts only. Custom-server cancellation does not require `actions_workflow_run_id`; GitHub cancellation still uses the GitHub Actions route and requires that field. Errors should be returned as non-2xx responses with a JSON body `{ "message": "..." }`. CodeQL shows the status and that message to the user. diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts index f4a95e66f9a..017380957e2 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -30,12 +30,13 @@ export async function submitVariantAnalysis( ): Promise { const server = getServerForVariantAnalysis(submission.serverUrl); if (server) { - return requestVariantAnalysisServer( + const analysis = await requestVariantAnalysisServer( server, "POST", variantAnalysesPath(submission.controllerRepoId), mapVariantAnalysisSubmission(submission), ); + return checkVariantAnalysis(analysis); } return submitGitHubVariantAnalysis(credentials, submission); } @@ -48,11 +49,12 @@ export async function getVariantAnalysis( ): Promise { const server = getServerForVariantAnalysis(serverUrl); if (server) { - return requestVariantAnalysisServer( + const analysis = await requestVariantAnalysisServer( server, "GET", `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}`, ); + return checkVariantAnalysis(analysis); } return getGitHubVariantAnalysis( credentials, @@ -70,11 +72,13 @@ export async function getVariantAnalysisRepo( ): Promise { const server = getServerForVariantAnalysis(serverUrl); if (server) { - return requestVariantAnalysisServer( + const task = await requestVariantAnalysisServer( server, "GET", `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}/repositories/${repoId}`, ); + checkServerData([task.repository]); + return task; } return getGitHubVariantAnalysisRepo( credentials, @@ -106,6 +110,41 @@ function variantAnalysesPath(controllerRepoId: number): string { return `/repositories/${controllerRepoId}/code-scanning/codeql/variant-analyses`; } +function checkVariantAnalysis( + analysis: ApiVariantAnalysis, +): ApiVariantAnalysis { + checkServerData( + analysis.scanned_repositories?.map((repo) => repo.repository) ?? [], + [analysis.id, analysis.controller_repo.id], + ); + return analysis; +} + +/** + * IDs and repository names from a server become request URLs and file paths, + * so reject IDs that are not integers and names that could leave CodeQL's + * storage directory: empty or dots-only parts, backslashes and colons. + */ +function checkServerData( + repositories: Array<{ id: number; full_name: string }>, + ids: number[] = [], +): void { + const valid = + ids.every(Number.isSafeInteger) && + repositories.every( + (repo) => + Number.isSafeInteger(repo.id) && + typeof repo.full_name === "string" && + /^[^\\:]+$/.test(repo.full_name) && + repo.full_name.split("/").every((part) => !/^\.*$/.test(part)), + ); + if (!valid) { + throw new Error( + "The variant analysis server returned an invalid response: IDs must be integers, and repository names must not contain empty or dots-only parts, backslashes or colons.", + ); + } +} + /** * Returns the configured server for a run submitted to `serverUrl`, or * `undefined` for a GitHub run while no server is configured. Throws if the diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 1f734bddf76..87d0bce42d0 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -447,10 +447,20 @@ describe("server log capabilities", () => { }); describe("variant analysis routing", () => { + // Valid as a variant analysis and as a repository task. + const response = { + id: 5, + controller_repo: { id: 0 }, + scanned_repositories: [{ repository: { id: 7, full_name: "a.b/.github" } }], + repository: { id: 7, full_name: "group/sub/c.js" }, + }; let credentials: ReturnType; beforeEach(() => { credentials = testCredentialsWithStub(); + fetchSpy.mockImplementation( + async () => new Response(JSON.stringify(response)), + ); }); it("uses GitHub when no server is configured", async () => { @@ -476,7 +486,7 @@ describe("variant analysis routing", () => { await expect( submitVariantAnalysis(credentials, submission), - ).resolves.toEqual({ id: 123 }); + ).resolves.toEqual(response); await getVariantAnalysis(credentials, 0, 5, SERVER_URL); await getVariantAnalysisRepo(credentials, 0, 5, 7, SERVER_URL); fetchSpy.mockResolvedValue(new Response(null, { status: 202 })); @@ -500,6 +510,29 @@ describe("variant analysis routing", () => { ); }); + it.each([ + { id: "../5" }, + { controller_repo: { id: 0.5 } }, + { scanned_repositories: [{ repository: { id: 7, full_name: "../x" } }] }, + { repository: { id: "7", full_name: "x/y" } }, + { repository: { id: 7, full_name: "x/.." } }, + { repository: { id: 7, full_name: "x//y" } }, + { repository: { id: 7, full_name: "x/..\\y" } }, + { repository: { id: 7, full_name: "C:/x" } }, + { repository: { id: 7, full_name: 7 } }, + ])("rejects unsafe IDs and repository names: %j", async (change) => { + await configureServer(); + fetchSpy.mockImplementation( + async () => new Response(JSON.stringify({ ...response, ...change })), + ); + await expect( + Promise.all([ + getVariantAnalysis(credentials, 0, 5, SERVER_URL), + getVariantAnalysisRepo(credentials, 0, 5, 7, SERVER_URL), + ]), + ).rejects.toThrow("IDs must be integers"); + }); + it("only sends a run to the server that ran it", async () => { const githubRun = createMockVariantAnalysis({}); const serverRun = { ...githubRun, serverUrl: SERVER_URL }; From 7cf28c13c5e3a3973f8fc15d0213928ecf16294b Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Wed, 18 Nov 2026 17:05:55 +0100 Subject: [PATCH 17/18] Adding custom-server support: Reject null server settings Treat only missing values and blank strings as unset. An explicit null must fail validation rather than falling back to GitHub. --- docs/variant-analysis-custom-server.md | 4 ++-- extensions/ql-vscode/src/config.ts | 2 +- .../variant-analysis/custom-server.test.ts | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/variant-analysis-custom-server.md b/docs/variant-analysis-custom-server.md index 1b33c83a719..dfa56b7f73b 100644 --- a/docs/variant-analysis-custom-server.md +++ b/docs/variant-analysis-custom-server.md @@ -26,8 +26,8 @@ VS Code's choice depends on the setting's scope and whether a remote value alrea | `codeQL.variantAnalysis.serverTokenProvider` | ID (`publisher.name`) of the installed extension that provides the token. | If `serverUrl` is set but invalid, variant analysis fails instead of falling back to GitHub. -Non-string values, query strings and fragments (including a bare `?` or `#`) are rejected; -a blank value counts as unset. Base path prefixes are supported. +Non-string values (including `null`), query strings and fragments (including a bare `?` or `#`) +are rejected; an absent setting or a blank string counts as unset. Base path prefixes are supported. The hidden server setting is the opt-in switch; there is no additional canary gate. ## Token provider extension diff --git a/extensions/ql-vscode/src/config.ts b/extensions/ql-vscode/src/config.ts index 03bc9566b78..88e1c7934bb 100644 --- a/extensions/ql-vscode/src/config.ts +++ b/extensions/ql-vscode/src/config.ts @@ -649,7 +649,7 @@ export interface VariantAnalysisServerConfig { */ function getGlobalStringSetting(setting: Setting): string | undefined { const value = setting.getGlobalValue(); - if (value === undefined || value === null) { + if (value === undefined) { return undefined; } if (typeof value !== "string") { diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index 87d0bce42d0..d235c79e157 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -108,7 +108,7 @@ describe("exporting custom-server results", () => { }); describe("variant analysis server settings", () => { - it.each([undefined, null, " "])( + it.each([undefined, " "])( "is not configured when the URL is %p", async (url) => { await setSetting("serverUrl", url); @@ -151,12 +151,22 @@ describe("variant analysis server settings", () => { `${SERVER_URL}/?`, `${SERVER_URL}/#`, "not a URL", - 42, ])("rejects %p but still counts it as configured", async (url) => { await setSetting("serverUrl", url); expect(() => getVariantAnalysisServerConfig()).toThrow(); expect(isVariantAnalysisServerConfigured()).toBe(true); }); + + it.each([false, 0, {}, null])( + "rejects a non-string URL (%p)", + async (url) => { + await setSetting("serverUrl", url); + expect(() => getVariantAnalysisServerConfig()).toThrow( + "codeQL.variantAnalysis.serverUrl must be a string.", + ); + expect(isVariantAnalysisServerConfigured()).toBe(true); + }, + ); }); describe("variant analysis server client", () => { From bd42976f402f8cc1b6b1341fbbcf5f1aaf0777c6 Mon Sep 17 00:00:00 2001 From: Nicolas Will Date: Wed, 18 Nov 2026 18:02:58 +0100 Subject: [PATCH 18/18] Adding custom-server support: Separate local and remote run IDs Reserve numeric local IDs for custom runs without changing existing GitHub storage or local interfaces. Persist the server ID for API calls, number custom-server Markdown export files, and give endpoint-specific guidance for saved runs. --- docs/variant-analysis-custom-server.md | 6 +++- .../query-history/query-history-manager.ts | 14 +++++++- ...-history-variant-analysis-domain-mapper.ts | 1 + ...ery-history-variant-analysis-dto-mapper.ts | 1 + .../query-history-variant-analysis-dto.ts | 1 + .../variant-analysis/markdown-generation.ts | 8 ++--- .../shared/variant-analysis.ts | 2 ++ .../variant-analysis/variant-analysis-api.ts | 11 +++--- .../variant-analysis-manager.ts | 36 ++++++++++++++++--- .../variant-analysis-mapper.ts | 7 +++- .../variant-analysis-monitor.ts | 4 ++- .../markdown-generation.test.ts | 34 ++++++++++++++++++ .../variant-analysis-manager.test.ts | 29 +++++++++++---- .../store/query-history-store.test.ts | 2 ++ .../variant-analysis/custom-server.test.ts | 11 +++--- 15 files changed, 141 insertions(+), 26 deletions(-) diff --git a/docs/variant-analysis-custom-server.md b/docs/variant-analysis-custom-server.md index dfa56b7f73b..906a74162a8 100644 --- a/docs/variant-analysis-custom-server.md +++ b/docs/variant-analysis-custom-server.md @@ -114,9 +114,13 @@ Request and response shapes match `src/variant-analysis/gh-api/variant-analysis. Variant analysis responses must include `controller_repo`; subsequent requests use its `id` as the repository path parameter (normally `0`). The analysis `id` is the server's run/session identifier. +Each custom-server run gets its own local ID and stores the server ID for API requests. GitHub runs +retain their existing storage layout. Custom-server runs saved by earlier development builds must +be rerun; their old directories are not reused because they may contain another endpoint's results. IDs must be integers. Repository `full_name` values become folder names, so they must not contain empty or dots-only `/`-separated parts, backslashes or colons; CodeQL rejects responses that do. Names with -more than two parts are accepted, but Markdown export names files after the first two parts only. +more than two parts are accepted. Local Markdown exports of custom-server results number each file +so repositories with the same name prefix do not overwrite one another's reports. Custom-server cancellation does not require `actions_workflow_run_id`; GitHub cancellation still uses the GitHub Actions route and requires that field. Errors should be returned as non-2xx responses with a JSON body `{ "message": "..." }`. CodeQL shows the status and that message to the user. diff --git a/extensions/ql-vscode/src/query-history/query-history-manager.ts b/extensions/ql-vscode/src/query-history/query-history-manager.ts index dbed31d73e7..6a141ceeb01 100644 --- a/extensions/ql-vscode/src/query-history/query-history-manager.ts +++ b/extensions/ql-vscode/src/query-history/query-history-manager.ts @@ -495,7 +495,19 @@ export class QueryHistoryManager extends DisposableObject { const history = await readQueryHistoryFromFile( this.queryMetadataStorageLocation, ); - this.treeDataProvider.allHistory = history; + this.treeDataProvider.allHistory = history.filter((item) => { + if ( + item.t === "variant-analysis" && + item.variantAnalysis.serverUrl && + item.variantAnalysis.serverRunId === undefined + ) { + void this.app.logger.log( + "Skipping an older custom-server variant analysis with unqualified storage. Rerun the analysis to see its results.", + ); + return false; + } + return true; + }); await Promise.all( this.treeDataProvider.allHistory.map(async (item) => { if (item.t === "variant-analysis") { diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts index 73719a21e0e..5eb35e6563c 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-domain-mapper.ts @@ -48,6 +48,7 @@ function mapVariantAnalysisDtoToDto( ): VariantAnalysisDto { return { id: variantAnalysis.id, + serverRunId: variantAnalysis.serverRunId, serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts index e2775b7b389..eab79f24795 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto-mapper.ts @@ -48,6 +48,7 @@ function mapVariantAnalysisToDomainModel( ): VariantAnalysis { return { id: variantAnalysis.id, + serverRunId: variantAnalysis.serverRunId, serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, diff --git a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts index e4550cdc639..de634a18447 100644 --- a/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts +++ b/extensions/ql-vscode/src/query-history/store/query-history-variant-analysis-dto.ts @@ -15,6 +15,7 @@ export interface QueryHistoryVariantAnalysisDto { export interface VariantAnalysisDto { id: number; + serverRunId?: number; serverUrl?: string; controllerRepo: { id: number; diff --git a/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts b/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts index ade753ee4f9..192abee8c1b 100644 --- a/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts +++ b/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts @@ -42,7 +42,7 @@ interface VariantAnalysisMarkdown { * Generates markdown files with variant analysis results. */ export async function generateVariantAnalysisMarkdown( - variantAnalysis: Pick, + variantAnalysis: Pick, results: AsyncIterable< [VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult] >, @@ -63,7 +63,7 @@ export async function generateVariantAnalysisMarkdown( fullName, resultsFiles.length, expectedResultsCount, - linkType, + linkType === "gist" || !!variantAnalysis.serverUrl, ); summaries.push({ fileName, @@ -431,10 +431,10 @@ function createVariantAnalysisFileName( fullName: string, index: number, expectedResultsCount: number, - linkType: MarkdownLinkType, + numbered: boolean, ) { const baseName = createFileName(fullName); - if (linkType === "gist") { + if (numbered) { const requiredNumberOfDecimals = Math.ceil( Math.log10(expectedResultsCount), ); 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 f62238b4240..d406b1748c5 100644 --- a/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts +++ b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts @@ -5,6 +5,8 @@ import type { ModelPackDetails } from "../../common/model-pack-details"; export interface VariantAnalysis { id: number; + /** Remote ID for custom runs, whose `id` is allocated locally. */ + serverRunId?: number; /** Custom server that created this run; absent for GitHub and legacy runs. */ serverUrl?: string; /** Runtime capability supplied to views, not persisted in query history. */ diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts index 017380957e2..9dac65bab98 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -97,7 +97,7 @@ export async function cancelVariantAnalysis( await requestVariantAnalysisServer( server, "POST", - `${variantAnalysesPath(variantAnalysis.controllerRepo.id)}/${variantAnalysis.id}/cancel`, + `${variantAnalysesPath(variantAnalysis.controllerRepo.id)}/${variantAnalysis.serverRunId ?? variantAnalysis.id}/cancel`, undefined, true, ); @@ -156,7 +156,9 @@ function getServerForVariantAnalysis( const server = getVariantAnalysisServerConfig(); if (server?.url !== serverUrl) { throw new Error( - "This variant analysis belongs to a different server. Restore the server setting used to submit it before retrying.", + serverUrl + ? `This variant analysis belongs to a different server. Set codeQL.variantAnalysis.serverUrl to ${serverUrl} before retrying.` + : "This variant analysis belongs to a different server (GitHub). Clear codeQL.variantAnalysis.serverUrl before retrying.", ); } return server; @@ -166,7 +168,8 @@ export async function openServerVariantAnalysisLogs( run: VariantAnalysis, ): Promise { const server = getServerForVariantAnalysis(run.serverUrl); - const openLogs = server && (await getServerLogOpener(server, run.id)); + const openLogs = + server && (await getServerLogOpener(server, run.serverRunId ?? run.id)); if (!openLogs) { throw new Error( "The configured variant analysis provider does not support viewing logs.", @@ -236,7 +239,7 @@ export function canViewServerVariantAnalysisLogs( } const capability = { available: false }; logCapabilities.set(key, capability); - void getServerLogOpener(server, run.id) + void getServerLogOpener(server, run.serverRunId ?? run.id) .then((openLogs) => { capability.available = openLogs !== undefined; }) 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 75921103a8d..0b3e30faca6 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts @@ -1,4 +1,5 @@ import { join } from "path"; +import { randomInt } from "crypto"; import { cancelVariantAnalysis, @@ -52,7 +53,7 @@ import { } from "./variant-analysis-mapper"; import PQueue from "p-queue"; import { createTimestampFile, saveBeforeStart } from "../run-queries-shared"; -import { readFile, remove, pathExists } from "fs-extra"; +import { mkdir, readFile, remove, pathExists } from "fs-extra"; import { EOL } from "os"; import type { ProgressCallback } from "../common/vscode/progress"; import { @@ -707,9 +708,12 @@ export class VariantAnalysisManager private async onVariantAnalysisSubmitted( variantAnalysis: VariantAnalysis, ): Promise { - await this.setVariantAnalysis(variantAnalysis); - + if (variantAnalysis.serverUrl) { + variantAnalysis.serverRunId = variantAnalysis.id; + variantAnalysis.id = await this.reserveServerRunId(); + } await this.prepareStorageDirectory(variantAnalysis.id); + await this.setVariantAnalysis(variantAnalysis); this.repoStates.set(variantAnalysis.id, {}); @@ -812,7 +816,7 @@ export class VariantAnalysisManager const repoTaskResponse = await getVariantAnalysisRepo( this.app.credentials, variantAnalysis.controllerRepo.id, - variantAnalysis.id, + variantAnalysis.serverRunId ?? variantAnalysis.id, scannedRepo.repository.id, variantAnalysis.serverUrl, ); @@ -1063,4 +1067,28 @@ export class VariantAnalysisManager this.getVariantAnalysisStorageLocation(variantAnalysisId), ); } + + private async reserveServerRunId(): Promise { + // Negative IDs keep GitHub's numeric storage unchanged. mkdir reserves the + // ID across workspaces before publishing the run to history. + for (;;) { + const id = -randomInt(1, 2 ** 48); + if (this.variantAnalyses.has(id)) { + continue; + } + try { + await mkdir(this.getVariantAnalysisStorageLocation(id)); + return id; + } catch (error) { + if (!( + error && + typeof error === "object" && + "code" in error && + error.code === "EEXIST" + )) { + throw error; + } + } + } + } } 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 cca834a797e..da329c27355 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-mapper.ts @@ -67,11 +67,16 @@ export function mapUpdatedVariantAnalysis( currentVariantAnalysis: VariantAnalysis, apiVariantAnalysis: ApiVariantAnalysis, ): VariantAnalysis { - return mapVariantAnalysis( + const updated = mapVariantAnalysis( currentVariantAnalysis, currentVariantAnalysis.status, apiVariantAnalysis, ); + if (currentVariantAnalysis.serverRunId !== undefined) { + updated.id = currentVariantAnalysis.id; + updated.serverRunId = currentVariantAnalysis.serverRunId; + } + return updated; } function mapVariantAnalysis( diff --git a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts index 5b72f888d0c..146ef7bcaec 100644 --- a/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts @@ -67,6 +67,7 @@ export class VariantAnalysisMonitor extends DisposableObject { variantAnalysis.query.name, variantAnalysis.language, variantAnalysis.serverUrl, + variantAnalysis.serverRunId ?? variantAnalysis.id, ); } finally { this.monitoringVariantAnalyses.delete(variantAnalysis.id); @@ -80,6 +81,7 @@ export class VariantAnalysisMonitor extends DisposableObject { queryName: string, language: QueryLanguage, serverUrl: string | undefined, + remoteId: number, ): Promise { const variantAnalysisLabel = `${queryName} (${language}) [${new Date( executionStartTime, @@ -102,7 +104,7 @@ export class VariantAnalysisMonitor extends DisposableObject { variantAnalysisSummary = await getVariantAnalysis( this.app.credentials, controllerRepoId, - variantAnalysisId, + remoteId, serverUrl, ); } catch (e) { diff --git a/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts b/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts index e22265de820..723c97149a6 100644 --- a/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts +++ b/extensions/ql-vscode/test/unit-tests/variant-analysis/markdown-generation.test.ts @@ -23,6 +23,40 @@ import { createMockScannedRepo } from "../../factories/variant-analysis/shared/s const dataPath = resolve(__dirname, "../data/markdown-generation"); describe(generateVariantAnalysisMarkdown.name, () => { + it("writes distinct files and summary links for nested custom-server repositories", async () => { + const run = { + ...createMockVariantAnalysis({}), + serverUrl: "https://mrva.example.com", + }; + async function* results(): AsyncIterable< + [VariantAnalysisScannedRepository, VariantAnalysisScannedRepositoryResult] + > { + for (const name of ["repo-a", "repo-b"]) { + const repo = createMockScannedRepo(); + repo.repository.fullName = `group/sub/${name}`; + repo.resultCount = 1; + yield [ + repo, + { variantAnalysisId: run.id, repositoryId: repo.repository.id }, + ]; + } + } + const { markdownFiles } = await generateVariantAnalysisMarkdown( + run, + results(), + 2, + "local", + ); + const [summary, first, second] = markdownFiles; + expect(first.fileName).not.toBe(second.fileName); + expect(summary.content.join("\n")).toContain( + `| group/sub/repo-a | [1 result(s)](./${first.fileName}.md) |`, + ); + expect(summary.content.join("\n")).toContain( + `| group/sub/repo-b | [1 result(s)](./${second.fileName}.md) |`, + ); + }); + it("exports locations as text when repository links are unavailable", async () => { const run = createMockVariantAnalysis({}); const repo = createMockScannedRepo(); diff --git a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts index 834e1038d7e..1bab3f304e2 100644 --- a/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts @@ -203,9 +203,7 @@ describe("Variant Analysis Manager", () => { serverUrl, controllerRepoId: 0, }; - const storage = variantAnalysisManager.getVariantAnalysisStorageLocation( - response.id, - ); + const storedRuns: VariantAnalysis[] = []; const sleepTime = VariantAnalysisMonitor.sleepTime; VariantAnalysisMonitor.sleepTime = 1; const downloads: Array> = []; @@ -229,14 +227,29 @@ describe("Variant Analysis Manager", () => { submission, ); const run = mapVariantAnalysisFromSubmission(submission, submitted, []); - await createTimestampFile(storage); - await variantAnalysisManager.rehydrateVariantAnalysis(run); + await variantAnalysisManager["onVariantAnalysisSubmitted"](run); + storedRuns.push(run); + const other = mapVariantAnalysisFromSubmission( + { ...submission, serverUrl: "https://other.example.com" }, + submitted, + [], + ); + await variantAnalysisManager["onVariantAnalysisSubmitted"](other); + storedRuns.push(other); + expect(other.id).not.toBe(run.id); + expect(run.id).toBeLessThan(0); + expect(other.id).toBeLessThan(0); + expect(run.serverRunId).toBe(response.id); + expect(other.serverRunId).toBe(response.id); await variantAnalysisManager.monitorVariantAnalysis(run); await Promise.all(downloads); expect(downloads).toHaveLength(1); expect(variantAnalysisManager.tryGetVariantAnalysis(run.id)?.status).toBe( VariantAnalysisStatus.Succeeded, ); + expect(variantAnalysisManager.tryGetVariantAnalysis(other.id)).toEqual( + other, + ); const results = await variantAnalysisManager.loadResults( run.id, task.repository.full_name, @@ -264,7 +277,11 @@ describe("Variant Analysis Manager", () => { } finally { VariantAnalysisMonitor.sleepTime = sleepTime; await Promise.allSettled(downloads); - await remove(storage); + await Promise.all( + storedRuns.map((run) => + variantAnalysisManager.removeVariantAnalysis(run), + ), + ); variantAnalysisManager.dispose(); variantAnalysisResultsManager.dispose(); } diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts index bbcaebe86e6..ec6bc77cf94 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/store/query-history-store.test.ts @@ -111,6 +111,8 @@ describe("write and read", () => { it("round-trips custom-server run data without changing legacy history", async () => { const serverRun = createMockVariantAnalysisHistoryItem({}); serverRun.variantAnalysis.serverUrl = "https://mrva.example.com"; + serverRun.variantAnalysis.serverRunId = serverRun.variantAnalysis.id; + serverRun.variantAnalysis.id = -1; serverRun.variantAnalysis.scannedRepos![0].repository.htmlUrl = "https://source.example.com/org/repo"; const legacyRun = createMockVariantAnalysisHistoryItem({}); diff --git a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts index d235c79e157..5da10aedbb0 100644 --- a/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -286,8 +286,10 @@ describe("opening server logs", () => { }); it("activates the companion and delegates without requesting a token", async () => { + run.serverRunId = 5; + run.id = -1; await openServerVariantAnalysisLogs(run); - expect(openVariantAnalysisLogs).toHaveBeenCalledWith(SERVER_URL, run.id); + expect(openVariantAnalysisLogs).toHaveBeenCalledWith(SERVER_URL, 5); expect(getVariantAnalysisServerToken).not.toHaveBeenCalled(); expect(fetchSpy).not.toHaveBeenCalled(); @@ -489,7 +491,8 @@ describe("variant analysis routing", () => { }; const run = { ...createMockVariantAnalysis({}), - id: 5, + id: -1, + serverRunId: 5, controllerRepo: { id: 0, fullName: "", private: true }, serverUrl: SERVER_URL, }; @@ -549,7 +552,7 @@ describe("variant analysis routing", () => { await configureServer(); await expect(getVariantAnalysis(credentials, 0, 5)).rejects.toThrow( - /different server/, + "Clear codeQL.variantAnalysis.serverUrl", ); await expect(cancelVariantAnalysis(credentials, githubRun)).rejects.toThrow( /different server/, @@ -558,7 +561,7 @@ describe("variant analysis routing", () => { await setSetting("serverUrl", "https://other.example.com"); await expect( getVariantAnalysis(credentials, 0, 5, SERVER_URL), - ).rejects.toThrow(/different server/); + ).rejects.toThrow(`Set codeQL.variantAnalysis.serverUrl to ${SERVER_URL}`); await expect(cancelVariantAnalysis(credentials, serverRun)).rejects.toThrow( /different server/, );