Skip to content

Commit

Permalink
Task: create search or open api description command (#5520)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Oct 3, 2024
1 parent 1cc92e2 commit 1808c82
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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<Record<string, KiotaSearchResultItem> | undefined> {
return connectToKiota<Record<string, KiotaSearchResultItem>>(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, KiotaSearchResult, void>(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IntegrationParams>): Promise<void> {
// 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!));
}
}
}
59 changes: 12 additions & 47 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<IntegrationParams> = {};
context.subscriptions.push(
vscode.window.registerUriHandler({
handleUri: async (uri: vscode.Uri) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -237,7 +240,7 @@ export async function activate(
}
}

deepLinkParams = {}; // Clear the state after the generation
clearDeepLinkParams(); // Clear the state after the generation
}
}
),
Expand All @@ -250,47 +253,8 @@ export async function activate(
void context.workspaceState.update('generatedOutput', undefined);
}
}),
registerCommandWithTelemetry(
reporter,
`${treeViewId}.searchOrOpenApiDescription`,
async (
searchParams: Partial<IntegrationParams> = {}
) => {
// 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<IntegrationParams> = {}) => await searchOrOpenApiDescriptionCommand.execute(searchParams)
),
registerCommandWithTelemetry(reporter, `${treeViewId}.closeDescription`, async () => {
const yesAnswer = vscode.l10n.t("Yes");
Expand Down Expand Up @@ -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.'));
Expand Down
11 changes: 11 additions & 0 deletions vscode/microsoft-kiota/src/handlers/deepLinkParamsHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IntegrationParams } from "../utilities/deep-linking";

let deepLinkParams: Partial<IntegrationParams> = {};

export const getDeepLinkParams = () => deepLinkParams;
export const setDeepLinkParams = (params: Partial<IntegrationParams>) => {
deepLinkParams = { ...deepLinkParams, ...params };
};
export const clearDeepLinkParams = () => {
deepLinkParams = {};
};

0 comments on commit 1808c82

Please sign in to comment.