Skip to content

feat: teamsApp/extendToM365 action supports scope argument #14400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions packages/fx-core/resource/yaml-schema/yaml.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,11 @@
"appPackagePath": {
"type": "string",
"description": "path to app package"
},
"scope": {
"type": "string",
"description": "The scope of the app. personal is only visible to the owners. shared is visible to other users in the tenant.",
"enum": ["personal", "shared"]
}
}
},
Expand All @@ -1401,6 +1406,10 @@
"appId": {
"description": "Required. The app ID of M365 title.",
"$ref": "#/definitions/envVarName"
},
"shareLink": {
"description": "Optional. The share link of the app.",
"$ref": "#/definitions/envVarName"
}
}
}
Expand Down
32 changes: 25 additions & 7 deletions packages/fx-core/src/component/driver/m365/acquire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { hooks } from "@feathersjs/hooks/lib";
import { FxError, Result, SystemError, UserError } from "@microsoft/teamsfx-api";

import { getLocalizedString } from "../../../common/localizeUtils";
import { PackageService } from "../../m365/packageService";
import { MosServiceEndpoint, MosServiceScope } from "../../m365/serviceConstant";
import { FileNotFoundError, InvalidActionInputError, assembleError } from "../../../error/common";
import { AppScope, PackageService } from "../../m365/packageService";
import { MosServiceEndpoint, MosServiceScope } from "../../m365/serviceConstant";
import { getAbsolutePath, wrapRun } from "../../utils/common";
import { logMessageKeys } from "../aad/utility/constants";
import { DriverContext } from "../interface/commonArgs";
Expand All @@ -19,6 +19,7 @@ import { addStartAndEndTelemetry } from "../middleware/addStartAndEndTelemetry";

interface AcquireArgs {
appPackagePath?: string; // The path of the app package
scope?: AppScope;
}

const actionName = "teamsApp/extendToM365";
Expand All @@ -27,6 +28,7 @@ const helpLink = "https://aka.ms/teamsfx-actions/teamsapp-extendToM365";
const outputKeys = {
titleId: "titleId",
appId: "appId",
shareLink: "shareLink",
};

@Service(actionName) // DO NOT MODIFY the service name
Expand Down Expand Up @@ -92,13 +94,20 @@ export class M365TitleAcquireDriver implements StepDriver {
throw sideloadingTokenRes.error;
}
const sideloadingToken = sideloadingTokenRes.value;
const sideloadingRes = await packageService.sideLoading(sideloadingToken, appPackagePath);
const sideloadingRes = await packageService.sideLoading(
sideloadingToken,
appPackagePath,
args.scope
);

const mapping = new Map<string, string>();
mapping.set(outputEnvVarNames!.get(outputKeys.titleId)!, sideloadingRes[0]);
mapping.set(outputEnvVarNames!.get(outputKeys.appId)!, sideloadingRes[1]);
if (outputEnvVarNames?.get(outputKeys.shareLink)) {
mapping.set(outputEnvVarNames.get(outputKeys.shareLink)!, sideloadingRes[2]);
}
return {
output: new Map([
[outputEnvVarNames!.get(outputKeys.titleId)!, sideloadingRes[0]],
[outputEnvVarNames!.get(outputKeys.appId)!, sideloadingRes[1]],
]),
output: mapping,
summaries: [getLocalizedString("driver.m365.acquire.summary", sideloadingRes[0])],
};
} catch (error) {
Expand All @@ -124,6 +133,15 @@ export class M365TitleAcquireDriver implements StepDriver {
invalidParameters.push("appPackagePath");
}

if (
args.scope &&
!Object.values(AppScope)
.map((v) => v.toLowerCase())
.includes(args.scope)
) {
invalidParameters.push("scope");
}

if (invalidParameters.length > 0) {
throw new InvalidActionInputError(actionName, invalidParameters, helpLink);
}
Expand Down
163 changes: 159 additions & 4 deletions packages/fx-core/tests/component/driver/m365/acquire.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "mocha";
import * as sinon from "sinon";
import chai from "chai";
import fs from "fs-extra";
import { PackageService } from "../../../../src/component/m365/packageService";
import "mocha";
import * as sinon from "sinon";
import { M365TitleAcquireDriver } from "../../../../src/component/driver/m365/acquire";
import { MockedLogProvider, MockedUserInteraction } from "../../../plugins/solution/util";
import { PackageService } from "../../../../src/component/m365/packageService";
import {
FileNotFoundError,
InvalidActionInputError,
UnhandledError,
} from "../../../../src/error/common";
import { MockedM365Provider } from "../../../core/utils";
import { MockedLogProvider, MockedUserInteraction } from "../../../plugins/solution/util";

describe("teamsApp/extendToM365", async () => {
const acquireDriver = new M365TitleAcquireDriver();
Expand Down Expand Up @@ -153,4 +153,159 @@ describe("teamsApp/extendToM365", async () => {
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
});

it("execute: invalid scope parameter", async () => {
const args = {
appPackagePath: "fakePath",
scope: "invalid-scope" as any,
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert(result.result.isErr());
if (result.result.isErr()) {
chai.assert.isTrue(result.result.error instanceof InvalidActionInputError);
chai.assert.isTrue(result.result.error.message.includes("scope"));
}
});

it("execute: valid scope personal", async () => {
const args = {
appPackagePath: "fakePath",
scope: "personal" as any,
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", "https://example.com/sharelink"]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
});

it("execute: valid scope shared", async () => {
const args = {
appPackagePath: "fakePath",
scope: "shared" as any,
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", "https://example.com/sharelink"]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
});

it("execute: with shareLink output key", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
["shareLink", "MY_SHARE_LINK"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", "https://example.com/sharelink"]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
chai.assert.equal(
(result.result as any).value.get("MY_SHARE_LINK"),
"https://example.com/sharelink"
);
});

it("execute: without shareLink output key should not include it in result", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", "https://example.com/sharelink"]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
chai.assert.isFalse((result.result as any).value.has("MY_SHARE_LINK"));
chai.assert.equal((result.result as any).value.size, 2);
});

it("execute: with scope and shareLink together", async () => {
const args = {
appPackagePath: "fakePath",
scope: "shared" as any,
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
["shareLink", "MY_SHARE_LINK"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", "https://example.com/sharelink"]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
chai.assert.equal(
(result.result as any).value.get("MY_SHARE_LINK"),
"https://example.com/sharelink"
);
});

it("execute: empty shareLink from service should still be set", async () => {
const args = {
appPackagePath: "fakePath",
};
const outputEnvVarNames = new Map([
["titleId", "MY_TITLE_ID"],
["appId", "MY_APP_ID"],
["shareLink", "MY_SHARE_LINK"],
]);

sinon
.stub(PackageService.prototype, "sideLoading")
.resolves(["test-title-id", "test-app-id", ""]);
sinon.stub(fs, "pathExists").resolves(true);

const result = await acquireDriver.execute(args, mockedDriverContext, outputEnvVarNames);
chai.assert.isTrue(result.result.isOk());
chai.assert.equal((result.result as any).value.get("MY_TITLE_ID"), "test-title-id");
chai.assert.equal((result.result as any).value.get("MY_APP_ID"), "test-app-id");
chai.assert.equal((result.result as any).value.get("MY_SHARE_LINK"), "");
});
});