diff --git a/extensions/ql-vscode/src/compare/compare-view.ts b/extensions/ql-vscode/src/compare/compare-view.ts index 977876f6f..ed5b2b620 100644 --- a/extensions/ql-vscode/src/compare/compare-view.ts +++ b/extensions/ql-vscode/src/compare/compare-view.ts @@ -26,6 +26,7 @@ import { import { telemetryListener } from "../common/vscode/telemetry"; import { redactableError } from "../common/errors"; import { App } from "../common/app"; +import { findResultSetNames } from "./result-set-names"; interface ComparePair { from: CompletedLocalQueryInfo; @@ -170,53 +171,33 @@ export class CompareView extends AbstractWebview< to: CompletedLocalQueryInfo, selectedResultSetName: string | undefined, ): Promise<[string[], string, RawResultSet, RawResultSet]> { - const fromSchemas = await this.cliServer.bqrsInfo( - from.completedQuery.query.resultsPaths.resultsPath, - ); - const toSchemas = await this.cliServer.bqrsInfo( - to.completedQuery.query.resultsPaths.resultsPath, - ); - const fromSchemaNames = fromSchemas["result-sets"].map( - (schema) => schema.name, - ); - const toSchemaNames = toSchemas["result-sets"].map((schema) => schema.name); - const commonResultSetNames = fromSchemaNames.filter((name) => - toSchemaNames.includes(name), + const { + commonResultSetNames, + currentResultSetDisplayName, + fromSchemas, + fromResultSetName, + toSchemas, + toResultSetName, + } = await findResultSetNames( + this.cliServer, + from, + to, + selectedResultSetName, ); - // Fall back on the default result set names if there are no common ones. - const defaultFromResultSetName = fromSchemaNames.find((name) => - name.startsWith("#"), - ); - const defaultToResultSetName = toSchemaNames.find((name) => - name.startsWith("#"), - ); - - if ( - commonResultSetNames.length === 0 && - !(defaultFromResultSetName || defaultToResultSetName) - ) { - throw new Error( - "No common result sets found between the two queries. Please check that the queries are compatible.", - ); - } - - const currentResultSetName = - selectedResultSetName || commonResultSetNames[0]; const fromResultSet = await this.getResultSet( fromSchemas, - currentResultSetName || defaultFromResultSetName!, + fromResultSetName, from.completedQuery.query.resultsPaths.resultsPath, ); const toResultSet = await this.getResultSet( toSchemas, - currentResultSetName || defaultToResultSetName!, + toResultSetName, to.completedQuery.query.resultsPaths.resultsPath, ); return [ commonResultSetNames, - currentResultSetName || - `${defaultFromResultSetName} <-> ${defaultToResultSetName}`, + currentResultSetDisplayName, fromResultSet, toResultSet, ]; diff --git a/extensions/ql-vscode/src/compare/result-set-names.ts b/extensions/ql-vscode/src/compare/result-set-names.ts new file mode 100644 index 000000000..c0fa5dd6b --- /dev/null +++ b/extensions/ql-vscode/src/compare/result-set-names.ts @@ -0,0 +1,55 @@ +import { CompletedLocalQueryInfo } from "../query-results"; +import { CodeQLCliServer } from "../codeql-cli/cli"; + +export async function findResultSetNames( + cliServer: CodeQLCliServer, + from: CompletedLocalQueryInfo, + to: CompletedLocalQueryInfo, + selectedResultSetName: string | undefined, +) { + const fromSchemas = await cliServer.bqrsInfo( + from.completedQuery.query.resultsPaths.resultsPath, + ); + const toSchemas = await cliServer.bqrsInfo( + to.completedQuery.query.resultsPaths.resultsPath, + ); + const fromSchemaNames = fromSchemas["result-sets"].map( + (schema) => schema.name, + ); + const toSchemaNames = toSchemas["result-sets"].map((schema) => schema.name); + const commonResultSetNames = fromSchemaNames.filter((name) => + toSchemaNames.includes(name), + ); + + // Fall back on the default result set names if there are no common ones. + const defaultFromResultSetName = fromSchemaNames.find((name) => + name.startsWith("#"), + ); + const defaultToResultSetName = toSchemaNames.find((name) => + name.startsWith("#"), + ); + + if ( + commonResultSetNames.length === 0 && + !(defaultFromResultSetName || defaultToResultSetName) + ) { + throw new Error( + "No common result sets found between the two queries. Please check that the queries are compatible.", + ); + } + + const currentResultSetName = selectedResultSetName || commonResultSetNames[0]; + const fromResultSetName = currentResultSetName || defaultFromResultSetName!; + const toResultSetName = currentResultSetName || defaultToResultSetName!; + + return { + commonResultSetNames, + currentResultSetDisplayName: + currentResultSetName || + `${defaultFromResultSetName} <-> ${defaultToResultSetName}`, + fromSchemas, + fromResultSetName, + toSchemas, + toResultSetName, + }; +}