From c50ce6069a983cb13706656ae8e9adc66fbf4664 Mon Sep 17 00:00:00 2001 From: Daniel Jurek Date: Wed, 23 Aug 2023 14:58:10 -0700 Subject: [PATCH] Docs onboarding v2 (#37997) * Some progress * Add logic for docs onboarding * Output stacktrace * Formatting, tiny refactor * Add "DocsCiConfigConfigProperties" to metadata JSON * More up to date eng/common * Update eng/common * Revert Update-DocsMsMetadata.ps1 * Add tfm=netstandard2.0 handler to a different location * docindex for testing * Keys instead of ContainsKey * -notin * Branch naming * Revert eng/common demonstration * Use new script in docindex.yml * Remove TODO, tests pass on Windows * Use Update-DocsMsPackages.ps1 --- eng/scripts/docs/Docs-Onboarding.ps1 | 110 ++++++++++++++++++ .../docs/tests/Docs-Onboarding.tests.ps1 | 79 +++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 eng/scripts/docs/Docs-Onboarding.ps1 create mode 100644 eng/scripts/docs/tests/Docs-Onboarding.tests.ps1 diff --git a/eng/scripts/docs/Docs-Onboarding.ps1 b/eng/scripts/docs/Docs-Onboarding.ps1 new file mode 100644 index 0000000000000..9e760e4581856 --- /dev/null +++ b/eng/scripts/docs/Docs-Onboarding.ps1 @@ -0,0 +1,110 @@ +. "$PSScriptRoot/../../common/scripts/common.ps1" + +Set-StrictMode -Version 3 + +function GetPropertyString($docsCiConfigProperties) { + $propertyArray = @() + foreach ($key in $docsCiConfigProperties.Keys) { + $propertyArray += "$key=$($docsCiConfigProperties[$key])" + } + return $propertyArray -join ';' +} + +function Get-DocsCiLine2($item) { + $line = '' + if ($item.ContainsKey('DocsCiConfigProperties') -and $item['DocsCiConfigProperties'].Count -gt 0) { + $line = "$($item['Id']),[$(GetPropertyString $item['DocsCiConfigProperties'])]$($item['Name'])" + } else { + $line = "$($item['Id']),$($item['Name'])" + } + + if ($item.ContainsKey('Version') -and $item['Version']) { + $line += ",$($item['Version'])" + } + + return $line +} + +function SetDocsCiConfigProperties($item, $moniker, $packageSourceOverride) { + # Order properties so that output is deterministic (more simple diffs) + $properties = [ordered]@{} + if ($item.ContainsKey('DocsCiConfigProperties')) { + $properties = $item['DocsCiConfigProperties'] + } + + # Set tfm to netstandard2.0 if not already set + if ('tfm' -notin $properties.Keys) { + $properties['tfm'] = 'netstandard2.0' + } + + # When in the preview moniker, always set isPrerelease to true + if ($moniker -eq 'preview') { + $properties['isPrerelease'] = 'true' + } + + # Handle dev version overrides for daily docs + if ($item['DevVersion'] -and $packageSourceOverride) { + $properties['isPrerelease'] = 'true' + $properties['customSource'] = $packageSourceOverride + } + + $item['DocsCiConfigProperties'] = $properties + + return $item +} + +function GetCiConfigPath($docRepoLocation, $moniker) { + $csvPath = Join-Path $docRepoLocation "bundlepackages/azure-dotnet.csv" + if ($moniker -eq 'preview') { + $csvPath = Join-Path $docRepoLocation "bundlepackages/azure-dotnet-preview.csv" + } elseif ($moniker -eq 'legacy') { + $csvPath = Join-Path $docRepoLocation "bundlepackages/azure-dotnet-legacy.csv" + } + return $csvPath +} + +function GetPackageId($packageName) { + return $packageName.Replace('.', '').ToLower() +} + +# $SetDocsPackageOnboarding = "Set-${Language}-DocsPackageOnboarding" +function Set-dotnet-DocsPackageOnboarding($moniker, $metadata, $docRepoLocation, $packageSourceOverride) { + $lines = @() + foreach ($package in $metadata) { + $package.Id = GetPackageId $package.Name + $package = SetDocsCiConfigProperties $package $moniker $packageSourceOverride + + $line = Get-DocsCiLine2 $package + $lines += $line + } + + Set-Content -Path (GetCiConfigPath $docRepoLocation $moniker) -Value $lines +} + +# $GetDocsPackagesAlreadyOnboarded = "Get-${Language}-DocsPackagesAlreadyOnboarded" +function Get-dotnet-DocsPackagesAlreadyOnboarded($docRepoLocation, $moniker) { + $onboardedPackages = Get-DocsCiConfig (GetCiConfigPath $docRepoLocation $moniker) + + $onboardedPackageHash = @{} + foreach ($package in $onboardedPackages) { + $packageProperties = @{ + Name = $package.Name + } + + if ($package.Versions -is [array]) { + if ($package.Versions.Count -gt 1) { + throw "Too many versions supplied for package: $(package.Name) in moniker: $moniker" + } + + if ($package.Versions.Count -eq 1) { + $packageProperties['Version'] = $package.Versions[0] + } else { + # Versions is an empty array, set to an empty string + $packageProperties['Version'] = '' + } + } + + $onboardedPackageHash[$package.Name] = $packageProperties + } + return $onboardedPackageHash +} diff --git a/eng/scripts/docs/tests/Docs-Onboarding.tests.ps1 b/eng/scripts/docs/tests/Docs-Onboarding.tests.ps1 new file mode 100644 index 0000000000000..a1c91c083d764 --- /dev/null +++ b/eng/scripts/docs/tests/Docs-Onboarding.tests.ps1 @@ -0,0 +1,79 @@ +Import-Module Pester + +BeforeAll { + . $PSScriptRoot/../Docs-Onboarding.ps1 +} + +Describe 'GetPropertyString' { + It 'returns when given ()' -ForEach @( + @{ inputValue = @{}; expected = '' }, + @{ inputValue = @{ test = 'value' }; expected = 'test=value' }, + @{ inputValue = [ordered]@{ test = 'value'; test2 = 'value2' }; expected = 'test=value;test2=value2' } + ) { + $result = GetPropertyString $inputValue + $result | Should -Be $expected + } +} + +Describe 'Get-DocsCiLine2' { + It 'returns when given ' -ForEach @( + @{ inputValue = @{ Id = 'id'; Name = 'name' }; expected = 'id,name' }, + @{ inputValue = @{ Id = 'id'; Name = 'name'; Version = 'version' }; expected = 'id,name,version' }, + @{ inputValue = @{ Id = 'id'; Name = 'name'; DocsCiConfigProperties = @{ test = 'value' } }; expected = 'id,[test=value]name' }, + @{ inputValue = @{ Id = 'id'; Name = 'name'; DocsCiConfigProperties = @{ test = 'value' }; Version = 'version' }; expected = 'id,[test=value]name,version' } + ) { + $result = Get-DocsCiLine2 $inputValue + $result | Should -Be $expected + } +} + +Describe 'SetDocsCiConfigProperties' { + It 'sets isPrerelease=true when moniker is preview' { + $package = @{ Id = 'id'; Name = 'name'; Version = 'version' } + $result = SetDocsCiConfigProperties $package 'preview' + + $result.DocsCiConfigProperties['isPrerelease'] | Should -Be 'true' + } + + It 'sets custom source when there is DevVersion and packageSourceOverride' { + $package = @{ Id = 'id'; Name = 'name'; Version = 'version'; DevVersion = 'DevVersionIsSet' } + $result = SetDocsCiConfigProperties $package 'preview' 'https://packageSourceOverride/' + + $result.DocsCiConfigProperties['isPrerelease'] | Should -Be 'true' + $result.DocsCiConfigProperties['customSource'] | Should -Be 'https://packageSourceOverride/' + } + + It 'combines existing properties with with isPrerelease when moniker is preview' { + $package = @{ Id = 'id'; Name = 'name'; Version = 'version'; DocsCiConfigProperties = @{ test = 'value' } } + $result = SetDocsCiConfigProperties $package 'preview' + + $result.DocsCiConfigProperties['isPrerelease'] | Should -Be 'true' + $result.DocsCiConfigProperties['test'] | Should -Be 'value' + } +} + +Describe 'GetCiConfigPath' { + It 'returns when given ' -ForEach @( + @{ inputValue = 'preview'; expected = './bundlepackages/azure-dotnet-preview.csv' }, + @{ inputValue = 'legacy'; expected = './bundlepackages/azure-dotnet-legacy.csv' }, + @{ inputValue = 'default'; expected = './bundlepackages/azure-dotnet.csv' } + ) { + $result = GetCiConfigPath '.' $inputValue + $result | Should -Be $expected + } + + It 'returns latest by default' { + $result = GetCiConfigPath '.' 'some random value' + $result | Should -Be './bundlepackages/azure-dotnet.csv' + } +} + +Describe 'GetPackageId' { + It 'returns when given ' -ForEach @( + @{ inputValue = 'Name.With.Dot.Separators'; expected = 'namewithdotseparators' }, + @{ inputValue = 'Name.With.Dots-and-dashes'; expected = 'namewithdots-and-dashes' } + ) { + $result = GetPackageId $inputValue + $result | Should -Be $expected + } +}