Skip to content

Commit

Permalink
fix: trim manifest short name (#12443)
Browse files Browse the repository at this point in the history
* fix: trim manifest short name

* fix: trim manifest short name

* fix: unite test for create
  • Loading branch information
jayzhang authored Sep 25, 2024
1 parent 1804b92 commit 488cc0b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
7 changes: 6 additions & 1 deletion packages/fx-core/src/component/coordinator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as path from "path";
import * as uuid from "uuid";
import * as xml2js from "xml2js";
import { AppStudioScopes, getResourceGroupInPortal } from "../../common/constants";
import { FeatureFlags, featureFlagManager } from "../../common/featureFlags";
import { ErrorContextMW, globalVars } from "../../common/globalVars";
import { getLocalizedString } from "../../common/localizeUtils";
import { convertToAlphanumericOnly } from "../../common/stringUtils";
Expand Down Expand Up @@ -54,6 +55,7 @@ import { developerPortalScaffoldUtils } from "../developerPortalScaffoldUtils";
import { DriverContext } from "../driver/interface/commonArgs";
import { updateTeamsAppV3ForPublish } from "../driver/teamsApp/appStudio";
import { Constants } from "../driver/teamsApp/constants";
import { manifestUtils } from "../driver/teamsApp/utils/ManifestUtils";
import { Generator } from "../generator/generator";
import { Generators } from "../generator/generatorProvider";
import { ActionContext, ActionExecutionMW } from "../middleware/actionExecutionMW";
Expand All @@ -64,7 +66,6 @@ import { metadataUtil } from "../utils/metadataUtil";
import { pathUtils } from "../utils/pathUtils";
import { settingsUtil } from "../utils/settingsUtil";
import { SummaryReporter } from "./summary";
import { featureFlagManager, FeatureFlags } from "../../common/featureFlags";

const M365Actions = [
"botAadApp/create",
Expand Down Expand Up @@ -215,6 +216,10 @@ class Coordinator {
return err(res.error);
}
}

const trimRes = await manifestUtils.trimManifestShortName(projectPath);
if (trimRes.isErr()) return err(trimRes.error);

return ok({ projectPath: projectPath, warnings });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.
import { hooks } from "@feathersjs/hooks";
import {
Context,
FxError,
IComposeExtension,
IMessagingExtensionCommand,
Expand All @@ -21,35 +20,34 @@ import "reflect-metadata";
import stripBom from "strip-bom";
import { v4 } from "uuid";
import isUUID from "validator/lib/isUUID";
import { getCapabilities as checkManifestCapabilities } from "../../../../common/projectTypeChecker";
import { ErrorContextMW } from "../../../../common/globalVars";
import { getCapabilities as checkManifestCapabilities } from "../../../../common/projectTypeChecker";
import { FileNotFoundError, JSONSyntaxError, ReadFileError } from "../../../../error/common";
import { CapabilityOptions } from "../../../../question/constants";
import { BotScenario } from "../../../constants";
import { convertManifestTemplateToV2, convertManifestTemplateToV3 } from "../../../migrate";
import { expandEnvironmentVariable } from "../../../utils/common";
import { WrapDriverContext } from "../../util/wrapUtil";
import { ManifestType } from "../../../utils/envFunctionUtils";
import { DriverContext } from "../../interface/commonArgs";
import {
getBotsTplExistingAppBasedOnVersion,
getBotsTplForCommandAndResponseBasedOnVersion,
getBotsTplForNotificationBasedOnVersion,
getBotsTplBasedOnVersion,
COMPOSE_EXTENSIONS_TPL_EXISTING_APP,
COMPOSE_EXTENSIONS_TPL_M365_V3,
COMPOSE_EXTENSIONS_TPL_V3,
getConfigurableTabsTplExistingAppBasedOnVersion,
getConfigurableTabsTplBasedOnVersion,
Constants,
STATIC_TABS_MAX_ITEMS,
STATIC_TABS_TPL_EXISTING_APP,
STATIC_TABS_TPL_V3,
WEB_APPLICATION_INFO_V3,
getBotsTplBasedOnVersion,
getBotsTplExistingAppBasedOnVersion,
getBotsTplForCommandAndResponseBasedOnVersion,
getBotsTplForNotificationBasedOnVersion,
getConfigurableTabsTplBasedOnVersion,
getConfigurableTabsTplExistingAppBasedOnVersion,
} from "../constants";
import { AppStudioError } from "../errors";
import { AppStudioResultFactory } from "../results";
import { getResolvedManifest } from "./utils";
import { ManifestType } from "../../../utils/envFunctionUtils";
import { DriverContext } from "../../interface/commonArgs";

export class ManifestUtils {
async readAppManifest(projectPath: string): Promise<Result<TeamsAppManifest, FxError>> {
Expand Down Expand Up @@ -393,6 +391,32 @@ export class ManifestUtils {
const manifest = JSON.parse(manifestString) as TeamsAppManifest;
return ok(manifest);
}

/**
* trim the short name in manifest to make sure it is no more than 25 length
*/
async trimManifestShortName(
projectPath: string,
maxLength = 25
): Promise<Result<undefined, FxError>> {
const manifestPath = this.getTeamsAppManifestPath(projectPath);
const manifest = (await fs.readJson(manifestPath)) as TeamsAppManifest;
const shortName = manifest.name.short;
let hasSuffix = false;
let trimmedName = shortName;
if (shortName.includes("${{APP_NAME_SUFFIX}}")) {
hasSuffix = true;
trimmedName = shortName.replace("${{APP_NAME_SUFFIX}}", "");
}
if (trimmedName.length <= maxLength) return ok(undefined);
let newShortName = trimmedName.replace(/\s/g, "").slice(0, maxLength);
if (hasSuffix) {
newShortName += "${{APP_NAME_SUFFIX}}";
}
manifest.name.short = newShortName;
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
return ok(undefined);
}
}

export const manifestUtils = new ManifestUtils();
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { MockTools, randomAppName } from "../../core/utils";
import { MockedUserInteraction } from "../../plugins/solution/util";
import mockedEnv, { RestoreFn } from "mocked-env";
import { FeatureFlagName } from "../../../src/common/featureFlags";
import { manifestUtils } from "../../../src/component/driver/teamsApp/utils/ManifestUtils";

describe("coordinator create", () => {
const sandbox = sinon.createSandbox();
Expand All @@ -43,6 +44,7 @@ describe("coordinator create", () => {
let mockedEnvRestore: RestoreFn;
beforeEach(() => {
sandbox.stub(fs, "ensureDir").resolves();
sandbox.stub(manifestUtils, "trimManifestShortName").resolves(ok(undefined));
generator = sandbox
.stub(DefaultTemplateGenerator.prototype, <any>"scaffolding")
.resolves(ok(undefined));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,39 @@ describe("readAppManifestSync", () => {
assert.isTrue(res.isErr() && res.error instanceof ReadFileError);
});
});

describe("trimManifestShortName", () => {
const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});

it("Success", async () => {
const teamsManifest = new TeamsAppManifest();
teamsManifest.name.short = "shortname abcdefghijklmn123456${{APP_NAME_SUFFIX}}";
sandbox.stub(fs, "readJson").resolves(teamsManifest);
sandbox.stub(fs, "writeFile").resolves();
const res = await manifestUtils.trimManifestShortName("projectPath");
assert.isTrue(res.isOk());
assert.equal(teamsManifest.name.short, "shortnameabcdefghijklmn12${{APP_NAME_SUFFIX}}");
});
it("Success no suffix", async () => {
const teamsManifest = new TeamsAppManifest();
teamsManifest.name.short = "shortname abcdefghijklmn123456";
sandbox.stub(fs, "readJson").resolves(teamsManifest);
sandbox.stub(fs, "writeFile").resolves();
const res = await manifestUtils.trimManifestShortName("projectPath");
assert.isTrue(res.isOk());
assert.equal(teamsManifest.name.short, "shortnameabcdefghijklmn12");
});
it("No need to trim", async () => {
const teamsManifest = new TeamsAppManifest();
teamsManifest.name.short = "shortname abcdefghijklmn${{APP_NAME_SUFFIX}}";
sandbox.stub(fs, "readJson").resolves(teamsManifest);
sandbox.stub(fs, "writeFile").resolves();
const res = await manifestUtils.trimManifestShortName("projectPath");
assert.isTrue(res.isOk());
assert.equal(teamsManifest.name.short, "shortname abcdefghijklmn${{APP_NAME_SUFFIX}}");
});
});

0 comments on commit 488cc0b

Please sign in to comment.