Skip to content

Commit 1b0b494

Browse files
committed
Update schema generation to handle case where there is already a PR open
1 parent 122e046 commit 1b0b494

File tree

1 file changed

+69
-47
lines changed

1 file changed

+69
-47
lines changed

.github/workflows/schema-generation.yml

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,48 @@ on:
44
schedule:
55
- cron: '0 1 * * 6' # Saturday at 1 AM UTC
66
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
env:
13+
TARGET_BRANCH: schema-updates
14+
# This branch is used for automatic schema updates
15+
FALLBACK_BRANCH: develop
716

817
jobs:
918
generate-schemas:
1019
runs-on: windows-latest
1120

1221
steps:
13-
- name: Checkout develop branch
22+
- name: Resolve branch to use
23+
id: resolve_branch
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
TARGET_BRANCH: ${{ env.TARGET_BRANCH }}
27+
FALLBACK_BRANCH: ${{ env.FALLBACK_BRANCH }}
28+
run: |
29+
$REPO = "github.com/$env:GITHUB_REPOSITORY"
30+
$targetBranchExists = git ls-remote --heads "https://x-access-token:$env:GITHUB_TOKEN@$REPO" $env:TARGET_BRANCH
31+
if ($targetBranchExists) {
32+
"branch=$env:TARGET_BRANCH" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
33+
Write-Host "Using existing branch: $env:TARGET_BRANCH"
34+
} else {
35+
"branch=$env:FALLBACK_BRANCH" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
36+
Write-Host "Using fallback branch: $env:FALLBACK_BRANCH"
37+
}
38+
shell: powershell
39+
40+
- name: Checkout repository
1441
uses: actions/checkout@v4
1542
with:
16-
ref: develop
1743
token: ${{ secrets.GITHUB_TOKEN }}
44+
ref: ${{ steps.resolve_branch.outputs.branch }}
45+
fetch-depth: 0
46+
47+
- name: Show which branch was used
48+
run: "echo \"Using branch: ${{ steps.resolve_branch.outputs.branch }}\""
1849

1950
- name: Setup .NET
2051
uses: actions/setup-dotnet@v4
@@ -304,55 +335,46 @@ jobs:
304335
}
305336
shell: powershell
306337

307-
- name: Create Pull Request
338+
- name: Commit and push changes
308339
if: steps.check-changes.outputs.has_changes == 'true'
309340
run: |
310341
# Configure git
311-
git config --global user.name 'github-actions[bot]'
312-
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
313-
314-
# Create and switch to new branch
315-
$branchName = "schema-updates"
316-
git checkout -b $branchName
317-
318-
# Commit changes
342+
git config --global user.name "github-actions[bot]"
343+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
344+
345+
# Create and checkout target branch (or switch if exists)
346+
$branch_name = "${{ env.TARGET_BRANCH }}"
347+
git checkout $branch_name 2>$null || git checkout -b $branch_name
348+
349+
# Add all changes (including new files and directories)
319350
git add -A
320-
git commit -m "Automated schema generation update
321-
322-
Generated on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss UTC')
323-
324-
This PR contains automatically generated JSON schema updates based on the latest BHoM repositories."
325-
326-
# Push branch
327-
git push origin $branchName --force
328-
329-
# Create pull request using GitHub CLI
330-
$prTitle = "Automated Schema Updates - $(Get-Date -Format 'yyyy-MM-dd')"
331-
$prBody = @"
332-
## Automated Schema Generation
333-
334-
This pull request contains automatically generated JSON schema updates.
335-
336-
**Generation Details:**
337-
- Generated on: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss UTC')
338-
- Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
339-
340-
**Changes:**
341-
The schemas have been regenerated based on the latest versions of all BHoM repositories listed in ``Included-repositories.txt``.
342-
343-
Please review the changes before merging.
344-
"@
345-
346-
# Install GitHub CLI if not available
347-
if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
348-
Write-Host "Installing GitHub CLI..."
349-
winget install --id GitHub.cli
351+
352+
# Check if there are any changes to commit
353+
$changes = git status --porcelain
354+
if ($changes) {
355+
Write-Host "Changes detected, committing..."
356+
git commit -m "Automated schema generation update - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`n`nGenerated on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss UTC')`n`nThis commit contains automatically generated JSON schema updates based on the latest BHoM repositories."
357+
358+
# Push the branch
359+
git push origin $branch_name
360+
Write-Host "Successfully pushed changes to branch: $branch_name"
361+
362+
# Check if a PR already exists for this branch
363+
$existingPr = gh pr list --head $branch_name --base develop --json number --jq '.[0].number'
364+
if ($existingPr) {
365+
Write-Host "PR already exists (#$existingPr), adding a comment..."
366+
gh pr comment $existingPr --body "Automated schema generation updates pushed to branch '$branch_name' on $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')."
367+
Write-Host "✓ Comment added to existing PR"
368+
} else {
369+
Write-Host "Creating pull request..."
370+
$prTitle = "Automated Schema Updates - $(Get-Date -Format 'yyyy-MM-dd')"
371+
$prBody = "## Automated Schema Generation`n`nThis pull request contains automatically generated JSON schema updates.`n`n**Generation Details:**`n- Generated on: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss UTC')`n- Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}`n`n**Changes:**`nThe schemas have been regenerated based on the latest versions of all BHoM repositories listed in \`\`Included-repositories.txt\`\`.`n`nPlease review the changes before merging."
372+
gh pr create --title "$prTitle" --body "$prBody" --head $branch_name --base develop
373+
Write-Host "✓ Pull request created successfully"
374+
}
375+
} else {
376+
Write-Host "No changes to commit"
350377
}
351-
352-
# Create PR
353-
$env:GH_TOKEN = "${{ secrets.GITHUB_TOKEN }}"
354-
gh pr create --title "$prTitle" --body "$prBody" --base develop --head $branchName
355-
356-
shell: powershell
378+
shell: pwsh
357379
env:
358380
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)