diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index 88b3488d5..172713bfe 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -915,8 +915,8 @@ async function activateWithInstalledDistribution( })); ctx.subscriptions.push( - commandRunner('codeQL.exportVariantAnalysisResults', async () => { - await exportRemoteQueryResults(qhm, rqm, ctx); + commandRunner('codeQL.exportVariantAnalysisResults', async (queryId?: string) => { + await exportRemoteQueryResults(qhm, rqm, ctx, queryId); }) ); diff --git a/extensions/ql-vscode/src/pure/interface-types.ts b/extensions/ql-vscode/src/pure/interface-types.ts index bde7d1881..f6693ae2c 100644 --- a/extensions/ql-vscode/src/pure/interface-types.ts +++ b/extensions/ql-vscode/src/pure/interface-types.ts @@ -422,6 +422,7 @@ export interface RemoteQueryDownloadAllAnalysesResultsMessage { export interface RemoteQueryExportResultsMessage { t: 'remoteQueryExportResults'; + queryId: string; } export interface CopyRepoListMessage { diff --git a/extensions/ql-vscode/src/query-history.ts b/extensions/ql-vscode/src/query-history.ts index bff2a76e9..4dd5e1c93 100644 --- a/extensions/ql-vscode/src/query-history.ts +++ b/extensions/ql-vscode/src/query-history.ts @@ -680,6 +680,10 @@ export class QueryHistoryManager extends DisposableObject { return this.treeDataProvider.getCurrent(); } + getRemoteQueryById(queryId: string): RemoteQueryHistoryItem | undefined { + return this.treeDataProvider.allHistory.find(i => i.t === 'remote' && i.queryId === queryId) as RemoteQueryHistoryItem; + } + async removeDeletedQueries() { await Promise.all(this.treeDataProvider.allHistory.map(async (item) => { if (item.t == 'local' && item.completedQuery && !(await fs.pathExists(item.completedQuery?.query.querySaveDir))) { diff --git a/extensions/ql-vscode/src/remote-queries/export-results.ts b/extensions/ql-vscode/src/remote-queries/export-results.ts index 3fc41f0f3..a3126f402 100644 --- a/extensions/ql-vscode/src/remote-queries/export-results.ts +++ b/extensions/ql-vscode/src/remote-queries/export-results.ts @@ -15,26 +15,41 @@ import { RemoteQueriesManager } from './remote-queries-manager'; import { generateMarkdown } from './remote-queries-markdown-generation'; import { RemoteQuery } from './remote-query'; import { AnalysisResults, sumAnalysesResults } from './shared/analysis-result'; +import { RemoteQueryHistoryItem } from './remote-query-history-item'; /** - * Exports the results of the currently-selected remote query. + * Exports the results of the given or currently-selected remote query. * The user is prompted to select the export format. */ export async function exportRemoteQueryResults( queryHistoryManager: QueryHistoryManager, remoteQueriesManager: RemoteQueriesManager, ctx: ExtensionContext, + queryId?: string, ): Promise { - const queryHistoryItem = queryHistoryManager.getCurrentQueryHistoryItem(); - if (!queryHistoryItem || queryHistoryItem.t !== 'remote') { - throw new Error('No variant analysis results currently open. To open results, click an item in the query history view.'); - } else if (!queryHistoryItem.completed) { + let queryHistoryItem: RemoteQueryHistoryItem; + if (queryId) { + const query = queryHistoryManager.getRemoteQueryById(queryId); + if (!query) { + void logger.log(`Could not find query with id ${queryId}`); + throw new Error('There was an error when trying to retrieve variant analysis information'); + } + queryHistoryItem = query; + } else { + const query = queryHistoryManager.getCurrentQueryHistoryItem(); + if (!query || query.t !== 'remote') { + throw new Error('No variant analysis results currently open. To open results, click an item in the query history view.'); + } + queryHistoryItem = query; + } + + if (!queryHistoryItem.completed) { throw new Error('Variant analysis results are not yet available.'); } - const queryId = queryHistoryItem.queryId; - void logger.log(`Exporting variant analysis results for query: ${queryId}`); + + void logger.log(`Exporting variant analysis results for query: ${queryHistoryItem.queryId}`); const query = queryHistoryItem.remoteQuery; - const analysesResults = remoteQueriesManager.getAnalysesResults(queryId); + const analysesResults = remoteQueriesManager.getAnalysesResults(queryHistoryItem.queryId); const gistOption = { label: '$(ports-open-browser-icon) Create Gist (GitHub)', diff --git a/extensions/ql-vscode/src/remote-queries/remote-queries-view.ts b/extensions/ql-vscode/src/remote-queries/remote-queries-view.ts index 64d5893be..4e48a357a 100644 --- a/extensions/ql-vscode/src/remote-queries/remote-queries-view.ts +++ b/extensions/ql-vscode/src/remote-queries/remote-queries-view.ts @@ -145,7 +145,7 @@ export class RemoteQueriesView extends AbstractWebview{totalAnalysesResults}/{totalResults} results; }; -const exportResults = () => { +const exportResults = (queryResult: RemoteQueryResult) => { vscode.postMessage({ t: 'remoteQueryExportResults', + queryId: queryResult.queryId, }); }; @@ -362,7 +363,7 @@ const AnalysesResults = ({ totalResults={totalResults} />
- Export all + exportResults(queryResult)}>Export all