Skip to content

Commit

Permalink
fix: add cli telemetry "account show" (#9273)
Browse files Browse the repository at this point in the history
* fix: add cli telemetry account show

* test: ut
  • Loading branch information
jayzhang authored Jul 12, 2023
1 parent 24f52e6 commit d99ee8f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/cli/src/cmds/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import * as constants from "../constants";
import { strings } from "../resource";
import { toLocaleLowerCase } from "../utils";
import { YargsCommand } from "../yargsCommand";
import {
TelemetryEvent,
TelemetryProperty,
TelemetrySuccess,
} from "../telemetry/cliTelemetryEvents";
import CliTelemetry from "../telemetry/cliTelemetry";

async function outputM365Info(commandType: "login" | "show"): Promise<boolean> {
const appStudioTokenJsonRes = await M365TokenProvider.getJsonObject({ scopes: AppStudioScopes });
Expand Down Expand Up @@ -98,8 +104,10 @@ class AccountShow extends YargsCommand {
}

public async runCommand(_args: { [argName: string]: string }): Promise<Result<null, FxError>> {
CliTelemetry.sendTelemetryEvent(TelemetryEvent.AccountShowStart);
const m365StatusRes = await M365TokenProvider.getStatus({ scopes: AppStudioScopes });
if (m365StatusRes.isErr()) {
CliTelemetry.sendTelemetryErrorEvent(TelemetryEvent.AccountShow, m365StatusRes.error);
return err(m365StatusRes.error);
}
const m365Status = m365StatusRes.value;
Expand All @@ -122,7 +130,9 @@ class AccountShow extends YargsCommand {
"Use `teamsfx account login azure` or `teamsfx account login m365` to log in to Azure or Microsoft 365 account."
);
}

CliTelemetry.sendTelemetryEvent(TelemetryEvent.AccountShow, {
[TelemetryProperty.Success]: TelemetrySuccess.Yes,
});
return ok(null);
}
}
Expand Down Expand Up @@ -157,6 +167,7 @@ export class M365Login extends YargsCommand {
}

public async runCommand(_args: { [argName: string]: string }): Promise<Result<null, FxError>> {
CliTelemetry.sendTelemetryEvent(TelemetryEvent.AccountShowStart);
await M365TokenProvider.signout();
await outputM365Info("login");

Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/telemetry/cliTelemetryEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

export enum TelemetryEvent {
//TODO: define CLI telemetry event

AccountShowStart = "account-show-start",
AccountShow = "account-show",

AccountLoginStart = "login-start",
AccountLogin = "login",

Expand Down
6 changes: 4 additions & 2 deletions packages/cli/tests/unit/cmds/account.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { err, ok } from "@microsoft/teamsfx-api";
import { M365TokenJSONNotFoundError } from "@microsoft/teamsfx-core";
import "mocha";
import { RestoreFn } from "mocked-env";
import sinon from "sinon";
Expand All @@ -10,16 +11,17 @@ import Account, { AzureLogin, M365Login } from "../../../src/cmds/account";
import AzureTokenProvider from "../../../src/commonlib/azureLogin";
import { signedOut } from "../../../src/commonlib/common/constant";
import M365TokenProvider from "../../../src/commonlib/m365Login";
import { expect, mockLogProvider, mockYargs } from "../utils";
import { FileNotFoundError, M365TokenJSONNotFoundError } from "@microsoft/teamsfx-core";
import { expect, mockLogProvider, mockTelemetry, mockYargs } from "../utils";

describe("Account Command Tests", function () {
const sandbox = sinon.createSandbox();
let messages: string[] = [];
const telemetryEvents: string[] = [];
const mockedEnvRestore: RestoreFn = () => {};

beforeEach(() => {
mockYargs(sandbox);
mockTelemetry(sandbox, telemetryEvents);
mockLogProvider(sandbox, messages);
});

Expand Down

0 comments on commit d99ee8f

Please sign in to comment.