Skip to content

Commit

Permalink
Support date- and semver-based api-versions (Azure#30891)
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths authored Oct 8, 2024
1 parent 4af4e91 commit cf170ef
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
46 changes: 29 additions & 17 deletions eng/scripts/Copy-ApiVersion-Functions.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function Get-NewTagSection($apiVersion, $resourceProvider, $apiVersionStatus, $specFiles) {

$tagVersion = $apiVersion -match '(?<date>\d{4}-\d{2})-\d{2}'
$tagVersion = $Matches['date']
$tagVersion = $apiVersion -match '(?<version>(\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
Expand All @@ -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-)(.*)(?<version>\d{4}-\d{2})(.*)'
$currentTag = $Matches['version']
$latestVersionDate = [datetime]($currentTag -replace '-preview', '')
$currentTag = $readmeContent -match '(?m)^(tag:\s*)(package-)(.*)(?<apiVersion>(?<date>\d{4}-\d{2}-\d{2})|(?<version>\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 '(?<apiVersion>(?<date>\d{4}-\d{2}-\d{2})|(?<version>\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 {
Expand Down
39 changes: 27 additions & 12 deletions eng/scripts/Tests/Copy-ApiVersion/Copy-ApiVersion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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" {
Expand All @@ -16,43 +18,56 @@ 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)

$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$"
}
}
}

0 comments on commit cf170ef

Please sign in to comment.