Skip to content
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
16 changes: 10 additions & 6 deletions Evergreen/Shared/Resolve-MicrosoftFwLink.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ function Resolve-MicrosoftFwLink {
$ResolvedUrl = Resolve-SystemNetWebRequest @params

# Find a version number in the final URL path, excluding query parameters.
$FileName = [System.IO.Path]::GetFileName($ResolvedUrl.ResponseUri.AbsolutePath)
$VersionMatch = [RegEx]::Match($FileName, "(?<![\d.])(\d+(?:\.\d+){1,4})(?!\.\d)")
if ($VersionMatch.Success) {
$Version = $VersionMatch.Groups[1].Value
# Check the file name first, then each parent path segment from the closest outwards.
$Version = "Unknown"
$Segments = @($ResolvedUrl.ResponseUri.AbsolutePath -split "/" | Where-Object { $_ })
for ($i = $Segments.Count - 1; $i -ge 0; $i--) {
$VersionMatch = [RegEx]::Match($Segments[$i], "(?<![\d.])(\d+(?:\.\d+){1,4})(?!\.\d)")
if ($VersionMatch.Success) {
$Version = $VersionMatch.Groups[1].Value
break
}
}
else {
$Version = "Unknown"
if ($Version -eq "Unknown") {
Write-Warning -Message "$($MyInvocation.MyCommand): Failed to match version number from: $($ResolvedUrl.ResponseUri.AbsoluteUri)."
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Shared/Resolve-MicrosoftFwLink.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ InModuleScope -ModuleName "Evergreen" {
$Result.URI | Should -Be "https://download.microsoft.com/files/Product-1.2.3.4-x64.msi?version=9.8.7"
}

It "Extracts the version from a parent path segment when the file name has none" {
$script:TestMicrosoftFwLinkResponse.ResponseUri = [System.Uri]"https://res.cdn.office.net/nativehost/5mttl/installer/v2/1.2026.1026.100/Microsoft.OutlookForWindows_x64.msix?version=9.8.7"

$Warnings = @()
$Result = Resolve-MicrosoftFwLink -Uri "https://go.microsoft.com/fwlink/?linkid=2195278" -WarningVariable Warnings

$Result.Version | Should -Be "1.2026.1026.100"
$Warnings.Count | Should -Be 0
}

It "Prefers the version in the file name over a parent path segment" {
$script:TestMicrosoftFwLinkResponse.ResponseUri = [System.Uri]"https://download.microsoft.com/files/9.9.9/Product-1.2.3.4-x64.msi"

$Result = Resolve-MicrosoftFwLink -Uri "https://go.microsoft.com/fwlink/?linkid=2248728"

$Result.Version | Should -Be "1.2.3.4"
}

It "Returns Unknown and warns when the final path has no version" {
$script:TestMicrosoftFwLinkResponse.ResponseUri = [System.Uri]"https://download.microsoft.com/files/Product-x64.msi?version=9.8.7"

Expand Down
Loading