Skip to content

Commit

Permalink
check null reference cases for version upgrade notification (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
YanaXu authored Jul 27, 2023
1 parent dae2d46 commit 7984785
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Azure.PowerShell.Common.Share.Survey;
using Microsoft.Azure.PowerShell.Common.Share.UpgradeNotification;
using Microsoft.Azure.PowerShell.Common.UpgradeNotification;
using Microsoft.Azure.ServiceManagement.Common.Models;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
Expand Down
29 changes: 29 additions & 0 deletions src/Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1742,4 +1742,15 @@ Note : Go to {0} for steps to suppress this breaking change warning, and other i
<data name="PreviewCmdletETAMessage" xml:space="preserve">
<value> The estimated generally available date is '{0}'.</value>
</data>
<data name="BreakingChangesMessage" xml:space="preserve">
<value>There will be breaking changes from {0} to {1}. Open {2} and check the details.</value>
</data>
<data name="MigrationGuideLink" xml:space="preserve">
<value>https://go.microsoft.com/fwlink/?linkid=2241373</value>
</data>
<data name="VersionUpgradeMessage" xml:space="preserve">
<value>You're using {0} version {1}. The latest version of {0} is {2}. Upgrade your Az modules using the following commands:
{3} {4} -WhatIf -- Simulate updating your Az modules.
{3} {4} -- Update your Az modules.</value>
</data>
</root>
30 changes: 15 additions & 15 deletions src/Common/UpgradeNotification/UpgradeNotificationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Expand All @@ -8,11 +10,10 @@
using System.Management.Automation;
using System.Threading;

namespace Microsoft.Azure.PowerShell.Common.Share.UpgradeNotification
namespace Microsoft.Azure.PowerShell.Common.UpgradeNotification
{
public class UpgradeNotificationHelper
{
private const string AZPSMigrationGuideLink = "https://go.microsoft.com/fwlink/?linkid=2241373";
private const string FrequencyKeyForUpgradeNotification = "VersionUpgradeNotification";
private static TimeSpan FrequencyTimeSpanForUpgradeNotification = TimeSpan.FromDays(30);

Expand Down Expand Up @@ -55,14 +56,18 @@ public static UpgradeNotificationHelper GetInstance()
return _instance;
}

public void WriteWarningMessageForVersionUpgrade(Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet cmdlet, AzurePSQoSEvent _qosEvent, IConfigManager configManager, IFrequencyService frequencyService) {
_qosEvent.HigherVersionsChecked = false;
_qosEvent.UpgradeNotificationPrompted = false;

public void WriteWarningMessageForVersionUpgrade(AzurePSCmdlet cmdlet, AzurePSQoSEvent _qosEvent, IConfigManager configManager, IFrequencyService frequencyService) {
// skip if it's the exceptional case.
if (cmdlet == null || _qosEvent == null || configManager == null || frequencyService == null) {
return;
}
try
{
_qosEvent.HigherVersionsChecked = false;
_qosEvent.UpgradeNotificationPrompted = false;

//disabled by az config, skip
if (configManager!=null&& configManager.GetConfigValue<bool>(ConfigKeysForCommon.CheckForUpgrade).Equals(false))
if (configManager.GetConfigValue<bool>(ConfigKeysForCommon.CheckForUpgrade).Equals(false))
{
return;
}
Expand All @@ -73,10 +78,6 @@ public void WriteWarningMessageForVersionUpgrade(Microsoft.WindowsAzure.Commands
return;
}

//register verion check and upgrade notification in frequency service
if (frequencyService == null) {
return;
}
frequencyService.Register(FrequencyKeyForUpgradeCheck, FrequencyTimeSpanForUpgradeCheck);
frequencyService.Register(FrequencyKeyForUpgradeNotification, FrequencyTimeSpanForUpgradeNotification);

Expand Down Expand Up @@ -119,12 +120,11 @@ public void WriteWarningMessageForVersionUpgrade(Microsoft.WindowsAzure.Commands

string latestModuleVersion = GetModuleLatestVersion(checkModuleName);
string updateModuleCmdletName = GetCmdletForUpdateModule();
string warningMsg = $"You're using {checkModuleName} version {checkModuleCurrentVersion}. The latest version of {checkModuleName} is {latestModuleVersion}. Upgrade your Az modules using the following commands:{Environment.NewLine}";
warningMsg += $" {updateModuleCmdletName} {upgradeModuleNames} -WhatIf -- Simulate updating your Az modules.{Environment.NewLine}";
warningMsg += $" {updateModuleCmdletName} {upgradeModuleNames} -- Update your Az modules.{Environment.NewLine}";
string warningMsg = string.Format(Resources.VersionUpgradeMessage, checkModuleName, checkModuleCurrentVersion, latestModuleVersion, updateModuleCmdletName, upgradeModuleNames);
if ("Az".Equals(checkModuleName) && GetInstance().HasHigherMajorVersion(checkModuleName, checkModuleCurrentVersion))
{
warningMsg += $"There will be breaking changes from {checkModuleCurrentVersion} to {latestModuleVersion}. Open {AZPSMigrationGuideLink} and check the details.{Environment.NewLine}";
warningMsg += Environment.NewLine;
warningMsg += string.Format(Resources.BreakingChangesMessage, checkModuleCurrentVersion, latestModuleVersion, Resources.MigrationGuideLink);
}
cmdlet.WriteWarning(warningMsg);
});
Expand Down

0 comments on commit 7984785

Please sign in to comment.