|
4 | 4 | workflow_dispatch: |
5 | 5 | schedule: |
6 | 6 | - cron: '0 1 * * 0' # Every Sunday at 1 AM UTC |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
7 | 11 |
|
8 | 12 | env: |
9 | 13 | TARGET_BRANCH: automatic-api-updates |
|
12 | 16 |
|
13 | 17 | jobs: |
14 | 18 | run: |
| 19 | + if: github.ref == 'refs/heads/main' |
15 | 20 | runs-on: windows-latest |
16 | 21 |
|
17 | 22 | steps: |
@@ -283,28 +288,35 @@ jobs: |
283 | 288 | # Configure git |
284 | 289 | git config --global user.name "github-actions[bot]" |
285 | 290 | git config --global user.email "github-actions[bot]@users.noreply.github.com" |
286 | | - |
287 | | - # Create and checkout new branch |
| 291 | +
|
| 292 | + # Create and checkout new branch (or switch if exists) |
288 | 293 | $branch_name = "${{ env.TARGET_BRANCH }}" |
289 | | - git checkout -b $branch_name |
290 | | - |
| 294 | + git checkout $branch_name 2>$null || git checkout -b $branch_name |
| 295 | +
|
291 | 296 | # Add all changes (including new files and directories) |
292 | 297 | git add -A |
293 | | - |
| 298 | +
|
294 | 299 | # Check if there are any changes to commit |
295 | 300 | $changes = git status --porcelain |
296 | 301 | if ($changes) { |
297 | 302 | Write-Host "Changes detected, committing..." |
298 | 303 | git commit -m "APIGenerator workflow results - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" |
299 | | - |
| 304 | +
|
300 | 305 | # Push the branch |
301 | 306 | git push origin $branch_name |
302 | 307 | Write-Host "Successfully pushed changes to branch: $branch_name" |
303 | | - |
304 | | - # Create pull request using GitHub CLI |
| 308 | +
|
| 309 | + # Check if a PR already exists for this branch |
| 310 | + $existingPr = gh pr list --head $branch_name --base main --json number --jq '.[0].number' |
| 311 | + if ($existingPr) { |
| 312 | + Write-Host "PR already exists (#$existingPr), adding a comment..." |
| 313 | + gh pr comment $existingPr --body "Automated API documentation updates pushed to branch '$branch_name' on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')." |
| 314 | + Write-Host "✓ Comment added to existing PR" |
| 315 | + } else { |
305 | 316 | Write-Host "Creating pull request..." |
306 | 317 | gh pr create --title "API Updates" --body "Automated API documentation updates generated by APIGenerator workflow on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" --head $branch_name --base main |
307 | 318 | Write-Host "✓ Pull request created successfully" |
| 319 | + } |
308 | 320 | } else { |
309 | 321 | Write-Host "No changes to commit" |
310 | 322 | } |
|
0 commit comments