openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.9.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/Test-ApiCompatibility.ps1

186lines · modeblame

dff9bb58Jose Arriaga Maldonado1 years ago1<#
2.SYNOPSIS
3Validates API compatibility between different versions of a .NET NuGet package.
4
5.DESCRIPTION
6This script performs API compatibility checks between a current version of a .NET package
7and a specified baseline version from NuGet.org. It uses the 'apicompat' tool to detect
8breaking changes and supports excluding specific namespaces from the comparison.
9
10The script performs these operations in sequence:
111. Builds and packs the current version of the package
122. Downloads the specified baseline version from NuGet.org
133. Compares the two versions using the 'apicompat' tool
144. Reports any breaking changes found in non-ignored namespaces
15
16.PARAMETER ProjectPath
17The path to the .NET project file (.csproj) to build and analyze
18
19.PARAMETER ReleasePath
20The output directory where the built package will be placed
21
22.PARAMETER PackageName
23The name of the NuGet package to compare
24
25.PARAMETER BaselineVersion
26The version number of the baseline package to compare against
27
28.PARAMETER IgnoredNamespaces
29An optional array of namespace names to exclude from the compatibility check
30
31.EXAMPLE
32.\Test-ApiCompatibility.ps1
33Runs the compatibility check using default parameters for the OpenAI SDK
34
35.EXAMPLE
36Invoke-APICompat -ProjectPath "src\MyProject.csproj" -ReleasePath "src\bin\Release" -PackageName "MyPackage" -BaselineVersion "1.0.0"
37Runs a compatibility check between the current version and version 1.0.0 of MyPackage
38
39.NOTES
40The script will generate warnings if breaking changes are detected in non-ignored namespaces.
41#>
42
43function Invoke-DotNetBuild {
44param(
45[Parameter(Mandatory = $true)]
46[string]$ProjectPath
47)
48
49Write-Output "Building $($ProjectPath)..."
50Write-Output ""
51& dotnet build $ProjectPath
52Write-Output ""
53}
54
55function Invoke-DotNetPack {
56param(
57[Parameter(Mandatory = $true)]
58[string]$ProjectPath
59)
60
61Write-Output "Packing $($ProjectPath)..."
62Write-Output ""
63& dotnet pack $ProjectPath
64Write-Output ""
65}
66
67function Invoke-APICompat {
68param (
69[Parameter(Mandatory = $true)]
70[string]$ProjectPath,
71
06c6fe07Jose Arriaga Maldonado5 months ago72[Parameter(Mandatory = $true)]
73[string]$DirectoryBuildPropsPath,
74
dff9bb58Jose Arriaga Maldonado1 years ago75[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
88try
89{
90Invoke-DotNetBuild -ProjectPath $ProjectPath
91
92Invoke-DotNetPack -ProjectPath $ProjectPath
93
94# Extract the values of VersionPrefix and VersionSuffix from the .csproj XML file.
06c6fe07Jose Arriaga Maldonado5 months ago95$xml = [xml](Get-Content $DirectoryBuildPropsPath)
96$versionPrefix = $($xml.Project.PropertyGroup[3].VersionPrefix)
97$versionSuffix = $($xml.Project.PropertyGroup[3].VersionSuffix)
dff9bb58Jose Arriaga Maldonado1 years ago98$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"
105New-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)"
111Invoke-RestMethod -Uri $baselineNuGetPackageUrl -OutFile $baselineNuGetPackagePath
112Sleep 10
113
114Write-Output "Testing API compatibility between versions $($currentVersion) (current) and $($BaselineVersion) (baseline)..."
115Write-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
126Write-Output $excludedRegex
127
128$warningsFound = 0
129
130foreach ($line in $($output -split "`r`n")) {
131if ($line -cmatch $warningRegex) {
132if ($($line -cnotmatch $ignoredRegex)) {
133$warningsFound++
134}
135}
136}
137
138if ($warningsFound -eq 0) {
139Write-Output "No API breaking changes found."
140Write-Output ""
141}
142else {
143foreach ($line in $($output -split "`r`n")) {
144if ($line -cmatch $warningRegex) {
145if ($($line -cnotmatch $ignoredRegex)){
146Write-Warning "$line"
147Write-Output ""
148}
149}
150else {
151Write-Output "$line"
152Write-Output ""
153}
154}
155}
156}
157finally {
158Remove-Item -Path $tempFolderPath -Recurse -Force
159Remove-Item -Path $currentNuGetPackagePath -Force
160Remove-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 ago166$buildPropsPath = Join-Path $repoRootPath "Directory.Build.props"
dff9bb58Jose Arriaga Maldonado1 years ago167$releasePath = Join-Path $repoRootPath "src\bin\Release"
168
3ab4a3c4Jose Arriaga Maldonado1 years ago169$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 ago181Invoke-APICompat -ProjectPath $projectPath `
06c6fe07Jose Arriaga Maldonado5 months ago182-DirectoryBuildPropsPath $buildPropsPath `
dff9bb58Jose Arriaga Maldonado1 years ago183-ReleasePath $releasePath `
184-PackageName "OpenAI" `
06c6fe07Jose Arriaga Maldonado5 months ago185-BaselineVersion "2.8.0" `
3ab4a3c4Jose Arriaga Maldonado1 years ago186-IgnoredNamespaces $experimentalNamespaces