|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +<# |
| 4 | +.DESCRIPTION |
| 5 | +Creates a pull request to update the @typespec/http-client-csharp dependency in the OpenAI SDK for .NET repository. |
| 6 | +This script follows the pattern used by the TypeSpec repository for creating PRs in downstream repositories. |
| 7 | +
|
| 8 | +.PARAMETER PackageVersion |
| 9 | +The version of the @typespec/http-client-csharp package to update to. |
| 10 | +
|
| 11 | +.PARAMETER AuthToken |
| 12 | +A GitHub personal access token for authentication. |
| 13 | +
|
| 14 | +.PARAMETER BranchName |
| 15 | +The name of the branch to create in the repository. |
| 16 | +
|
| 17 | +.PARAMETER RepoPath |
| 18 | +The 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)] |
| 25 | +param( |
| 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 | + |
| 39 | +# Set up variables for the PR |
| 40 | +$RepoOwner = "openai" |
| 41 | +$RepoName = "openai-dotnet" |
| 42 | +$BaseBranch = "main" |
| 43 | +$PRBranch = $BranchName |
| 44 | + |
| 45 | +function Write-Log { |
| 46 | + param([string]$Message) |
| 47 | + Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message" -ForegroundColor Green |
| 48 | +} |
| 49 | + |
| 50 | +function Write-Warning-Log { |
| 51 | + param([string]$Message) |
| 52 | + Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): WARNING: $Message" -ForegroundColor Yellow |
| 53 | +} |
| 54 | + |
| 55 | +function Write-Error-Log { |
| 56 | + param([string]$Message) |
| 57 | + Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): ERROR: $Message" -ForegroundColor Red |
| 58 | +} |
| 59 | + |
| 60 | +Write-Log "Starting TypeSpec generator update process" |
| 61 | +Write-Log "Target version: $PackageVersion" |
| 62 | +Write-Log "Repository: $RepoOwner/$RepoName" |
| 63 | +Write-Log "Branch: $PRBranch" |
| 64 | + |
| 65 | +try { |
| 66 | + Push-Location $RepoPath |
| 67 | + |
| 68 | + # Get current version from package.json files |
| 69 | + $openAiPackageJsonPath = "codegen/package.json" |
| 70 | + |
| 71 | + if (-not (Test-Path $openAiPackageJsonPath)) { |
| 72 | + throw "OpenAI package.json not found at: $openAiPackageJsonPath" |
| 73 | + } |
| 74 | + |
| 75 | + # Read current versions |
| 76 | + $openAiPackageJson = Get-Content $openAiPackageJsonPath -Raw | ConvertFrom-Json |
| 77 | + |
| 78 | + $currentVersion = $openAiPackageJson.dependencies.'@typespec/http-client-csharp' |
| 79 | + |
| 80 | + Write-Log "Current OpenAI version: $currentVersion" |
| 81 | + |
| 82 | + # Check if update is needed |
| 83 | + if ($currentVersion -eq $PackageVersion) { |
| 84 | + Write-Log "No update needed. Already at version: $PackageVersion" |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + Write-Log "Update needed: $currentVersion -> $PackageVersion" |
| 89 | + |
| 90 | + # Create a new branch |
| 91 | + Write-Log "Creating branch: $PRBranch" |
| 92 | + git checkout -b $PRBranch |
| 93 | + if ($LASTEXITCODE -ne 0) { |
| 94 | + throw "Failed to create branch: $PRBranch" |
| 95 | + } |
| 96 | + |
| 97 | + # Update OpenAI package.json |
| 98 | + Write-Log "Updating OpenAI package.json" |
| 99 | + $openAiPackageJson.dependencies.'@typespec/http-client-csharp' = $PackageVersion |
| 100 | + $openAiPackageJson | ConvertTo-Json -Depth 10 | Set-Content -Path $openAiPackageJsonPath |
| 101 | + |
| 102 | + # Update Microsoft.TypeSpec.Generator.ClientModel version in csproj files |
| 103 | + $openAiCsprojPath = "codegen/generator/src/OpenAI.Library.Plugin.csproj" |
| 104 | + |
| 105 | + Write-Log "Updating Microsoft.TypeSpec.Generator.ClientModel version in csproj files" |
| 106 | + |
| 107 | + # Update OpenAI csproj |
| 108 | + if (Test-Path $openAiCsprojPath) { |
| 109 | + $openAiCsproj = Get-Content $openAiCsprojPath -Raw |
| 110 | + $openAiCsproj = $openAiCsproj -replace '(<PackageReference Include="Microsoft\.TypeSpec\.Generator\.ClientModel" Version=")[^"]*(")', "`${1}$PackageVersion`${2}" |
| 111 | + Set-Content -Path $openAiCsprojPath -Value $openAiCsproj |
| 112 | + Write-Log "Updated OpenAI csproj: $openAiCsprojPath" |
| 113 | + } else { |
| 114 | + Write-Warning-Log "OpenAI csproj not found at: $openAiCsprojPath" |
| 115 | + } |
| 116 | + |
| 117 | + # Install dependencies from codegen directory (using workspaces) |
| 118 | + Write-Log "Installing dependencies from codegen directory (using npm workspaces)" |
| 119 | + Push-Location "codegen" |
| 120 | + npm install |
| 121 | + if ($LASTEXITCODE -ne 0) { |
| 122 | + throw "npm install failed" |
| 123 | + } |
| 124 | + |
| 125 | + # Build OpenAI plugin |
| 126 | + Write-Log "Building OpenAI plugin" |
| 127 | + npm run clean && npm run build |
| 128 | + if ($LASTEXITCODE -ne 0) { |
| 129 | + Write-Warning-Log "OpenAI plugin build failed, but continuing..." |
| 130 | + } |
| 131 | + Pop-Location |
| 132 | + |
| 133 | + # Regenerate OpenAI SDK code |
| 134 | + Write-Log "Regenerating OpenAI SDK code" |
| 135 | + Push-Location "." |
| 136 | + try { |
| 137 | + pwsh scripts/Invoke-CodeGen.ps1 |
| 138 | + } catch { |
| 139 | + Write-Warning-Log "OpenAI code generation failed: $_" |
| 140 | + } |
| 141 | + Pop-Location |
| 142 | + |
| 143 | + # Check if there are changes to commit |
| 144 | + $gitStatus = git status --porcelain |
| 145 | + if (-not $gitStatus) { |
| 146 | + Write-Log "No changes detected. Skipping commit and PR creation." |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + # Configure git |
| 151 | + git config --local user.email "[email protected]" |
| 152 | + git config --local user.name "GitHub Action" |
| 153 | + |
| 154 | + # Add and commit changes |
| 155 | + Write-Log "Adding and committing changes" |
| 156 | + git add codegen/package.json |
| 157 | + git add codegen/generator/src/OpenAI.Library.Plugin.csproj |
| 158 | + git add package-lock.json |
| 159 | + git add ./ # Add any generated code changes |
| 160 | + |
| 161 | + $commitMessage = @" |
| 162 | +Update @typespec/http-client-csharp to $PackageVersion |
| 163 | +
|
| 164 | +- Updated @typespec/http-client-csharp from $currentVersion to $PackageVersion |
| 165 | +- Updated Microsoft.TypeSpec.Generator.ClientModel from $currentVersion to $PackageVersion |
| 166 | +- Regenerated OpenAI SDK code with new generator version |
| 167 | +- Updated centrally managed package-lock.json file with new dependency versions |
| 168 | +"@ |
| 169 | + |
| 170 | + git commit -m $commitMessage |
| 171 | + if ($LASTEXITCODE -ne 0) { |
| 172 | + throw "Failed to commit changes" |
| 173 | + } |
| 174 | + |
| 175 | + # Push the branch |
| 176 | + Write-Log "Pushing branch to remote" |
| 177 | + git push origin $PRBranch |
| 178 | + if ($LASTEXITCODE -ne 0) { |
| 179 | + throw "Failed to push branch" |
| 180 | + } |
| 181 | + |
| 182 | + # Create PR using GitHub CLI |
| 183 | + Write-Log "Creating PR using GitHub CLI" |
| 184 | + $env:GH_TOKEN = $AuthToken |
| 185 | + |
| 186 | + $prTitle = "Update @typespec/http-client-csharp to $PackageVersion" |
| 187 | + $prBody = @" |
| 188 | +This PR automatically updates the TypeSpec HTTP client C# generator version and regenerates the SDK code. |
| 189 | +
|
| 190 | +## Changes |
| 191 | +- Updated ``@typespec/http-client-csharp`` from ``$currentVersion`` to ``$PackageVersion`` |
| 192 | +- Updated ``Microsoft.TypeSpec.Generator.ClientModel`` from ``$currentVersion`` to ``$PackageVersion`` |
| 193 | +- Updated OpenAI plugin package.json file |
| 194 | +- Updated OpenAI plugin csproj file |
| 195 | +- Regenerated OpenAI SDK code using the new generator version |
| 196 | +- Updated centrally managed package-lock.json file with new dependency versions |
| 197 | +
|
| 198 | +## Details |
| 199 | +- Generator package: [@typespec/http-client-csharp](https://www.npmjs.com/package/@typespec/http-client-csharp) |
| 200 | +- Version update: ``$currentVersion`` → ``$PackageVersion`` |
| 201 | +
|
| 202 | +## Testing |
| 203 | +Please run the existing test suites to ensure the generated code works correctly: |
| 204 | +- Build and test the OpenAI SDK |
| 205 | +- Verify API compatibility and functionality |
| 206 | +
|
| 207 | +## Notes |
| 208 | +This 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. |
| 209 | +
|
| 210 | +If 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. |
| 211 | +"@ |
| 212 | + |
| 213 | + $prUrl = gh pr create --title $prTitle --body $prBody --base $BaseBranch --head $PRBranch 2>&1 |
| 214 | + |
| 215 | + if ($LASTEXITCODE -ne 0) { |
| 216 | + throw "Failed to create PR using gh CLI: $prUrl" |
| 217 | + } |
| 218 | + |
| 219 | + Write-Log "Successfully created PR: $prUrl" |
| 220 | + |
| 221 | +} catch { |
| 222 | + Write-Error-Log "Error creating PR: $_" |
| 223 | + exit 1 |
| 224 | +} finally { |
| 225 | + Pop-Location |
| 226 | +} |
0 commit comments