openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Test-ApiCompatibility.ps1
181lines · modecode
| 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 | |
| 72 | [Parameter(Mandatory = $true)] |
| 73 | [string]$ReleasePath, |
| 74 | |
| 75 | [Parameter(Mandatory = $true)] |
| 76 | [string]$PackageName, |
| 77 | |
| 78 | [Parameter(Mandatory = $true)] |
| 79 | [string]$BaselineVersion, |
| 80 | |
| 81 | [Parameter(Mandatory = $false)] |
| 82 | [string[]]$IgnoredNamespaces |
| 83 | ) |
| 84 | |
| 85 | try |
| 86 | { |
| 87 | Invoke-DotNetBuild -ProjectPath $ProjectPath |
| 88 | |
| 89 | Invoke-DotNetPack -ProjectPath $ProjectPath |
| 90 | |
| 91 | # Extract the values of VersionPrefix and VersionSuffix from the .csproj XML file. |
| 92 | $xml = [xml](Get-Content $ProjectPath) |
| 93 | $versionPrefix = $($xml.Project.PropertyGroup[0].VersionPrefix) |
| 94 | $versionSuffix = $($xml.Project.PropertyGroup[0].VersionSuffix) |
| 95 | $currentVersion = [string]::IsNullOrEmpty($versionSuffix) ? "$($versionPrefix)" : "$($versionPrefix)-$($versionSuffix)" |
| 96 | |
| 97 | $currentNuGetPackagePath = Join-Path $ReleasePath "$($PackageName).$($currentVersion).nupkg" |
| 98 | $currentNuGetSymbolsPath = Join-Path $ReleasePath "$($PackageName).$($currentVersion).snupkg" |
| 99 | |
| 100 | # Create temporary folder |
| 101 | $tempFolderPath = Join-Path $PSScriptRoot "\TempApiCompatibility" |
| 102 | New-Item -ItemType Directory -Path $tempFolderPath | Out-Null |
| 103 | |
| 104 | # Download OpenAI NuGet package |
| 105 | $baselineNuGetPackageName = "$($PackageName).$($BaselineVersion).nupkg" |
| 106 | $baselineNuGetPackagePath = Join-Path $tempFolderPath $baselineNuGetPackageName |
| 107 | $baselineNuGetPackageUrl = "https://www.nuget.org/api/v2/package/$($PackageName)/$($BaselineVersion)" |
| 108 | Invoke-RestMethod -Uri $baselineNuGetPackageUrl -OutFile $baselineNuGetPackagePath |
| 109 | Sleep 10 |
| 110 | |
| 111 | Write-Output "Testing API compatibility between versions $($currentVersion) (current) and $($BaselineVersion) (baseline)..." |
| 112 | Write-Output "" |
| 113 | |
| 114 | # Run apicompat and redirect the error output to a variable |
| 115 | $output = apicompat package $currentNuGetPackagePath --baseline-package $baselineNuGetPackagePath 2>&1 |
| 116 | |
| 117 | # Individual warnings from apicompat have identifiers such as "CP0001", "CP0002", etc. |
| 118 | $warningRegex = "CP\d\d\d\d" |
| 119 | |
| 120 | # Concatenate the ignored namespaces into a single string, delimiting them by "|" and escaping the "." |
| 121 | $ignoredRegex = $IgnoredNamespaces -join "|" -creplace "\.", "\." |
| 122 | |
| 123 | Write-Output $excludedRegex |
| 124 | |
| 125 | $warningsFound = 0 |
| 126 | |
| 127 | foreach ($line in $($output -split "`r`n")) { |
| 128 | if ($line -cmatch $warningRegex) { |
| 129 | if ($($line -cnotmatch $ignoredRegex)) { |
| 130 | $warningsFound++ |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if ($warningsFound -eq 0) { |
| 136 | Write-Output "No API breaking changes found." |
| 137 | Write-Output "" |
| 138 | } |
| 139 | else { |
| 140 | foreach ($line in $($output -split "`r`n")) { |
| 141 | if ($line -cmatch $warningRegex) { |
| 142 | if ($($line -cnotmatch $ignoredRegex)){ |
| 143 | Write-Warning "$line" |
| 144 | Write-Output "" |
| 145 | } |
| 146 | } |
| 147 | else { |
| 148 | Write-Output "$line" |
| 149 | Write-Output "" |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | finally { |
| 155 | Remove-Item -Path $tempFolderPath -Recurse -Force |
| 156 | Remove-Item -Path $currentNuGetPackagePath -Force |
| 157 | Remove-Item -Path $currentNuGetSymbolsPath -Force |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | $repoRootPath = Join-Path $PSScriptRoot .. -Resolve |
| 162 | $projectPath = Join-Path $repoRootPath "src\OpenAI.csproj" |
| 163 | $releasePath = Join-Path $repoRootPath "src\bin\Release" |
| 164 | |
| 165 | $experimentalNamespaces = @( |
| 166 | "OpenAI.Assistants", |
| 167 | "OpenAI.Batch", |
| 168 | "OpenAI.Containers", |
| 169 | "OpenAI.Evals", |
| 170 | "OpenAI.FineTuning", |
| 171 | "OpenAI.Graders", |
| 172 | "OpenAI.Realtime", |
| 173 | "OpenAI.Responses", |
| 174 | "OpenAI.VectorStores" |
| 175 | ) |
| 176 | |
| 177 | Invoke-APICompat -ProjectPath $projectPath ` |
| 178 | -ReleasePath $releasePath ` |
| 179 | -PackageName "OpenAI" ` |
| 180 | -BaselineVersion "2.7.0" ` |
| 181 | -IgnoredNamespaces $experimentalNamespaces |