Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 10 second timeout on checking for an updates #2550

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
116 changes: 68 additions & 48 deletions src/Commands/Utilities/VersionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,95 @@

namespace PnP.PowerShell.Commands.Utilities
{
/// <summary>
/// Functionality to take care of checking if a newer version of PnP PowerShell is available
/// </summary>
public static class VersionChecker
{

/// <summary>
/// URL to the PnP PowerShell release notes for the nightly release
/// </summary>
private static readonly Uri NightlyVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/dev/version.txt");

/// <summary>
/// URL to the PnP PowerShell release notes for the stable release
/// </summary>
private static readonly Uri ReleaseVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/master/version.txt");

/// <summary>
/// Boolean to indicate if the version check has already been performed
/// </summary>
private static bool VersionChecked;

/// <summary>
/// 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.
/// </summary>
public static short VersionCheckTimeOut = 10;

/// <summary>
/// Performs the check for a newer PnP PowerShell version
/// </summary>
/// <param name="cmdlet">Cmdlet instance from which this check is done</param>
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)
{ }
}

/// <summary>
/// Checks if the provided version is newer than the current version
/// </summary>
/// <param name="availableVersionString">The version to check the current version against</param>
/// <returns>True if the provided version is newer than the current version, false if it is not</returns>
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)
Expand All @@ -81,44 +121,18 @@ public static bool IsNewer(string availableVersionString)
}
return false;
}
// #if !NETFRAMEWORK
// var currentVersion = new SemanticVersion(assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().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;
// }

/// <summary>
/// Retrieves the latest available version of PnP PowerShell. Based on the provided isNightly flag, it will check for the latest nightly or stable release.
/// </summary>
/// <returns>The latest available version</returns>
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)
{
Expand All @@ -128,12 +142,18 @@ internal static string GetAvailableVersion(bool isNightly)
}
return null;
}

/// <summary>
/// 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.
/// </summary>
/// <returns>The latest available version</returns>
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);
}
}
Expand Down