Skip to content

Commit ef23cc7

Browse files
committed
Improve API generation workflow PR handling
Adds explicit permissions for contents and pull-requests, restricts job to main branch, and enhances branch/PR logic to comment on existing PRs or create new ones as needed. This streamlines automated API documentation updates and prevents duplicate pull requests.
1 parent 5e011ec commit ef23cc7

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

.github/workflows/generation.yml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
workflow_dispatch:
55
schedule:
66
- cron: '0 1 * * 0' # Every Sunday at 1 AM UTC
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
711

812
env:
913
TARGET_BRANCH: automatic-api-updates
@@ -12,6 +16,7 @@ env:
1216

1317
jobs:
1418
run:
19+
if: github.ref == 'refs/heads/main'
1520
runs-on: windows-latest
1621

1722
steps:
@@ -283,28 +288,35 @@ jobs:
283288
# Configure git
284289
git config --global user.name "github-actions[bot]"
285290
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)
288293
$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+
291296
# Add all changes (including new files and directories)
292297
git add -A
293-
298+
294299
# Check if there are any changes to commit
295300
$changes = git status --porcelain
296301
if ($changes) {
297302
Write-Host "Changes detected, committing..."
298303
git commit -m "APIGenerator workflow results - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
299-
304+
300305
# Push the branch
301306
git push origin $branch_name
302307
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 {
305316
Write-Host "Creating pull request..."
306317
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
307318
Write-Host "✓ Pull request created successfully"
319+
}
308320
} else {
309321
Write-Host "No changes to commit"
310322
}

0 commit comments

Comments
 (0)