openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Test-ApiCompatibility.ps1
186lines · modeblame
dff9bb58Jose Arriaga Maldonado1 years ago | 1 | <# |
| 2 | .SYNOPSIS | |
| 3 | Validates API compatibility between different versions of a .NET NuGet package. | |
| 4 | | |
| 5 | .DESCRIPTION | |
| 6 | This script performs API compatibility checks between a current version of a .NET package | |
| 7 | and a specified baseline version from NuGet.org. It uses the 'apicompat' tool to detect | |
| 8 | breaking changes and supports excluding specific namespaces from the comparison. | |
| 9 | | |
| 10 | The script performs these operations in sequence: | |
| 11 | 1. Builds and packs the current version of the package | |
| 12 | 2. Downloads the specified baseline version from NuGet.org | |
| 13 | 3. Compares the two versions using the 'apicompat' tool | |
| 14 | 4. Reports any breaking changes found in non-ignored namespaces | |
| 15 | | |
| 16 | .PARAMETER ProjectPath | |
| 17 | The path to the .NET project file (.csproj) to build and analyze | |
| 18 | | |
| 19 | .PARAMETER ReleasePath | |
| 20 | The output directory where the built package will be placed | |
| 21 | | |
| 22 | .PARAMETER PackageName | |
| 23 | The name of the NuGet package to compare | |
| 24 | | |
| 25 | .PARAMETER BaselineVersion | |
| 26 | The version number of the baseline package to compare against | |
| 27 | | |
| 28 | .PARAMETER IgnoredNamespaces | |
| 29 | An optional array of namespace names to exclude from the compatibility check | |
| 30 | | |
| 31 | .EXAMPLE | |
| 32 | .\Test-ApiCompatibility.ps1 | |
| 33 | Runs the compatibility check using default parameters for the OpenAI SDK | |
| 34 | | |
| 35 | .EXAMPLE | |
| 36 | Invoke-APICompat -ProjectPath "src\MyProject.csproj" -ReleasePath "src\bin\Release" -PackageName "MyPackage" -BaselineVersion "1.0.0" | |
| 37 | Runs a compatibility check between the current version and version 1.0.0 of MyPackage | |
| 38 | | |
| 39 | .NOTES | |
| 40 | The script will generate warnings if breaking changes are detected in non-ignored namespaces. | |
| 41 | #> | |
| 42 | | |
| 43 | function Invoke-DotNetBuild { | |
| 44 | param( | |
| 45 | [Parameter(Mandatory = $true)] | |
| 46 | [string]$ProjectPath | |
| 47 | ) | |
| 48 | | |
| 49 | Write-Output "Building $($ProjectPath)..." | |
| 50 | Write-Output "" | |
| 51 | & dotnet build $ProjectPath | |
| 52 | Write-Output "" | |
| 53 | } | |
| 54 | | |
| 55 | function Invoke-DotNetPack { | |
| 56 | param( | |
| 57 | [Parameter(Mandatory = $true)] | |
| 58 | [string]$ProjectPath | |
| 59 | ) | |
| 60 | | |
| 61 | Write-Output "Packing $($ProjectPath)..." | |
| 62 | Write-Output "" | |
| 63 | & dotnet pack $ProjectPath | |
| 64 | Write-Output "" | |
| 65 | } | |
| 66 | | |
| 67 | function Invoke-APICompat { | |
| 68 | param ( | |
| 69 | [Parameter(Mandatory = $true)] | |
| 70 | [string]$ProjectPath, | |
| 71 | | |
06c6fe07Jose Arriaga Maldonado5 months ago | 72 | [Parameter(Mandatory = $true)] |
| 73 | [string]$DirectoryBuildPropsPath, | |
| 74 | | |
dff9bb58Jose Arriaga Maldonado1 years ago | 75 | [Parameter(Mandatory = $true)] |
| 76 | [string]$ReleasePath, | |
| 77 | | |
| 78 | [Parameter(Mandatory = $true)] | |
| 79 | [string]$PackageName, | |
| 80 | | |
| 81 | [Parameter(Mandatory = $true)] | |
| 82 | [string]$BaselineVersion, | |
| 83 | | |
| 84 | [Parameter(Mandatory = $false)] | |
| 85 | [string[]]$IgnoredNamespaces | |
| 86 | ) | |
| 87 | | |
| 88 | try | |
| 89 | { | |
| 90 | Invoke-DotNetBuild -ProjectPath $ProjectPath | |
| 91 | | |
| 92 | Invoke-DotNetPack -ProjectPath $ProjectPath | |
| 93 | | |
| 94 | # Extract the values of VersionPrefix and VersionSuffix from the .csproj XML file. | |
06c6fe07Jose Arriaga Maldonado5 months ago | 95 | $xml = [xml](Get-Content $DirectoryBuildPropsPath) |
| 96 | $versionPrefix = $($xml.Project.PropertyGroup[3].VersionPrefix) | |
| 97 | $versionSuffix = $($xml.Project.PropertyGroup[3].VersionSuffix) | |
dff9bb58Jose Arriaga Maldonado1 years ago | 98 | $currentVersion = [string]::IsNullOrEmpty($versionSuffix) ? "$($versionPrefix)" : "$($versionPrefix)-$($versionSuffix)" |
| 99 | | |
| 100 | $currentNuGetPackagePath = Join-Path $ReleasePath "$($PackageName).$($currentVersion).nupkg" | |
| 101 | $currentNuGetSymbolsPath = Join-Path $ReleasePath "$($PackageName).$($currentVersion).snupkg" | |
| 102 | | |
| 103 | # Create temporary folder | |
| 104 | $tempFolderPath = Join-Path $PSScriptRoot "\TempApiCompatibility" | |
| 105 | New-Item -ItemType Directory -Path $tempFolderPath | Out-Null | |
| 106 | | |
| 107 | # Download OpenAI NuGet package | |
| 108 | $baselineNuGetPackageName = "$($PackageName).$($BaselineVersion).nupkg" | |
| 109 | $baselineNuGetPackagePath = Join-Path $tempFolderPath $baselineNuGetPackageName | |
| 110 | $baselineNuGetPackageUrl = "https://www.nuget.org/api/v2/package/$($PackageName)/$($BaselineVersion)" | |
| 111 | Invoke-RestMethod -Uri $baselineNuGetPackageUrl -OutFile $baselineNuGetPackagePath | |
| 112 | Sleep 10 | |
| 113 | | |
| 114 | Write-Output "Testing API compatibility between versions $($currentVersion) (current) and $($BaselineVersion) (baseline)..." | |
| 115 | Write-Output "" | |
| 116 | | |
| 117 | # Run apicompat and redirect the error output to a variable | |
| 118 | $output = apicompat package $currentNuGetPackagePath --baseline-package $baselineNuGetPackagePath 2>&1 | |
| 119 | | |
| 120 | # Individual warnings from apicompat have identifiers such as "CP0001", "CP0002", etc. | |
| 121 | $warningRegex = "CP\d\d\d\d" | |
| 122 | | |
| 123 | # Concatenate the ignored namespaces into a single string, delimiting them by "|" and escaping the "." | |
| 124 | $ignoredRegex = $IgnoredNamespaces -join "|" -creplace "\.", "\." | |
| 125 | | |
| 126 | Write-Output $excludedRegex | |
| 127 | | |
| 128 | $warningsFound = 0 | |
| 129 | | |
| 130 | foreach ($line in $($output -split "`r`n")) { | |
| 131 | if ($line -cmatch $warningRegex) { | |
| 132 | if ($($line -cnotmatch $ignoredRegex)) { | |
| 133 | $warningsFound++ | |
| 134 | } | |
| 135 | } | |
| 136 | } | |
| 137 | | |
| 138 | if ($warningsFound -eq 0) { | |
| 139 | Write-Output "No API breaking changes found." | |
| 140 | Write-Output "" | |
| 141 | } | |
| 142 | else { | |
| 143 | foreach ($line in $($output -split "`r`n")) { | |
| 144 | if ($line -cmatch $warningRegex) { | |
| 145 | if ($($line -cnotmatch $ignoredRegex)){ | |
| 146 | Write-Warning "$line" | |
| 147 | Write-Output "" | |
| 148 | } | |
| 149 | } | |
| 150 | else { | |
| 151 | Write-Output "$line" | |
| 152 | Write-Output "" | |
| 153 | } | |
| 154 | } | |
| 155 | } | |
| 156 | } | |
| 157 | finally { | |
| 158 | Remove-Item -Path $tempFolderPath -Recurse -Force | |
| 159 | Remove-Item -Path $currentNuGetPackagePath -Force | |
| 160 | Remove-Item -Path $currentNuGetSymbolsPath -Force | |
| 161 | } | |
| 162 | } | |
| 163 | | |
| 164 | $repoRootPath = Join-Path $PSScriptRoot .. -Resolve | |
| 165 | $projectPath = Join-Path $repoRootPath "src\OpenAI.csproj" | |
06c6fe07Jose Arriaga Maldonado5 months ago | 166 | $buildPropsPath = Join-Path $repoRootPath "Directory.Build.props" |
dff9bb58Jose Arriaga Maldonado1 years ago | 167 | $releasePath = Join-Path $repoRootPath "src\bin\Release" |
| 168 | | |
3ab4a3c4Jose Arriaga Maldonado1 years ago | 169 | $experimentalNamespaces = @( |
| 170 | "OpenAI.Assistants", | |
| 171 | "OpenAI.Batch", | |
| 172 | "OpenAI.Containers", | |
| 173 | "OpenAI.Evals", | |
| 174 | "OpenAI.FineTuning", | |
| 175 | "OpenAI.Graders", | |
| 176 | "OpenAI.Realtime", | |
| 177 | "OpenAI.Responses", | |
| 178 | "OpenAI.VectorStores" | |
| 179 | ) | |
| 180 | | |
dff9bb58Jose Arriaga Maldonado1 years ago | 181 | Invoke-APICompat -ProjectPath $projectPath ` |
06c6fe07Jose Arriaga Maldonado5 months ago | 182 | -DirectoryBuildPropsPath $buildPropsPath ` |
dff9bb58Jose Arriaga Maldonado1 years ago | 183 | -ReleasePath $releasePath ` |
| 184 | -PackageName "OpenAI" ` | |
06c6fe07Jose Arriaga Maldonado5 months ago | 185 | -BaselineVersion "2.8.0" ` |
3ab4a3c4Jose Arriaga Maldonado1 years ago | 186 | -IgnoredNamespaces $experimentalNamespaces |