From a556ff58857418ccb6d9ffb02c87fd4c897c1445 Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Fri, 11 Nov 2022 14:48:22 +0100 Subject: [PATCH 1/2] Cleaned up code, added timeout on checking for an update --- src/Commands/Utilities/VersionChecker.cs | 116 +++++++++++++---------- 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/src/Commands/Utilities/VersionChecker.cs b/src/Commands/Utilities/VersionChecker.cs index adb876a97..ef9dd94e3 100644 --- a/src/Commands/Utilities/VersionChecker.cs +++ b/src/Commands/Utilities/VersionChecker.cs @@ -6,55 +6,95 @@ namespace PnP.PowerShell.Commands.Utilities { + /// + /// Functionality to take care of checking if a newer version of PnP PowerShell is available + /// public static class VersionChecker { - + /// + /// URL to the PnP PowerShell release notes for the nightly release + /// private static readonly Uri NightlyVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/dev/version.txt"); + + /// + /// URL to the PnP PowerShell release notes for the stable release + /// private static readonly Uri ReleaseVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/master/version.txt"); + + /// + /// Boolean to indicate if the version check has already been performed + /// private static bool VersionChecked; + /// + /// Timeout in seconds to allow for the version check to be performed at most. If it exceeds this time, the check will silently be skipped. Verbose output will show when this happens. + /// + public static short VersionCheckTimeOut = 10; + + /// + /// Performs the check for a newer PnP PowerShell version + /// + /// Cmdlet instance from which this check is done public static void CheckVersion(PSCmdlet cmdlet) { - // do we need to check versions. Is the environment variable set? + // Do we need to check versions: is the environment variable set? var pnppowershellUpdatecheck = Environment.GetEnvironmentVariable("PNPPOWERSHELL_UPDATECHECK"); if (!string.IsNullOrEmpty(pnppowershellUpdatecheck)) { + // If the environment variable is set to false or off, we don't need to check if (pnppowershellUpdatecheck.ToLower() == "off" || pnppowershellUpdatecheck.ToLower() == "false") { VersionChecked = true; } } + // If the version has already been checked, no need to do so again + if (VersionChecked) return; + try { - if (!VersionChecked) - { - var assembly = Assembly.GetExecutingAssembly(); - var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); - var productVersion = versionInfo.ProductVersion; - var isNightly = productVersion.Contains("-"); + // Get the current version of PnP PowerShell being used + var assembly = Assembly.GetExecutingAssembly(); + var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); + var productVersion = versionInfo.ProductVersion; + var isNightly = productVersion.Contains("-"); + + cmdlet?.WriteVerbose($"Checking for updates, current version is {productVersion}. See https://pnp.github.io/powershell/articles/configuration.html#disable-or-enable-version-checks for more information."); - var onlineVersion = GetAvailableVersion(isNightly); + // Check for the latest available version + var onlineVersion = GetAvailableVersion(isNightly); - if (IsNewer(onlineVersion) && cmdlet != null) + if (IsNewer(onlineVersion)) + { + if (cmdlet != null) { var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell{(isNightly ? " -AllowPrerelease" : "")}' to update.\nUse 'Get-PnPChangeLog {(!isNightly ? $"-Release {onlineVersion}" : "-Nightly")}' to list changes.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n"; CmdletMessageWriter.WriteFormattedWarning(cmdlet, updateMessage); } - VersionChecked = true; } + else + { + cmdlet?.WriteVerbose("No newer version of PnP PowerShell is available"); + } + VersionChecked = true; + } + catch (Exception e) + { + cmdlet?.WriteVerbose($"Error checking for updates: {e.Message}"); } - catch (Exception) - { } } + /// + /// Checks if the provided version is newer than the current version + /// + /// The version to check the current version against + /// True if the provided version is newer than the current version, false if it is not public static bool IsNewer(string availableVersionString) { var assembly = Assembly.GetExecutingAssembly(); var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); var productVersion = versionInfo.ProductVersion; - if (Version.TryParse(availableVersionString, out Version availableVersion)) { if (availableVersion.Major > versionInfo.ProductMajorPart) @@ -81,44 +121,18 @@ public static bool IsNewer(string availableVersionString) } return false; } - // #if !NETFRAMEWORK - // var currentVersion = new SemanticVersion(assembly.GetCustomAttribute().InformationalVersion); - // if (SemanticVersion.TryParse(availableVersionString, out SemanticVersion availableVersion)) - // #else - // var currentVersion = new Version(((AssemblyFileVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version); - // if (Version.TryParse(availableVersionString, out Version availableVersion)) - // #endif - // { - // if (availableVersion.Major > currentVersion.Major) - // { - // return true; - // } - // else - // { - // if (availableVersion.Major == currentVersion.Major && availableVersion.Minor > currentVersion.Minor) - // { - // return true; - // } - // #if !NETFRAMEWORK - // else - // { - // if (!string.IsNullOrEmpty(currentVersion.PreReleaseLabel)) - // { - // if (availableVersion.Major == currentVersion.Major && availableVersion.Minor == currentVersion.Minor && availableVersion.Patch > currentVersion.Patch) - // { - // return true; - // } - // } - // } - // #endif - // } - // } - // return false; - // } + /// + /// Retrieves the latest available version of PnP PowerShell. Based on the provided isNightly flag, it will check for the latest nightly or stable release. + /// + /// The latest available version internal static string GetAvailableVersion(bool isNightly) { var httpClient = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient(); + + // Deliberately lowering timeout as the version check is not critical so in case of a slower or blocked internet connection, this should not block the cmdlet for too long + httpClient.Timeout = TimeSpan.FromSeconds(VersionCheckTimeOut); + var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, isNightly ? NightlyVersionCheckUrl : ReleaseVersionCheckUrl)).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { @@ -128,12 +142,18 @@ internal static string GetAvailableVersion(bool isNightly) } return null; } + + /// + /// Retrieves the latest available version of PnP PowerShell. If the current version is a nightly build, it will check for the latest nightly build as well. If the current version is a stable build, it will only check for the latest stable build. + /// + /// The latest available version public static string GetAvailableVersion() { var assembly = Assembly.GetExecutingAssembly(); var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); var productVersion = versionInfo.ProductVersion; var isNightly = productVersion.Contains("-"); + return GetAvailableVersion(isNightly); } } From 9ad0cb66d65fa88cb6d7e1cc0417b79769ff9029 Mon Sep 17 00:00:00 2001 From: Koen Zomers Date: Fri, 11 Nov 2022 14:52:38 +0100 Subject: [PATCH 2/2] Added changelog entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69cd1ebe6..09619aa4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Changed +- Added a 10 second timeout on the new version check on `Connect-PnPOnline` to prevent the cmdlet from hanging when the connection is slow, GitHub being blocked by a firewall or GitHub being unavailable [#2550](https://github.com/pnp/powershell/pull/2550) + ### Removed ### Fixed +### Contributors + +- Koen Zomers [koenzomers] + ## [1.12.0] ### Added