diff --git a/docs/variant-analysis-custom-server.md b/docs/variant-analysis-custom-server.md new file mode 100644 index 00000000000..906a74162a8 --- /dev/null +++ b/docs/variant-analysis-custom-server.md @@ -0,0 +1,141 @@ +# 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 (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 + +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. +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. 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. + +## 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). diff --git a/extensions/ql-vscode/package.json b/extensions/ql-vscode/package.json index 9bc4b725f66..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" }, { @@ -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", @@ -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/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/config.ts b/extensions/ql-vscode/src/config.ts index aabd1061b92..88e1c7934bb 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) { + 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; @@ -632,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/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/query-history/query-history-manager.ts b/extensions/ql-vscode/src/query-history/query-history-manager.ts index 6087816778f..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") { @@ -623,12 +635,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); } @@ -1044,6 +1073,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/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..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,8 @@ function mapVariantAnalysisDtoToDto( ): VariantAnalysisDto { return { id: variantAnalysis.id, + serverRunId: variantAnalysis.serverRunId, + serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, fullName: variantAnalysis.controllerRepo.fullName, @@ -96,6 +98,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 7f81ed91821..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,8 @@ function mapVariantAnalysisToDomainModel( ): VariantAnalysis { return { id: variantAnalysis.id, + serverRunId: variantAnalysis.serverRunId, + serverUrl: variantAnalysis.serverUrl, controllerRepo: { id: variantAnalysis.controllerRepo.id, fullName: variantAnalysis.controllerRepo.fullName, @@ -102,6 +104,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 dfe07af1de3..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,8 @@ export interface QueryHistoryVariantAnalysisDto { export interface VariantAnalysisDto { id: number; + serverRunId?: number; + serverUrl?: string; controllerRepo: { id: number; fullName: string; @@ -47,6 +49,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/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/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/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/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/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..8da01612f1e --- /dev/null +++ b/extensions/ql-vscode/src/variant-analysis/gh-api/variant-analysis-server-client.ts @@ -0,0 +1,162 @@ +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; + openVariantAnalysisLogs?( + serverUrl: string, + analysisId: number, + ): 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. + */ +export 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/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/markdown-generation.ts b/extensions/ql-vscode/src/variant-analysis/markdown-generation.ts index e295282d343..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, @@ -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; } /** @@ -432,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/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/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/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/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/shared/variant-analysis.ts b/extensions/ql-vscode/src/variant-analysis/shared/variant-analysis.ts index a83a5f2bbf6..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,12 @@ 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. */ + canViewLogs?: boolean; controllerRepo: Repository; language: QueryLanguage; query: { @@ -31,6 +37,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 { @@ -136,6 +152,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-api.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts new file mode 100644 index 00000000000..9dac65bab98 --- /dev/null +++ b/extensions/ql-vscode/src/variant-analysis/variant-analysis-api.ts @@ -0,0 +1,266 @@ +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"; +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 { + 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) { + const analysis = await requestVariantAnalysisServer( + server, + "POST", + variantAnalysesPath(submission.controllerRepoId), + mapVariantAnalysisSubmission(submission), + ); + return checkVariantAnalysis(analysis); + } + return submitGitHubVariantAnalysis(credentials, submission); +} + +export async function getVariantAnalysis( + credentials: Credentials, + controllerRepoId: number, + variantAnalysisId: number, + serverUrl?: string, +): Promise { + const server = getServerForVariantAnalysis(serverUrl); + if (server) { + const analysis = await requestVariantAnalysisServer( + server, + "GET", + `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}`, + ); + return checkVariantAnalysis(analysis); + } + return getGitHubVariantAnalysis( + credentials, + controllerRepoId, + variantAnalysisId, + ); +} + +export async function getVariantAnalysisRepo( + credentials: Credentials, + controllerRepoId: number, + variantAnalysisId: number, + repoId: number, + serverUrl?: string, +): Promise { + const server = getServerForVariantAnalysis(serverUrl); + if (server) { + const task = await requestVariantAnalysisServer( + server, + "GET", + `${variantAnalysesPath(controllerRepoId)}/${variantAnalysisId}/repositories/${repoId}`, + ); + checkServerData([task.repository]); + return task; + } + return getGitHubVariantAnalysisRepo( + credentials, + controllerRepoId, + variantAnalysisId, + repoId, + ); +} + +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.serverRunId ?? variantAnalysis.id}/cancel`, + undefined, + true, + ); + return; + } + return cancelGitHubVariantAnalysis(credentials, variantAnalysis); +} + +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 + * 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( + 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; +} + +export async function openServerVariantAnalysisLogs( + run: VariantAnalysis, +): Promise { + const server = getServerForVariantAnalysis(run.serverUrl); + 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.", + ); + } + 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; + }); +} + +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.serverRunId ?? 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-manager.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts index ad5dee850bf..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,9 +1,12 @@ import { join } from "path"; +import { randomInt } from "crypto"; import { + cancelVariantAnalysis, submitVariantAnalysis, getVariantAnalysisRepo, -} from "./gh-api/gh-api-client"; + openServerVariantAnalysisLogs, +} from "./variant-analysis-api"; import type { VariantAnalysis as ApiVariantAnalysis } from "./gh-api/variant-analysis"; import type { AuthenticationSessionsChangeEvent, @@ -50,9 +53,8 @@ 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 { cancelVariantAnalysis } from "./gh-api/gh-actions-api-client"; import type { ProgressCallback } from "../common/vscode/progress"; import { UserCancellationException, @@ -98,7 +100,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; @@ -337,6 +339,7 @@ export class VariantAnalysisManager } await saveBeforeStart(); + const serverUrl = getVariantAnalysisServerConfig()?.url; progress({ maxStep: 5, @@ -400,6 +403,7 @@ export class VariantAnalysisManager }; const variantAnalysisSubmission: VariantAnalysisSubmission = { + serverUrl, startTime: queryStartTime, actionRepoRef: actionBranch, controllerRepoId: controllerRepo.id, @@ -428,6 +432,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) ) { @@ -643,7 +648,7 @@ export class VariantAnalysisManager variantAnalysisId, this.getVariantAnalysisStorageLocation(variantAnalysisId), repositoryFullName, - options, + { ...options, serverUrl: variantAnalysis.serverUrl }, ); } @@ -703,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, {}); @@ -808,11 +816,13 @@ export class VariantAnalysisManager const repoTaskResponse = await getVariantAnalysisRepo( this.app.credentials, variantAnalysis.controllerRepo.id, - variantAnalysis.id, + variantAnalysis.serverRunId ?? variantAnalysis.id, scannedRepo.repository.id, + variantAnalysis.serverUrl, ); repoTask = mapVariantAnalysisRepositoryTask(repoTaskResponse); + repoTask.repository.htmlUrl ??= scannedRepo.repository.htmlUrl; } catch (e) { repoState.downloadStatus = VariantAnalysisScannedRepositoryDownloadStatus.Failed; @@ -927,7 +937,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}`, ); @@ -959,6 +969,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, @@ -1052,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 2b8858035a2..da329c27355 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, @@ -31,6 +45,7 @@ export function mapVariantAnalysisFromSubmission( ): VariantAnalysis { return mapVariantAnalysis( { + serverUrl: submission.serverUrl, language: submission.language, query: { name: submission.query.name, @@ -52,17 +67,23 @@ 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( currentVariantAnalysis: Pick< VariantAnalysis, | "language" + | "serverUrl" | "query" | "queries" | "databases" @@ -92,6 +113,7 @@ function mapVariantAnalysis( const variantAnalysis: VariantAnalysis = { id: response.id, + serverUrl: currentVariantAnalysis.serverUrl, controllerRepo: { id: response.controller_repo.id, fullName: response.controller_repo.full_name, @@ -126,6 +148,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), @@ -145,6 +168,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-monitor.ts b/extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts index 8592f84353d..146ef7bcaec 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 { @@ -66,6 +66,8 @@ export class VariantAnalysisMonitor extends DisposableObject { variantAnalysis.executionStartTime, variantAnalysis.query.name, variantAnalysis.language, + variantAnalysis.serverUrl, + variantAnalysis.serverRunId ?? variantAnalysis.id, ); } finally { this.monitoringVariantAnalyses.delete(variantAnalysis.id); @@ -78,6 +80,8 @@ export class VariantAnalysisMonitor extends DisposableObject { executionStartTime: number, queryName: string, language: QueryLanguage, + serverUrl: string | undefined, + remoteId: number, ): Promise { const variantAnalysisLabel = `${queryName} (${language}) [${new Date( executionStartTime, @@ -100,7 +104,8 @@ export class VariantAnalysisMonitor extends DisposableObject { variantAnalysisSummary = await getVariantAnalysis( this.app.credentials, controllerRepoId, - variantAnalysisId, + remoteId, + serverUrl, ); } catch (e) { const errorMessage = getErrorMessage(e); 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/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/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) => ( 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 ( ` type RawResultsTableProps = { resultSet: RawResultSet; - fileLinkPrefix: string; + fileLinkPrefix: string | undefined; sourceLocationPrefix: string; }; diff --git a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx index 3ac0599ae6c..650006acd5c 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/VariantAnalysis.tsx @@ -5,7 +5,10 @@ import type { VariantAnalysisScannedRepositoryResult, VariantAnalysisScannedRepositoryState, } from "../../variant-analysis/shared/variant-analysis"; -import { VariantAnalysisStatus } from "../../variant-analysis/shared/variant-analysis"; +import { + VariantAnalysisStatus, + hasVariantAnalysisLogs, +} from "../../variant-analysis/shared/variant-analysis"; import { VariantAnalysisHeader } from "./VariantAnalysisHeader"; import { VariantAnalysisOutcomePanels } from "./VariantAnalysisOutcomePanels"; import { VariantAnalysisLoading } from "./VariantAnalysisLoading"; @@ -151,13 +154,15 @@ export function VariantAnalysis({ if ( variantAnalysis === undefined || (variantAnalysis.status === VariantAnalysisStatus.InProgress && + !variantAnalysis.serverUrl && variantAnalysis.actionsWorkflowRunId === undefined) ) { 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/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/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 && ( { ); }; + 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/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx index 3279ed446d2..0ecd4bbd36d 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,41 @@ 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.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/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..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 @@ -17,10 +17,80 @@ 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("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(); + 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( 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..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 @@ -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,41 @@ 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("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-manager.test.ts b/extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-manager.test.ts index 850e221d84f..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 @@ -1,8 +1,16 @@ -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"; +import * as variantAnalysisApi from "../../../../src/variant-analysis/variant-analysis-api"; import { ensureDir, outputJson, @@ -51,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); @@ -117,6 +129,164 @@ 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 storedRuns: VariantAnalysis[] = []; + 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 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, + ); + 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 Promise.all( + storedRuns.map((run) => + variantAnalysisManager.removeVariantAnalysis(run), + ), + ); + variantAnalysisManager.dispose(); + variantAnalysisResultsManager.dispose(); + } + }); + describe("rehydrateVariantAnalysis", () => { const variantAnalysis = createMockVariantAnalysis({}); @@ -654,6 +824,48 @@ 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") + .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/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/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/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/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/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..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", () => { @@ -408,6 +425,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); 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..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 @@ -108,6 +108,20 @@ 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"; + 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({}); + 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"); 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..5da10aedbb0 --- /dev/null +++ b/extensions/ql-vscode/test/vscode-tests/no-workspace/variant-analysis/custom-server.test.ts @@ -0,0 +1,583 @@ +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, + 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"; +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"; + +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); +} + +async function configureServer() { + await setSetting("serverUrl", SERVER_URL); + await setSetting("serverTokenProvider", PROVIDER_ID); +} + +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)", + 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, + ); + }, + ); +}); + +describe("variant analysis server settings", () => { + it.each([undefined, " "])( + "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", + ])("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", () => { + 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/); + }, + ); +}); + +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 () => { + run.serverRunId = 5; + run.id = -1; + await openServerVariantAnalysisLogs(run); + expect(openVariantAnalysisLogs).toHaveBeenCalledWith(SERVER_URL, 5); + 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", + ); + }); +}); + +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(); + } + }, + ); +}); + +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 () => { + 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: -1, + serverRunId: 5, + controllerRepo: { id: 0, fullName: "", private: true }, + serverUrl: SERVER_URL, + }; + + await expect( + submitVariantAnalysis(credentials, submission), + ).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 })); + 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.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 }; + + await configureServer(); + await expect(getVariantAnalysis(credentials, 0, 5)).rejects.toThrow( + "Clear codeQL.variantAnalysis.serverUrl", + ); + 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(`Set codeQL.variantAnalysis.serverUrl to ${SERVER_URL}`); + await expect(cancelVariantAnalysis(credentials, serverRun)).rejects.toThrow( + /different server/, + ); + 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(); + } + }); +}); 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,