From b41de43c6e4ec98efca604625c6c4475e49877ec Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Sun, 21 Jul 2024 21:42:42 -0700 Subject: [PATCH 1/4] feat: add rush alert cli draft --- .../rush-lib/src/cli/RushCommandLineParser.ts | 2 ++ .../rush-lib/src/cli/actions/AlertAction.ts | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 libraries/rush-lib/src/cli/actions/AlertAction.ts diff --git a/libraries/rush-lib/src/cli/RushCommandLineParser.ts b/libraries/rush-lib/src/cli/RushCommandLineParser.ts index 80009e6d883..220cd42a304 100644 --- a/libraries/rush-lib/src/cli/RushCommandLineParser.ts +++ b/libraries/rush-lib/src/cli/RushCommandLineParser.ts @@ -60,6 +60,7 @@ import { PhasedScriptAction } from './scriptActions/PhasedScriptAction'; import type { IBuiltInPluginConfiguration } from '../pluginFramework/PluginLoader/BuiltInPluginLoader'; import { InitSubspaceAction } from './actions/InitSubspaceAction'; import { RushAlerts } from '../utilities/RushAlerts'; +import { AlertAction } from './actions/AlertAction'; /** * Options for `RushCommandLineParser`. @@ -289,6 +290,7 @@ export class RushCommandLineParser extends CommandLineParser { try { // Alphabetical order this.addAction(new AddAction(this)); + this.addAction(new AlertAction(this)); this.addAction(new ChangeAction(this)); this.addAction(new CheckAction(this)); this.addAction(new DeployAction(this)); diff --git a/libraries/rush-lib/src/cli/actions/AlertAction.ts b/libraries/rush-lib/src/cli/actions/AlertAction.ts new file mode 100644 index 00000000000..4c8e67cf178 --- /dev/null +++ b/libraries/rush-lib/src/cli/actions/AlertAction.ts @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. +// See LICENSE in the project root for license information. + +import type { CommandLineFlagParameter } from '@rushstack/ts-command-line'; + +import { BaseRushAction } from './BaseRushAction'; +import type { RushCommandLineParser } from '../RushCommandLineParser'; +export class AlertAction extends BaseRushAction { + private readonly _snoozeParameter: CommandLineFlagParameter; + + public constructor(parser: RushCommandLineParser) { + super({ + actionName: 'alert', + summary: 'Use this command to interact with Rush alert feature', + documentation: + 'The "rush alert" command is used to interact with Rush alert feature. For example,' + + ' you can mute the alerts if you think they are annoying', + parser + }); + + this._snoozeParameter = this.defineFlagParameter({ + parameterLongName: '--snooze', + description: 'Snooze the alerts for today.' + }); + } + + protected async runAsync(): Promise { + if (this._snoozeParameter.value!) { + console.log('Snoozing'); + } + } +} From bd6f52d010bf92fc05af6f31e7bfff03bf12cb8c Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:27:04 -0700 Subject: [PATCH 2/4] feat: rush alerts --snooze --- .../rush-lib/src/cli/actions/AlertAction.ts | 15 +++++++++-- .../rush-lib/src/utilities/RushAlerts.ts | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/libraries/rush-lib/src/cli/actions/AlertAction.ts b/libraries/rush-lib/src/cli/actions/AlertAction.ts index 4c8e67cf178..35899eee2a7 100644 --- a/libraries/rush-lib/src/cli/actions/AlertAction.ts +++ b/libraries/rush-lib/src/cli/actions/AlertAction.ts @@ -5,8 +5,11 @@ import type { CommandLineFlagParameter } from '@rushstack/ts-command-line'; import { BaseRushAction } from './BaseRushAction'; import type { RushCommandLineParser } from '../RushCommandLineParser'; +import { ConsoleTerminalProvider, Terminal } from '@rushstack/terminal'; +import { RushAlerts } from '../../utilities/RushAlerts'; export class AlertAction extends BaseRushAction { private readonly _snoozeParameter: CommandLineFlagParameter; + private readonly _rushAlerts: RushAlerts; public constructor(parser: RushCommandLineParser) { super({ @@ -20,13 +23,21 @@ export class AlertAction extends BaseRushAction { this._snoozeParameter = this.defineFlagParameter({ parameterLongName: '--snooze', - description: 'Snooze the alerts for today.' + description: + 'Snooze the alerts for today.' + + ' Please note, if you delete the `common/temp` folder, the snooze state will be reset. ' + }); + + const terminal: Terminal = new Terminal(new ConsoleTerminalProvider()); + this._rushAlerts = new RushAlerts({ + rushConfiguration: this.rushConfiguration, + terminal }); } protected async runAsync(): Promise { if (this._snoozeParameter.value!) { - console.log('Snoozing'); + await this._rushAlerts.snoozeAlertsAsync(); } } } diff --git a/libraries/rush-lib/src/utilities/RushAlerts.ts b/libraries/rush-lib/src/utilities/RushAlerts.ts index ac47145f59f..8623af77226 100644 --- a/libraries/rush-lib/src/utilities/RushAlerts.ts +++ b/libraries/rush-lib/src/utilities/RushAlerts.ts @@ -25,6 +25,7 @@ interface IRushAlertsConfigEntry { } interface IRushAlertsState { lastUpdateTime: string; + snooze: boolean; alerts: Array; } interface IRushAlertStateEntry { @@ -73,6 +74,11 @@ export class RushAlerts { return false; } + // if snooze is enabled, also return false + if (rushAlertsState.snooze) { + return false; + } + return true; } @@ -262,6 +268,7 @@ export class RushAlerts { if (validAlerts.length > 0) { const rushAlertsState: IRushAlertsState = { lastUpdateTime: new Date().toISOString(), + snooze: false, alerts: validAlerts }; @@ -276,4 +283,22 @@ export class RushAlerts { await FileSystem.deleteFileAsync(this.rushAlertsStateFilePath); } } + + public async snoozeAlertsAsync(): Promise { + const rushAlertsState: IRushAlertsState | undefined = await this._loadRushAlertsStateAsync(); + + if (!rushAlertsState) { + return; + } + + rushAlertsState.snooze = true; + + await JsonFile.saveAsync(rushAlertsState, this.rushAlertsStateFilePath, { + ignoreUndefinedValues: true, + headerComment: '// THIS FILE IS MACHINE-GENERATED -- DO NOT MODIFY', + jsonSyntax: JsonSyntax.JsonWithComments + }); + + this._terminal.writeLine('Snoozing the rush alerts for today!'); + } } From 5227d53a79cddb283eaf3e0d82a5d434a681438a Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:11:22 -0700 Subject: [PATCH 3/4] feat: add rushVersion to alerts state --- .../rush-lib/src/cli/RushCommandLineParser.ts | 4 ++- .../rush-lib/src/utilities/RushAlerts.ts | 28 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libraries/rush-lib/src/cli/RushCommandLineParser.ts b/libraries/rush-lib/src/cli/RushCommandLineParser.ts index 220cd42a304..3fa3792c4b0 100644 --- a/libraries/rush-lib/src/cli/RushCommandLineParser.ts +++ b/libraries/rush-lib/src/cli/RushCommandLineParser.ts @@ -237,7 +237,9 @@ export class RushCommandLineParser extends CommandLineParser { terminal: this._terminal }); if (await rushAlerts.isAlertsStateUpToDateAsync()) { - await rushAlerts.printAlertsAsync(); + if (!(await rushAlerts.isSnoozeAsync())) { + await rushAlerts.printAlertsAsync(); + } } else { await rushAlerts.retrieveAlertsAsync(); } diff --git a/libraries/rush-lib/src/utilities/RushAlerts.ts b/libraries/rush-lib/src/utilities/RushAlerts.ts index 8623af77226..30f1e0344ad 100644 --- a/libraries/rush-lib/src/utilities/RushAlerts.ts +++ b/libraries/rush-lib/src/utilities/RushAlerts.ts @@ -26,6 +26,7 @@ interface IRushAlertsConfigEntry { interface IRushAlertsState { lastUpdateTime: string; snooze: boolean; + rushVersion: string; alerts: Array; } interface IRushAlertStateEntry { @@ -61,7 +62,16 @@ export class RushAlerts { public async isAlertsStateUpToDateAsync(): Promise { const rushAlertsState: IRushAlertsState | undefined = await this._loadRushAlertsStateAsync(); - if (rushAlertsState === undefined || !rushAlertsState.lastUpdateTime) { + if ( + rushAlertsState === undefined || + !rushAlertsState.lastUpdateTime || + rushAlertsState.rushVersion === undefined + ) { + return false; + } + + // if the state file is generated by another Rush version, also return false + if (rushAlertsState.rushVersion !== this._rushConfiguration.rushConfigurationJson.rushVersion) { return false; } @@ -74,11 +84,6 @@ export class RushAlerts { return false; } - // if snooze is enabled, also return false - if (rushAlertsState.snooze) { - return false; - } - return true; } @@ -269,6 +274,7 @@ export class RushAlerts { const rushAlertsState: IRushAlertsState = { lastUpdateTime: new Date().toISOString(), snooze: false, + rushVersion: this._rushConfiguration.rushConfigurationJson.rushVersion, alerts: validAlerts }; @@ -301,4 +307,14 @@ export class RushAlerts { this._terminal.writeLine('Snoozing the rush alerts for today!'); } + + public async isSnoozeAsync(): Promise { + const rushAlertsState: IRushAlertsState | undefined = await this._loadRushAlertsStateAsync(); + + if (rushAlertsState === undefined || !rushAlertsState.snooze) { + return false; + } + + return true; + } } From b82875f45a3766208621f081c284e728cc9eafb9 Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:34:37 -0700 Subject: [PATCH 4/4] feat: rush change --- .../rush/chao-rush-alert-cmd_2024-07-22-19-34.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@microsoft/rush/chao-rush-alert-cmd_2024-07-22-19-34.json diff --git a/common/changes/@microsoft/rush/chao-rush-alert-cmd_2024-07-22-19-34.json b/common/changes/@microsoft/rush/chao-rush-alert-cmd_2024-07-22-19-34.json new file mode 100644 index 00000000000..d39389ccfe0 --- /dev/null +++ b/common/changes/@microsoft/rush/chao-rush-alert-cmd_2024-07-22-19-34.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Add rush alert CLI", + "type": "none" + } + ], + "packageName": "@microsoft/rush" +} \ No newline at end of file