Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/actions/copilotPublish.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export * from './submitCatalog';
export * from "./pipelineDeploy";
export * from "./updateOrgSettings";
export * from "./setGovernanceConfig";
export * from "./copilotPublish";
65 changes: 65 additions & 0 deletions test/actions/copilotPublish.test.ts
Original file line number Diff line number Diff line change
@@ -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<any[],any>;
let authenticateEnvironmentStub: Sinon.SinonStub<any[], any>;
let clearAuthenticationStub: Sinon.SinonStub<any[], any>;
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<void> {
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);
})
})