openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joseharriaga/stable-api-listing

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/Submit-GeneratorUpdatePr.ps1

335lines · modecode

1#!/usr/bin/env pwsh
2
3<#
4.DESCRIPTION
5Creates a pull request to update the @typespec/http-client-csharp dependency in the OpenAI SDK for .NET repository.
6This script follows the pattern used by the TypeSpec repository for creating PRs in downstream repositories.
7
8.PARAMETER PackageVersion
9The version of the @typespec/http-client-csharp package to update to.
10
11.PARAMETER AuthToken
12A GitHub personal access token for authentication.
13
14.PARAMETER BranchName
15The name of the branch to create in the repository.
16
17.PARAMETER RepoPath
18The path to the local repository. Defaults to current directory.
19
20.EXAMPLE
21# Update to a specific version
22./Submit-GeneratorUpdatePr.ps1 -PackageVersion "1.0.0-alpha.20250625.4" -AuthToken "ghp_xxxx"
23#>
24[CmdletBinding(SupportsShouldProcess = $true)]
25param(
26 [Parameter(Mandatory = $true)]
27 [string]$PackageVersion,
28
29 [Parameter(Mandatory = $true)]
30 [string]$AuthToken,
31
32 [Parameter(Mandatory = $false)]
33 [string]$BranchName = "typespec/update-http-client-csharp-$PackageVersion",
34
35 [Parameter(Mandatory = $false)]
36 [string]$RepoPath = ".",
37
38 [Parameter(Mandatory = $false)]
39 [string]$ActionRunUrl = ""
40)
41
42# Set up variables for the PR
43# Track if any warnings were encountered during execution
44$WarningsEncountered = $false
45$RepoOwner = "openai"
46$RepoName = "openai-dotnet"
47$BaseBranch = "main"
48$PRBranch = $BranchName
49
50function Write-Log {
51 param([string]$Message)
52 Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message" -ForegroundColor Green
53}
54
55function Write-WarningLog {
56 param([string]$Message)
57 Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): WARNING: $Message" -ForegroundColor Yellow
58 # Set the global warning flag to track that warnings occurred
59 $script:WarningsEncountered = $true
60}
61
62function Write-ErrorLog {
63 param([string]$Message)
64 Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): ERROR: $Message" -ForegroundColor Red
65}
66
67# Function to get package dependencies using npm view
68function Get-PackageDependencies {
69 param([string]$PackageName, [string]$PackageVersion)
70
71 Write-Log "Fetching dependencies for $PackageName version $PackageVersion"
72
73 try {
74 # Properly format the package specification for npm view
75 $packageSpec = "$PackageName@$PackageVersion"
76
77 # Run npm view command and parse JSON output
78 $npmViewOutput = & npm view $packageSpec devDependencies --json 2>&1
79
80 # Check if there was an error
81 if ($LASTEXITCODE -ne 0) {
82 Write-WarningLog "Failed to get dependencies for $PackageName version $PackageVersion. Error: $npmViewOutput"
83 return $null
84 }
85
86 # Parse the JSON output
87 $dependencies = $npmViewOutput | ConvertFrom-Json
88
89 return $dependencies
90 }
91 catch {
92 Write-WarningLog "Error fetching dependencies for $PackageName version $PackageVersion $_"
93 return $null
94 }
95}
96
97$InjectedDependencies = @(
98 '@azure-tools/typespec-client-generator-core',
99 '@typespec/http',
100 '@typespec/openapi'
101)
102
103Write-Log "Starting TypeSpec generator update process"
104Write-Log "Target version: $PackageVersion"
105Write-Log "Repository: $RepoOwner/$RepoName"
106Write-Log "Branch: $PRBranch"
107
108try {
109 Push-Location $RepoPath
110
111 # Get current version from package.json files
112 $openAiPackageJsonPath = "codegen/package.json"
113
114 if (-not (Test-Path $openAiPackageJsonPath)) {
115 throw "OpenAI package.json not found at: $openAiPackageJsonPath"
116 }
117
118 # Read current versions
119 $openAiPackageJson = Get-Content $openAiPackageJsonPath -Raw | ConvertFrom-Json
120
121 $currentVersion = $openAiPackageJson.dependencies.'@typespec/http-client-csharp'
122
123 Write-Log "Current OpenAI version: $currentVersion"
124
125 # Check if update is needed
126 if ($currentVersion -eq $PackageVersion) {
127 Write-Log "No update needed. Already at version: $PackageVersion"
128 return
129 }
130
131 Write-Log "Update needed: $currentVersion -> $PackageVersion"
132
133 # Create a new branch
134 Write-Log "Creating branch: $PRBranch"
135 git checkout -b $PRBranch
136 if ($LASTEXITCODE -ne 0) {
137 throw "Failed to create branch: $PRBranch"
138 }
139
140 # Update OpenAI package.json
141 Write-Log "Updating OpenAI package.json"
142 # Fetch dependencies of the http-client-csharp package
143 $httpClientDependencies = Get-PackageDependencies -PackageName '@typespec/http-client-csharp' -PackageVersion $PackageVersion
144
145 # Update the injected dependencies in the package.json
146 if ($httpClientDependencies -ne $null) {
147 Write-Log "Updating injected dependencies in OpenAI package.json"
148
149 foreach ($dependency in $InjectedDependencies) {
150 if ($httpClientDependencies.PSObject.Properties.Name -contains $dependency) {
151 $dependencyVersion = $httpClientDependencies.$dependency
152 Write-Log "Updating $dependency to version $dependencyVersion"
153
154 # Update the dependency in the package.json
155 if ($openAiPackageJson.dependencies.PSObject.Properties.Name -contains $dependency) {
156 $openAiPackageJson.dependencies.$dependency = $dependencyVersion
157 Write-Log "Updated $dependency to version $dependencyVersion"
158 } else {
159 Write-WarningLog "Dependency $dependency not found in package.json"
160 }
161 } else {
162 Write-WarningLog "Dependency $dependency not found in @typespec/http-client-csharp version $PackageVersion"
163 }
164 }
165 } else {
166 Write-WarningLog "Could not fetch dependencies for @typespec/http-client-csharp version $PackageVersion"
167 }
168
169
170 $openAiPackageJson.dependencies.'@typespec/http-client-csharp' = $PackageVersion
171 $openAiPackageJson | ConvertTo-Json -Depth 10 | Set-Content -Path $openAiPackageJsonPath
172
173 # Update Microsoft.TypeSpec.Generator.ClientModel version in Directory.Packages.props (central package management)
174 $directoryPackagesPropsPath = "Directory.Packages.props"
175
176 Write-Log "Updating Microsoft.TypeSpec.Generator.ClientModel version in Directory.Packages.props"
177
178 if (Test-Path $directoryPackagesPropsPath) {
179 $directoryPackagesProps = Get-Content $directoryPackagesPropsPath -Raw
180 $directoryPackagesProps = $directoryPackagesProps -replace '(<PackageVersion Include="Microsoft\.TypeSpec\.Generator\.ClientModel" Version=")[^"]*(")', "`${1}$PackageVersion`${2}"
181 Set-Content -Path $directoryPackagesPropsPath -Value $directoryPackagesProps -NoNewline
182 Write-Log "Updated Directory.Packages.props: $directoryPackagesPropsPath"
183 } else {
184 Write-WarningLog "Directory.Packages.props not found at: $directoryPackagesPropsPath"
185 }
186
187 # Delete previous package-lock.json
188 Write-Log "Deleting previous package-lock.json"
189 if (Test-Path "package-lock.json") {
190 Remove-Item -Path "package-lock.json" -Force
191 }
192
193 # Install dependencies from root directory (using workspaces)
194 Write-Log "Installing dependencies from root directory"
195 npm install
196 if ($LASTEXITCODE -ne 0) {
197 throw "npm install failed"
198 }
199
200 # Build OpenAI plugin
201 Write-Log "Building OpenAI plugin"
202 Push-Location "codegen"
203 try {
204 & npm run clean
205 if ($LASTEXITCODE -ne 0) {
206 throw "npm run clean failed with exit code $LASTEXITCODE"
207 }
208
209 & npm run build
210 if ($LASTEXITCODE -ne 0) {
211 throw "npm run build failed with exit code $LASTEXITCODE"
212 }
213 } catch {
214 Write-WarningLog "OpenAI plugin build failed, but continuing: $_"
215 }
216 Pop-Location
217
218 # Regenerate OpenAI SDK code
219 Write-Log "Regenerating OpenAI SDK code"
220 Push-Location "."
221 try {
222 pwsh scripts/Invoke-CodeGen.ps1
223 } catch {
224 Write-WarningLog "OpenAI code generation failed: $_"
225 }
226 Pop-Location
227
228 # Build the updated library
229 Write-Log "Building the library"
230 Push-Location "."
231 try {
232 & dotnet build src/OpenAI.csproj
233 if ($LASTEXITCODE -ne 0) {
234 throw "Build failed with exit code $LASTEXITCODE"
235 }
236 } catch {
237 Write-WarningLog "Building the library failed: $_"
238 }
239 Pop-Location
240
241 # Check if there are changes to commit
242 $gitStatus = git status --porcelain
243 if (-not $gitStatus) {
244 Write-Log "No changes detected. Skipping commit and PR creation."
245 return
246 }
247
248 # Configure git
249 git config --local user.email "action@github.com"
250 git config --local user.name "GitHub Action"
251
252 # Add and commit changes
253 Write-Log "Adding and committing changes"
254 git add codegen/package.json
255 git add Directory.Packages.props
256 git add api
257 git add package-lock.json
258 git add ./ # Add any generated code changes
259
260 $commitMessage = @"
261Update @typespec/http-client-csharp to $PackageVersion
262
263- Updated @typespec/http-client-csharp from $currentVersion to $PackageVersion
264- Updated Microsoft.TypeSpec.Generator.ClientModel from $currentVersion to $PackageVersion
265- Regenerated OpenAI SDK code with new generator version
266- Updated centrally managed package-lock.json file with new dependency versions
267"@
268
269 git commit -m $commitMessage
270 if ($LASTEXITCODE -ne 0) {
271 throw "Failed to commit changes"
272 }
273
274 # Push the branch
275 Write-Log "Pushing branch to remote"
276 git push origin $PRBranch
277 if ($LASTEXITCODE -ne 0) {
278 throw "Failed to push branch"
279 }
280
281 # Create PR using GitHub CLI
282 Write-Log "Creating PR using GitHub CLI"
283 $env:GH_TOKEN = $AuthToken
284
285 # Update PR title if warnings were encountered
286 $prTitle = "Update @typespec/http-client-csharp to $PackageVersion"
287 if ($WarningsEncountered) {
288 $prTitle = "Succeeded with Issues: $prTitle"
289 }
290 $prBody = @"
291This PR automatically updates the TypeSpec HTTP client C# generator version and regenerates the SDK code.
292
293## Changes
294- Updated ``@typespec/http-client-csharp`` from ``$currentVersion`` to ``$PackageVersion``
295- Updated ``Microsoft.TypeSpec.Generator.ClientModel`` from ``$currentVersion`` to ``$PackageVersion``
296- Updated OpenAI plugin package.json file
297- Updated ``Directory.Packages.props`` with new generator package version
298- Regenerated OpenAI SDK code using the new generator version
299- Updated centrally managed package-lock.json file with new dependency versions
300
301## Details
302- Generator package: [@typespec/http-client-csharp](https://www.npmjs.com/package/@typespec/http-client-csharp)
303- Version update: ``$currentVersion`` → ``$PackageVersion``
304
305## Testing
306Please run the existing test suites to ensure the generated code works correctly:
307- Build and test the OpenAI SDK
308- Verify API compatibility and functionality
309
310## Notes
311This PR was created automatically by the **Update TypeSpec Generator Version** workflow. The workflow runs weekly and when manually triggered to keep the generator version current with the latest TypeSpec improvements and fixes.
312$(if ($ActionRunUrl) { "`n- [Action Run]($ActionRunUrl)" })
313If there are any issues with the generated code, please review the [TypeSpec release notes](https://github.com/microsoft/typespec/releases) for breaking changes or new features that may require manual adjustments.
314"@
315
316 $prUrl = gh pr create --title $prTitle --body $prBody --base $BaseBranch --head $PRBranch 2>&1
317
318 if ($LASTEXITCODE -ne 0) {
319 throw "Failed to create PR using gh CLI: $prUrl"
320 }
321
322 Write-Log "Successfully created PR: $prUrl"
323 # If warnings were encountered, make the script exit with non-zero code
324 # This will mark the GitHub Action step as failed but still create the PR
325 if ($WarningsEncountered) {
326 Write-WarningLog "Warnings were encountered during execution. PR was created but marking step as failed."
327 exit 1
328 }
329
330} catch {
331 Write-ErrorLog "Error creating PR: $_"
332 exit 1
333} finally {
334 Pop-Location
335}
336