chore: auto-merge sync PR when no conflicts

This commit is contained in:
Kristjan Kruus 2026-03-24 14:05:01 +02:00
parent 44d96c3d4b
commit 49b1d054f3

View file

@ -18,26 +18,43 @@ jobs:
env: env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 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: | 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 # 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') EXISTING=$(gh pr list --repo ${{ github.repository }} --base my-custom-setup --head main --state open --json number,mergeable --jq '.[0]')
if [ -n "$EXISTING" ]; then PR_NUM=$(echo "$EXISTING" | jq -r '.number // empty')
echo "PR #$EXISTING already open — skipping."
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 else
# Check if main has commits not in my-custom-setup echo "PR #$PR_NUM already open."
BEHIND=$(gh api repos/${{ github.repository }}/compare/my-custom-setup...main --jq '.ahead_by') fi
if [ "$BEHIND" -gt 0 ] 2>/dev/null; then
gh pr create \ # Wait a moment for GitHub to compute mergeability
--repo ${{ github.repository }} \ sleep 10
--base my-custom-setup \
--head main \ # Check if the PR is mergeable (no conflicts)
--title "Sync upstream changes into my-custom-setup" \ MERGEABLE=$(gh pr view "$PR_NUM" --repo ${{ github.repository }} --json mergeable --jq '.mergeable')
--body "Automated PR to merge latest upstream changes from \`main\` into \`my-custom-setup\`." if [ "$MERGEABLE" = "MERGEABLE" ]; then
echo "PR created." gh pr merge "$PR_NUM" --repo ${{ github.repository }} --merge --auto
else echo "PR #$PR_NUM auto-merged."
echo "No new upstream commits — skipping PR." else
fi echo "PR #$PR_NUM has conflicts (status: $MERGEABLE) — leaving open for manual resolution."
fi fi
env: env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}