From 57af4046bac186575375053e4b1d290e9c0a75af Mon Sep 17 00:00:00 2001 From: Sebastian Ma Date: Wed, 10 Jul 2024 21:24:13 -0700 Subject: [PATCH] Switch to bundle API for validation (#50) --- package.json | 13 ++------- src/api.ts | 20 +++++++++++++ src/commands/validate-add.ts | 41 --------------------------- src/commands/validate-rab-bundle.ts | 44 +++++++++++++++++++++++++++++ src/extension.ts | 4 +-- 5 files changed, 68 insertions(+), 54 deletions(-) delete mode 100644 src/commands/validate-add.ts create mode 100644 src/commands/validate-rab-bundle.ts diff --git a/package.json b/package.json index bec8e02..0c2999e 100644 --- a/package.json +++ b/package.json @@ -89,8 +89,8 @@ "title": "Download" }, { - "command": "orab.add.validate", - "title": "RAB: Validate" + "command": "orab.bundle.validate", + "title": "RAB: Validate RAB Bundle" }, { "command": "orab.add.version-check", @@ -159,10 +159,6 @@ "command": "orab.bundle.delete", "when": "false" }, - { - "command": "orab.add.validate", - "when": "false" - }, { "command": "orab.add.version-check", "when": "false" @@ -209,11 +205,6 @@ } ], "explorer/context": [ - { - "when": "resourceFilename =~ /\\.add\\.json$/", - "command": "orab.add.validate", - "group": "orab" - }, { "when": "resourceFilename =~ /\\.add\\.json$/", "command": "orab.add.version-check", diff --git a/src/api.ts b/src/api.ts index d571ce9..2053fc6 100644 --- a/src/api.ts +++ b/src/api.ts @@ -300,6 +300,26 @@ export namespace bundle { } } + export async function validate(bundle: Buffer): Promise { + + let url = `${apiRootPath}/${resource}/validate`; + log.debug(`Calling '${url}'`); + try { + let res = await callAPI(async () => (await getClient()).post(url, bundle, { + headers: { + 'Content-Type': 'application/zip', + }, + }) as Promise>); + logInfoServer(res?.data); + return res.data; + } catch (err) { + if (err instanceof AxiosError && err.response?.status !== 404) { + logInfoServer(err.response?.data); + } + throw new RABError(`Failed to call 'POST ${url}'`, err); + } + } + } /** diff --git a/src/commands/validate-add.ts b/src/commands/validate-add.ts deleted file mode 100644 index 3ff7636..0000000 --- a/src/commands/validate-add.ts +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright © 2022-2024, Oracle and/or its affiliates. - * This software is licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl. - */ - -import * as vscode from 'vscode'; - -import path from 'path'; - -import { firstValueFrom } from 'rxjs'; -import * as api from '../api'; -import { log } from '../logger'; -import { fs } from '../utils'; -import { RABError, showErrorMessage, showInfoMessage } from '../utils/ui-utils'; - -async function callback(file: vscode.Uri, context: vscode.ExtensionContext) { - - const isSaved = await firstValueFrom(fs.confirmSaveFile(file, true)); - if (!isSaved) { - return; - } - - log.info(`Validating adapter definition ${path.basename(file.fsPath)}...`); - - try { - let report = await api.registration.validateAdd(file); - if (report.valid) { - showInfoMessage("👍 Good job. Your adapter definition document is valid."); - } else { - showErrorMessage("❌ Your adapter definition document is invalid. See OUTPUT for more."); - } - } catch (err) { - showErrorMessage(new RABError('Cannot validate adapter definition.', err)); - } -} - -export function register(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand("orab.add.validate", callback) - ); -} \ No newline at end of file diff --git a/src/commands/validate-rab-bundle.ts b/src/commands/validate-rab-bundle.ts new file mode 100644 index 0000000..09ad92e --- /dev/null +++ b/src/commands/validate-rab-bundle.ts @@ -0,0 +1,44 @@ +/** + * Copyright © 2022-2024, Oracle and/or its affiliates. + * This software is licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl. + */ + +import * as vscode from 'vscode'; + + +import * as api from '../api'; +import { log } from '../logger'; +import { get as getProfileManager } from '../profile-manager-provider'; +import { RABError, showErrorMessage, showInfoMessage, withProgress } from '../utils/ui-utils'; +import { createRABBundle } from '../workspace-manager'; + +async function callback(file: vscode.Uri, context: vscode.ExtensionContext): Promise { + + withProgress("Validating RAB bundle...", async () => { + log.info(`Validating RAB bundle...`); + let bundle; + try { + bundle = await createRABBundle(); + } catch (err) { + showErrorMessage(err); return; + } + + let profileName = (await getProfileManager().active())?.name; + log.info(`Using the publisher profile '${profileName}'`); + + try { + let ret; + ret = await api.bundle.validate(bundle.content); + showInfoMessage(`Adapter '${bundle.id}' is ${ret.valid ? 'valid' : 'invalid'}.`); + } catch (err) { + showErrorMessage(new RABError('Cannot validate the bundle.', err)); + } + }); +} + +export function register(context: vscode.ExtensionContext) { + + context.subscriptions.push( + vscode.commands.registerCommand("orab.bundle.validate", callback) + ); +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 87a4245..09a7aac 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -12,6 +12,7 @@ import * as addLocate from './commands/add-locate'; import * as downloadRABBundle from './commands/download-rab-bundle'; import * as registerRABBundle from './commands/register-rab-bundle'; import * as removeRABBundle from './commands/remove-rab-bundle'; +import * as validateRABBundle from './commands/validate-rab-bundle'; import * as createRabBundle from './commands/create-rab-bundle'; import * as importRABBundle from './commands/import-rab-bundle'; @@ -21,7 +22,6 @@ import * as explorerOutlineTriggersNew from './commands/explorer-outline-trigger import * as initWorkspace from './commands/init-workspace'; import * as insertSecurityPolicy from './commands/insert-security-policy'; import * as insertTestConnection from './commands/insert-test-connection'; -import * as validateAdd from './commands/validate-add'; import * as versionCheck from './commands/version-check'; import * as addListProvider from './providers/add-list-provider'; @@ -41,9 +41,9 @@ async function registerCommands(context: vscode.ExtensionContext) { insertSecurityPolicy.register(context); insertTestConnection.register(context); registerRABBundle.register(context); - validateAdd.register(context); versionCheck.register(context); createRabBundle.register(context); + validateRABBundle.register(context); importRABBundle.register(context); }