From 1808c82cdae750cdbac9ab86df1a9bf32d311085 Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Thu, 3 Oct 2024 15:57:41 +0300 Subject: [PATCH] Task: create search or open api description command (#5520) --- .../searchDescription.ts | 3 +- .../searchOrOpenApiDescriptionCommand.ts | 68 +++++++++++++++++++ vscode/microsoft-kiota/src/extension.ts | 59 ++++------------ .../src/handlers/deepLinkParamsHandler.ts | 11 +++ 4 files changed, 93 insertions(+), 48 deletions(-) rename vscode/microsoft-kiota/src/{ => commands/open-api-tree-view/search-or-open-api-description}/searchDescription.ts (86%) create mode 100644 vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand.ts create mode 100644 vscode/microsoft-kiota/src/handlers/deepLinkParamsHandler.ts diff --git a/vscode/microsoft-kiota/src/searchDescription.ts b/vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchDescription.ts similarity index 86% rename from vscode/microsoft-kiota/src/searchDescription.ts rename to vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchDescription.ts index e69d9afc6a..c1b4e82839 100644 --- a/vscode/microsoft-kiota/src/searchDescription.ts +++ b/vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchDescription.ts @@ -1,7 +1,8 @@ -import { connectToKiota, KiotaSearchResult, KiotaSearchResultItem } from "./kiotaInterop"; import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; +import { KiotaSearchResultItem, connectToKiota, KiotaSearchResult } from "../../../kiotaInterop"; + export function searchDescription(context: vscode.ExtensionContext, searchTerm: string, clearCache: boolean): Promise | undefined> { return connectToKiota>(context, async (connection) => { const request = new rpc.RequestType2( diff --git a/vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand.ts b/vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand.ts new file mode 100644 index 0000000000..1f4c3ee157 --- /dev/null +++ b/vscode/microsoft-kiota/src/commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand.ts @@ -0,0 +1,68 @@ +import TelemetryReporter from "@vscode/extension-telemetry"; +import * as vscode from "vscode"; + +import { extensionId, treeViewId } from "../../../constants"; +import { getExtensionSettings } from "../../../extensionSettings"; +import { setDeepLinkParams } from "../../../handlers/deepLinkParamsHandler"; +import { OpenApiTreeProvider } from "../../../openApiTreeProvider"; +import { searchSteps } from "../../../steps"; +import { IntegrationParams, validateDeepLinkQueryParams } from "../../../utilities/deep-linking"; +import { Command } from "../../Command"; +import { searchDescription } from "./searchDescription"; +import { openTreeViewWithProgress } from "../../../utilities/progress"; + +export class SearchOrOpenApiDescriptionCommand extends Command { + + private _openApiTreeProvider: OpenApiTreeProvider; + private _context: vscode.ExtensionContext; + + constructor(openApiTreeProvider: OpenApiTreeProvider, context: vscode.ExtensionContext) { + super(); + this._openApiTreeProvider = openApiTreeProvider; + this._context = context; + } + + public getName(): string { + return `${treeViewId}.searchOrOpenApiDescription`; + } + + public async execute(searchParams: Partial): Promise { + // set deeplink params if exists + if (Object.keys(searchParams).length > 0) { + let [params, errorsArray] = validateDeepLinkQueryParams(searchParams); + setDeepLinkParams(params); + const reporter = new TelemetryReporter(this._context.extension.packageJSON.telemetryInstrumentationKey); + reporter.sendTelemetryEvent("DeepLinked searchOrOpenApiDescription", { + "searchParameters": JSON.stringify(searchParams), + "validationErrors": errorsArray.join(", ") + }); + } + + // proceed to enable loading of openapi description + const yesAnswer = vscode.l10n.t("Yes, override it"); + if (this._openApiTreeProvider.hasChanges()) { + const response = await vscode.window.showWarningMessage( + vscode.l10n.t( + "Before adding a new API description, consider that your changes and current selection will be lost."), + yesAnswer, + vscode.l10n.t("Cancel") + ); + if (response !== yesAnswer) { + return; + } + } + + const config = await searchSteps(x => vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + cancellable: false, + title: vscode.l10n.t("Searching...") + }, (progress, _) => { + const settings = getExtensionSettings(extensionId); + return searchDescription(this._context, x, settings.clearCache); + })); + + if (config.descriptionPath) { + await openTreeViewWithProgress(() => this._openApiTreeProvider.setDescriptionUrl(config.descriptionPath!)); + } + } +} diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index ed20965d37..564d3fd57d 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -14,6 +14,7 @@ import { FilterDescriptionCommand } from './commands/open-api-tree-view/filterDe import { OpenDocumentationPageCommand } from './commands/open-api-tree-view/openDocumentationPageCommand'; import { RemoveAllFromSelectedEndpointsCommand } from './commands/open-api-tree-view/removeAllFromSelectedEndpointsCommand'; import { RemoveFromSelectedEndpointsCommand } from './commands/open-api-tree-view/removeFromSelectedEndpointsCommand'; +import { SearchOrOpenApiDescriptionCommand } from './commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand'; import { KIOTA_WORKSPACE_FILE, dependenciesInfo, extensionId, statusBarCommandId, treeViewFocusCommand, treeViewId } from "./constants"; import { DependenciesViewProvider } from "./dependenciesViewProvider"; import { GenerationType, KiotaGenerationLanguage, KiotaPluginType } from "./enums"; @@ -22,6 +23,7 @@ import { generateClient } from "./generateClient"; import { generatePlugin } from "./generatePlugin"; import { getKiotaVersion } from "./getKiotaVersion"; import { getLanguageInformation, getLanguageInformationForDescription } from "./getLanguageInformation"; +import { clearDeepLinkParams, getDeepLinkParams, setDeepLinkParams } from './handlers/deepLinkParamsHandler'; import { ClientOrPluginProperties, ConsumerOperation, @@ -32,8 +34,7 @@ import { } from "./kiotaInterop"; import { checkForLockFileAndPrompt } from "./migrateFromLockFile"; import { OpenApiTreeNode, OpenApiTreeProvider } from "./openApiTreeProvider"; -import { searchDescription } from "./searchDescription"; -import { GenerateState, generateSteps, searchSteps } from "./steps"; +import { GenerateState, generateSteps } from "./steps"; import { updateClients } from "./updateClients"; import { getSanitizedString, getWorkspaceJsonDirectory, getWorkspaceJsonPath, @@ -78,11 +79,11 @@ export async function activate( const filterDescriptionCommand = new FilterDescriptionCommand(openApiTreeProvider); const openDocumentationPageCommand = new OpenDocumentationPageCommand(); const editPathsCommand = new EditPathsCommand(openApiTreeProvider); + const searchOrOpenApiDescriptionCommand = new SearchOrOpenApiDescriptionCommand(openApiTreeProvider, context); await loadTreeView(context); await checkForLockFileAndPrompt(context); let codeLensProvider = new CodeLensProvider(); - let deepLinkParams: Partial = {}; context.subscriptions.push( vscode.window.registerUriHandler({ handleUri: async (uri: vscode.Uri) => { @@ -91,13 +92,14 @@ export async function activate( } const queryParameters = getQueryParameters(uri); if (uri.path.toLowerCase() === "/opendescription") { - let errorsArray: string[]; - [deepLinkParams, errorsArray] = validateDeepLinkQueryParams(queryParameters); + let [params, errorsArray] = validateDeepLinkQueryParams(queryParameters); + setDeepLinkParams(params); reporter.sendTelemetryEvent("DeepLink.OpenDescription initialization status", { "queryParameters": JSON.stringify(queryParameters), "validationErrors": errorsArray.join(", ") }); + let deepLinkParams = getDeepLinkParams(); if (deepLinkParams.descriptionurl) { await openTreeViewWithProgress(() => openApiTreeProvider.setDescriptionUrl(deepLinkParams.descriptionurl!)); return; @@ -140,6 +142,7 @@ export async function activate( registerCommandWithTelemetry(reporter, `${treeViewId}.generateClient`, async () => { + const deepLinkParams = getDeepLinkParams(); const selectedPaths = openApiTreeProvider.getSelectedPaths(); if (selectedPaths.length === 0) { await vscode.window.showErrorMessage( @@ -237,7 +240,7 @@ export async function activate( } } - deepLinkParams = {}; // Clear the state after the generation + clearDeepLinkParams(); // Clear the state after the generation } } ), @@ -250,47 +253,8 @@ export async function activate( void context.workspaceState.update('generatedOutput', undefined); } }), - registerCommandWithTelemetry( - reporter, - `${treeViewId}.searchOrOpenApiDescription`, - async ( - searchParams: Partial = {} - ) => { - // set deeplink params if exists - if (Object.keys(searchParams).length > 0) { - let errorsArray: string[]; - [deepLinkParams, errorsArray] = validateDeepLinkQueryParams(searchParams); - reporter.sendTelemetryEvent("DeepLinked searchOrOpenApiDescription", { - "searchParameters": JSON.stringify(searchParams), - "validationErrors": errorsArray.join(", ") - }); - } - - // proceed to enable loading of openapi description - const yesAnswer = vscode.l10n.t("Yes, override it"); - if (!openApiTreeProvider.isEmpty() && openApiTreeProvider.hasChanges()) { - const response = await vscode.window.showWarningMessage( - vscode.l10n.t( - "Before adding a new API description, consider that your changes and current selection will be lost."), - yesAnswer, - vscode.l10n.t("Cancel") - ); - if (response !== yesAnswer) { - return; - } - } - const config = await searchSteps(x => vscode.window.withProgress({ - location: vscode.ProgressLocation.Notification, - cancellable: false, - title: vscode.l10n.t("Searching...") - }, (progress, _) => { - const settings = getExtensionSettings(extensionId); - return searchDescription(context, x, settings.clearCache); - })); - if (config.descriptionPath) { - await openTreeViewWithProgress(() => openApiTreeProvider.setDescriptionUrl(config.descriptionPath!)); - } - } + registerCommandWithTelemetry(reporter, searchOrOpenApiDescriptionCommand.getName(), + async (searchParams: Partial = {}) => await searchOrOpenApiDescriptionCommand.execute(searchParams) ), registerCommandWithTelemetry(reporter, `${treeViewId}.closeDescription`, async () => { const yesAnswer = vscode.l10n.t("Yes"); @@ -450,6 +414,7 @@ export async function activate( if (!isSuccess) { await exportLogsAndShowErrors(result); } + const deepLinkParams = getDeepLinkParams(); const isttkIntegration = deepLinkParams.source && deepLinkParams.source.toLowerCase() === 'ttk'; if (!isttkIntegration) { void vscode.window.showInformationMessage(vscode.l10n.t('Plugin generated successfully.')); diff --git a/vscode/microsoft-kiota/src/handlers/deepLinkParamsHandler.ts b/vscode/microsoft-kiota/src/handlers/deepLinkParamsHandler.ts new file mode 100644 index 0000000000..5ed9b6bc56 --- /dev/null +++ b/vscode/microsoft-kiota/src/handlers/deepLinkParamsHandler.ts @@ -0,0 +1,11 @@ +import { IntegrationParams } from "../utilities/deep-linking"; + +let deepLinkParams: Partial = {}; + +export const getDeepLinkParams = () => deepLinkParams; +export const setDeepLinkParams = (params: Partial) => { + deepLinkParams = { ...deepLinkParams, ...params }; +}; +export const clearDeepLinkParams = () => { + deepLinkParams = {}; +}; \ No newline at end of file