diff --git a/.github/workflows/sync-fork.yml b/.github/workflows/sync-fork.yml index 6493ba117..85dedb605 100644 --- a/.github/workflows/sync-fork.yml +++ b/.github/workflows/sync-fork.yml @@ -18,26 +18,43 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Create PR from main to my-custom-setup + - name: Create and auto-merge PR from main to my-custom-setup run: | + # Check if main has commits not in my-custom-setup + BEHIND=$(gh api repos/${{ github.repository }}/compare/my-custom-setup...main --jq '.ahead_by') + if [ "$BEHIND" -le 0 ] 2>/dev/null; then + echo "No new upstream commits — nothing to do." + exit 0 + fi + # Check if there's already an open PR from main to my-custom-setup - EXISTING=$(gh pr list --repo ${{ github.repository }} --base my-custom-setup --head main --state open --json number --jq '.[0].number') - if [ -n "$EXISTING" ]; then - echo "PR #$EXISTING already open — skipping." + EXISTING=$(gh pr list --repo ${{ github.repository }} --base my-custom-setup --head main --state open --json number,mergeable --jq '.[0]') + PR_NUM=$(echo "$EXISTING" | jq -r '.number // empty') + + if [ -z "$PR_NUM" ]; then + # Create the PR + PR_URL=$(gh pr create \ + --repo ${{ github.repository }} \ + --base my-custom-setup \ + --head main \ + --title "Sync upstream changes into my-custom-setup" \ + --body "Automated PR to merge latest upstream changes from \`main\` into \`my-custom-setup\`.") + PR_NUM=$(echo "$PR_URL" | grep -oP '\d+$') + echo "Created PR #$PR_NUM" else - # Check if main has commits not in my-custom-setup - BEHIND=$(gh api repos/${{ github.repository }}/compare/my-custom-setup...main --jq '.ahead_by') - if [ "$BEHIND" -gt 0 ] 2>/dev/null; then - gh pr create \ - --repo ${{ github.repository }} \ - --base my-custom-setup \ - --head main \ - --title "Sync upstream changes into my-custom-setup" \ - --body "Automated PR to merge latest upstream changes from \`main\` into \`my-custom-setup\`." - echo "PR created." - else - echo "No new upstream commits — skipping PR." - fi + echo "PR #$PR_NUM already open." + fi + + # Wait a moment for GitHub to compute mergeability + sleep 10 + + # Check if the PR is mergeable (no conflicts) + MERGEABLE=$(gh pr view "$PR_NUM" --repo ${{ github.repository }} --json mergeable --jq '.mergeable') + if [ "$MERGEABLE" = "MERGEABLE" ]; then + gh pr merge "$PR_NUM" --repo ${{ github.repository }} --merge --auto + echo "PR #$PR_NUM auto-merged." + else + echo "PR #$PR_NUM has conflicts (status: $MERGEABLE) — leaving open for manual resolution." fi env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}