Skip to content

Commit 4b53727

Browse files
authored
Add GitHub action to auto-update TypeSpec generator version (#597)
Add GitHub action to auto-update TypeSpec generator version
1 parent 9320b2a commit 4b53727

File tree

2 files changed

+301
-0
lines changed

2 files changed

+301
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Update TypeSpec Generator Version
2+
3+
on:
4+
schedule:
5+
# Run weekly on Mondays at 9 AM UTC
6+
- cron: '0 9 * * 1'
7+
workflow_dispatch:
8+
# Allow manual triggering
9+
repository_dispatch:
10+
# Allow triggering from TypeSpec repository on new releases
11+
types: [typespec-release]
12+
13+
jobs:
14+
update-generator:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
pull-requests: write
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
with:
24+
token: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: '22.x'
30+
31+
- name: Setup .NET
32+
uses: actions/setup-dotnet@v4
33+
with:
34+
dotnet-version: 8.x
35+
36+
- name: Check for generator updates
37+
id: check-updates
38+
run: |
39+
# Get current version from the OpenAI package.json file
40+
CURRENT_VERSION=$(node -p "require('.codegen/package.json').dependencies['@typespec/http-client-csharp']" 2>/dev/null || echo "unknown")
41+
42+
echo "Current OpenAI version: $CURRENT_VERSION"
43+
44+
# Get latest version from npm registry
45+
LATEST_VERSION=$(npm view @typespec/http-client-csharp version 2>/dev/null || echo "unknown")
46+
echo "Latest version from npm: $LATEST_VERSION"
47+
48+
# Validate we got valid versions
49+
if [ "$CURRENT_VERSION" = "unknown" ] || [ "$LATEST_VERSION" = "unknown" ]; then
50+
echo "Error: Failed to get version information"
51+
echo "Current: $CURRENT_VERSION, Latest: $LATEST_VERSION"
52+
exit 1
53+
fi
54+
55+
# Compare versions (simple string comparison for alpha versions)
56+
if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then
57+
echo "Update needed: $CURRENT_VERSION -> $LATEST_VERSION"
58+
echo "needs-update=true" >> $GITHUB_OUTPUT
59+
echo "current-version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
60+
echo "latest-version=$LATEST_VERSION" >> $GITHUB_OUTPUT
61+
else
62+
echo "No update needed - already at latest version: $CURRENT_VERSION"
63+
echo "needs-update=false" >> $GITHUB_OUTPUT
64+
fi
65+
66+
- name: Update generator version and create PR
67+
if: steps.check-updates.outputs.needs-update == 'true'
68+
run: |
69+
LATEST_VERSION="${{ steps.check-updates.outputs.latest-version }}"
70+
71+
# Use the PowerShell script to handle the entire update process (following TypeSpec pattern)
72+
pwsh ./scripts/Submit-GeneratorUpdatePr.ps1 \
73+
-PackageVersion "$LATEST_VERSION" \
74+
-AuthToken "${{ secrets.GITHUB_TOKEN }}" \
75+
-RepoPath "."
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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

Comments
 (0)