diff --git a/eng/scripts/Copy-ApiVersion-Functions.ps1 b/eng/scripts/Copy-ApiVersion-Functions.ps1 index 53a5aebccc3c..a26d0c0f87f9 100644 --- a/eng/scripts/Copy-ApiVersion-Functions.ps1 +++ b/eng/scripts/Copy-ApiVersion-Functions.ps1 @@ -1,12 +1,12 @@ function Get-NewTagSection($apiVersion, $resourceProvider, $apiVersionStatus, $specFiles) { - $tagVersion = $apiVersion -match '(?\d{4}-\d{2})-\d{2}' - $tagVersion = $Matches['date'] + $tagVersion = $apiVersion -match '(?(\d{4}-\d{2}-\d{2}|\d+\.\d+)(-preview(\.\d+)?)?)' + $tagVersion = $Matches['version'] $baseDir = "$resourceProvider/$apiVersionStatus/$apiVersion" - - if ($apiVersionStatus -eq "preview") { - $tagVersion = "preview-" + $tagVersion - } + + if ($apiVersionStatus -eq "preview") { + $tagVersion = "preview-" + $tagVersion + } $content = @" ### Tag: package-$tagVersion @@ -31,23 +31,35 @@ function Get-ReadmeWithNewTag($readmeContent, $tagContent) { function Get-ReadmeWithLatestTag($readmeContent, $newApiVersion, $newApiVersionStatus ) { # Get the current tag date - $currentTag = $readmeContent -match '(?m)^(tag:\s*)(package-)(.*)(?\d{4}-\d{2})(.*)' - $currentTag = $Matches['version'] - $latestVersionDate = [datetime]($currentTag -replace '-preview', '') + $currentTag = $readmeContent -match '(?m)^(tag:\s*)(package-)(.*)(?(?\d{4}-\d{2}-\d{2})|(?\d+\.\d+))' + if ($currentTag = $Matches['date']) { + $latestVersionDate = [datetime]($currentTag -replace '-preview', '') - # Convert the new OpenAPI version to a date - $newVersionDate = [datetime]($newApiVersion -replace '-preview', '') + # Convert the new OpenAPI version to a date + $newVersionDate = [datetime]($newApiVersion -replace '-preview', '') - # Compare two dates - if ($latestVersionDate -gt $newVersionDate) { - Write-Warning "The new version is not newer than the current default version in the readme file." + # Compare two dates + if ($latestVersionDate -gt $newVersionDate) { + Write-Warning "The new version is not newer than the current default version in the readme file." + } + } elseif ($currentTag = $Matches['version']) { + $latestVersion = [version]($currentTag -replace '-preview', '') + + # Convert the new OpenAPI version to a date + $newVersion = [Version]($newApiVersion -replace '-preview', '') + + # Compare two semvers + if ($latestVersion -gt $newVersion) { + Write-Warning "The new version is not newer than the current default version in the readme file." + } } - $tagVersion = $newApiVersion -match '\d{4}-\d{2}' - $tagVersion = $Matches[0] + + $tagVersion = $newApiVersion -match '(?(?\d{4}-\d{2}-\d{2})|(?\d+\.\d+)(-preview(\.\d+)?)?)' + $tagVersion = $Matches['apiVersion'] if ($newApiVersionStatus -eq "preview") { $tagVersion = "preview-" + $tagVersion } - return $readmeContent -replace '(?m)^(tag:\s*)(package-.*)', "tag: package-$tagVersion" + return $readmeContent -replace '(?m)^(tag:\s*)(package-.*)', "tag: package-$tagVersion" } function New-GitAddAndCommit { diff --git a/eng/scripts/Tests/Copy-ApiVersion/Copy-ApiVersion.Tests.ps1 b/eng/scripts/Tests/Copy-ApiVersion/Copy-ApiVersion.Tests.ps1 index ff4fe41e479a..9bbaa3b230b8 100644 --- a/eng/scripts/Tests/Copy-ApiVersion/Copy-ApiVersion.Tests.ps1 +++ b/eng/scripts/Tests/Copy-ApiVersion/Copy-ApiVersion.Tests.ps1 @@ -1,6 +1,8 @@ Import-Module Pester -. "$PSScriptRoot\..\..\Copy-ApiVersion-Functions.ps1" +BeforeAll { + . "$PSScriptRoot\..\..\Copy-ApiVersion-Functions.ps1" +} Describe "Copy-ApiVersion regex tests" { Context "Generate new version tag section" { @@ -16,6 +18,18 @@ Describe "Copy-ApiVersion regex tests" { provider = "Microsoft.Compute\ComputeRP" versionStatus = "stable" specsDir = '..\..\..\..\..\specification\compute\resource-manager\Microsoft.Compute\ComputeRP\stable\2023-09-01' + }, + @{ + version = "7.6-preview.1" + provider = "Microsoft.KeyVault" + versionStatus = "preview" + specsDir = '..\..\..\..\..\specification\keyvault\data-plane\Microsoft.KeyVault\preview\7.6-preview.1' + }, + @{ + version = "7.5" + provider = "Microsoft.Compute\ComputeRP" + versionStatus = "stable" + specsDir = '..\..\..\..\..\specification\keyvault\data-plane\Microsoft.KeyVault\stable\7.5' } ) { param($version, $provider, $versionStatus, $specsDir) @@ -23,36 +37,37 @@ Describe "Copy-ApiVersion regex tests" { $jsonFiles = Get-ChildItem $PSScriptRoot+$specsDir -Filter '*.json' | Select-Object -ExpandProperty Name $newTagSection = Get-NewTagSection $version $provider $versionStatus $jsonFiles - $version -match '\d{4}-\d{2}' - $tag = $Matches[0] + $tag = $version if ($versionStatus -eq "preview") { $tag = "preview-" + $tag } - $newTagSection | Should match "package-$tag" + $newTagSection | Should -Match "(?m)^### Tag: package-$tag$" } + # TODO: This is fragile. The tests stop working when a service team updates their readme.md. We should instead take fixed copies or something. It "Default version gets updated" -TestCases @( - @{ - inputReadme = '..\..\..\..\..\specification\agrifood\resource-manager\readme.md' - apiVersion = "2024-01-01-preview" - versionStatus = "preview" - }, @{ inputReadme = '..\..\..\..\..\specification\compute\resource-manager\readme.md' apiVersion = "2024-01-01" versionStatus = "stable" + }, + @{ + inputReadme = '..\..\..\..\..\specification\keyvault\data-plane\readme.md' + apiVersion = "7.6-preview.1" + versionStatus = "preview" } ) { param($inputReadme, $apiVersion, $versionStatus) $contents = Get-Content $PSScriptRoot+$inputReadme -Raw $output = Get-ReadmeWithLatestTag $contents $apiVersion $versionStatus - $tag = $apiVersion -match '\d{4}-\d{2}' - $tag = $Matches[0] + + $tag = $apiVersion if ($versionStatus -eq "preview") { $tag = "preview-" + $tag } - $output | Should match "(?m)^tag:\s*package-$tag" + + $output | Should -Match "(?m)^tag:\s*package-$tag$" } } } \ No newline at end of file