From 2d623e4de6d6258f7ab6e9905d135bbd73b28186 Mon Sep 17 00:00:00 2001 From: Prabal Deb Date: Fri, 6 Dec 2024 09:56:32 +0530 Subject: [PATCH] Copilot Publish Wrapper --- src/actions/copilotPublish.ts | 37 ++++++++++++++++ src/actions/index.ts | 1 + test/actions/copilotPublish.test.ts | 65 +++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/actions/copilotPublish.ts create mode 100644 test/actions/copilotPublish.test.ts diff --git a/src/actions/copilotPublish.ts b/src/actions/copilotPublish.ts new file mode 100644 index 00000000..d00d99fc --- /dev/null +++ b/src/actions/copilotPublish.ts @@ -0,0 +1,37 @@ +import { authenticateEnvironment, clearAuthentication } from "../pac/auth/authenticate"; +import createPacRunner from "../pac/createPacRunner"; +import { RunnerParameters } from "../Parameters"; +import { AuthCredentials } from "../pac/auth/authParameters"; +import { HostParameterEntry, IHostAbstractions, CommonActionParameters } from "../host/IHostAbstractions"; +import { InputValidator } from "../host/InputValidator"; + +export interface CopilotPublishParameters extends CommonActionParameters { + credentials: AuthCredentials; + environmentUrl: string; + botId: HostParameterEntry; +} + +export async function copilotPublish(parameters: CopilotPublishParameters, runnerParameters: RunnerParameters, host: IHostAbstractions): Promise { + const logger = runnerParameters.logger; + const pac = createPacRunner(runnerParameters); + + try { + const authenticateResult = await authenticateEnvironment(pac, parameters.credentials, parameters.environmentUrl, logger); + logger.log("The Authentication Result: " + authenticateResult); + + const pacArgs = ["copilot", "publish"]; + const validator = new InputValidator(host); + validator.pushInput(pacArgs, "--bot", parameters.botId); + validator.pushCommon(pacArgs, parameters); + + logger.log("Calling pac cli inputs: " + pacArgs.join(" ")); + const pacResult = await pac(...pacArgs); + logger.log("CopilotPublish Action Result: " + pacResult); + } catch (error) { + logger.error(`failed: ${error instanceof Error ? error.message : error}`); + throw error; + } finally { + const clearAuthResult = await clearAuthentication(pac); + logger.log("The Clear Authentication Result: " + clearAuthResult); + } +} diff --git a/src/actions/index.ts b/src/actions/index.ts index 66a5cd69..2c95e9b0 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -35,3 +35,4 @@ export * from './submitCatalog'; export * from "./pipelineDeploy"; export * from "./updateOrgSettings"; export * from "./setGovernanceConfig"; +export * from "./copilotPublish"; diff --git a/test/actions/copilotPublish.test.ts b/test/actions/copilotPublish.test.ts new file mode 100644 index 00000000..633a228a --- /dev/null +++ b/test/actions/copilotPublish.test.ts @@ -0,0 +1,65 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import rewiremock from "../rewiremock"; +import * as sinonChai from "sinon-chai"; +import * as chaiAsPromised from "chai-as-promised"; +import { should, use } from "chai"; +import { restore, stub } from "sinon"; +import { ClientCredentials, RunnerParameters } from "../../src"; +import { createDefaultMockRunnerParameters, createMockClientCredentials, mockEnvironmentUrl } from "./mock/mockData"; +import { CopilotPublishParameters } from "../../src/actions"; +import { mockHost } from "./mock/mockHost"; +import Sinon = require("sinon"); +import { IHostAbstractions } from "src/host/IHostAbstractions"; +should(); +use(sinonChai); +use(chaiAsPromised); + +describe("action: copilot publish", () => { + let pacStub: Sinon.SinonStub; + let authenticateEnvironmentStub: Sinon.SinonStub; + let clearAuthenticationStub: Sinon.SinonStub; + const mockClientCredentials: ClientCredentials = createMockClientCredentials(); + const environmentUrl: string = mockEnvironmentUrl; + + beforeEach(() => { + pacStub = stub(); + authenticateEnvironmentStub = stub(); + clearAuthenticationStub = stub(); + }); + afterEach(() => restore()); + + async function runActionWithMocks(copilotPublishParameters: CopilotPublishParameters, host: IHostAbstractions): Promise { + const runnerParameters: RunnerParameters = createDefaultMockRunnerParameters(); + const mockedActionModule = await rewiremock.around(() => import("../../src/actions/copilotPublish"), + (mock) => { + mock(() => import("../../src/pac/createPacRunner")).withDefault(() => pacStub); + mock(() => import("../../src/pac/auth/authenticate")).with( + { + authenticateEnvironment: authenticateEnvironmentStub, + clearAuthentication: clearAuthenticationStub + }); + }); + + authenticateEnvironmentStub.returns("Authentication successfully created."); + clearAuthenticationStub.returns("Authentication profiles and token cache removed"); + pacStub.returns(""); + await mockedActionModule.copilotPublish(copilotPublishParameters, runnerParameters, host); + } + + it("calls pac runner with correct arguments", async () => { + const copilotPublishParameters: CopilotPublishParameters = { + credentials: mockClientCredentials, + environmentUrl: environmentUrl, + botId: { name: "BotId", required: true }, + logToConsole: true + } + + const host = new mockHost(); + + await runActionWithMocks(copilotPublishParameters, host); + + authenticateEnvironmentStub.should.have.been.calledOnceWith(pacStub, mockClientCredentials, environmentUrl); + pacStub.should.have.been.calledOnceWith("copilot", "publish", "--bot", host.botId); + clearAuthenticationStub.should.have.been.calledOnceWith(pacStub); + }) +})