Skip to content

Commit 48f3d68

Browse files
Merge pull request #436 from holly-cummins/fix-syncbot-conflict-handling
Handle conflicting cherry-picks in syncbot, also reduce conflicts by not making every PR try to fully align the branches
2 parents fcd5ea1 + 208143e commit 48f3d68

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

.github/workflows/syncbot.yml

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
id: cherry-pick
2727
env:
2828
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2930
run: |
3031
git config user.name syncbot
3132
git config user.email github-actions@github.com
@@ -36,7 +37,6 @@ jobs:
3637
3738
# Get the source branch name and target branch
3839
SOURCE_BRANCH="${{ github.head_ref }}"
39-
SOURCE_SHA="${{ github.event.pull_request.head.sha }}"
4040
TARGET_BRANCH="${{ steps.target-branch.outputs.branch }}"
4141
SYNC_BRANCH="sync-${SOURCE_BRANCH}-to-${TARGET_BRANCH}"
4242
@@ -55,11 +55,12 @@ jobs:
5555
# Create sync branch from target branch (will reset if remote exists)
5656
git checkout -b $SYNC_BRANCH origin/$TARGET_BRANCH
5757
58-
# Get all commits from the PR
59-
COMMITS=$(git log --reverse --format="%H" origin/$TARGET_BRANCH..$SOURCE_SHA)
58+
# Get only the commits that are part of the PR
59+
COMMITS=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits --jq '.[].sha')
6060
6161
# Cherry-pick each commit
6262
echo "Cherry-picking commits..."
63+
SKIPPED_COMMITS=""
6364
for commit in $COMMITS; do
6465
echo "Cherry-picking $commit"
6566
git cherry-pick $commit || {
@@ -68,41 +69,52 @@ jobs:
6869
echo "Cherry-pick of $commit is empty (already applied), skipping"
6970
git cherry-pick --skip
7071
else
71-
echo "Cherry-pick failed for $commit"
72+
echo "Cherry-pick of $commit conflicted, skipping"
7273
git cherry-pick --abort
73-
echo "cherry_pick_failed=true" >> $GITHUB_OUTPUT
74-
exit 1
74+
SKIPPED_COMMITS="$SKIPPED_COMMITS $commit"
7575
fi
7676
}
7777
done
78-
79-
echo "Successfully cherry-picked all commits"
78+
79+
if [ -n "$SKIPPED_COMMITS" ]; then
80+
echo "skipped_commits=$SKIPPED_COMMITS" >> $GITHUB_OUTPUT
81+
echo "Some commits were skipped due to conflicts:$SKIPPED_COMMITS"
82+
fi
83+
84+
echo "Cherry-pick complete"
8085
8186
- name: Push sync branch and create PR
82-
if: steps.cherry-pick.outputs.cherry_pick_failed != 'true'
8387
env:
8488
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8589
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8690
run: |
8791
SOURCE_BRANCH="${{ steps.cherry-pick.outputs.source_branch }}"
8892
TARGET_BRANCH="${{ steps.cherry-pick.outputs.target_branch }}"
8993
SYNC_BRANCH="${{ steps.cherry-pick.outputs.sync_branch }}"
90-
94+
SKIPPED="${{ steps.cherry-pick.outputs.skipped_commits }}"
95+
9196
# Push the sync branch
9297
git push origin $SYNC_BRANCH --force
9398
echo "Successfully pushed sync branch"
94-
99+
95100
# Create PR using GitHub CLI
96101
PR_TITLE="[Sync] $(gh pr view ${{ github.event.pull_request.number }} --json title --jq .title)"
97102
PR_BODY="Automated sync of PR #${{ github.event.pull_request.number }} from \`$SOURCE_BRANCH\` to \`$TARGET_BRANCH\`
98-
99-
Syncbot will not be offended sync isn't appropriate and you close this PR.
100-
103+
104+
Syncbot will not be offended if the sync isn't appropriate and you close this PR.
105+
101106
Original PR: #${{ github.event.pull_request.number }}"
102-
107+
108+
if [ -n "$SKIPPED" ]; then
109+
PR_BODY="$PR_BODY
110+
111+
**Note:** The following commits were skipped due to conflicts and may need manual syncing:
112+
$(for sha in $SKIPPED; do echo "- \`$sha\`"; done)"
113+
fi
114+
103115
# Check if PR already exists
104116
EXISTING_PR=$(gh pr list --head $SYNC_BRANCH --base $TARGET_BRANCH --json number --jq '.[0].number' || echo "")
105-
117+
106118
if [ -n "$EXISTING_PR" ]; then
107119
echo "PR already exists: #$EXISTING_PR"
108120
gh pr edit $EXISTING_PR --body "$PR_BODY"
@@ -111,8 +123,8 @@ jobs:
111123
echo "Created new PR: $NEW_PR"
112124
fi
113125
114-
- name: Update PR comment with matched PR
115-
if: steps.cherry-pick.outputs.cherry_pick_failed != 'true'
126+
- name: Update PR comment on success
127+
if: steps.cherry-pick.outputs.skipped_commits == ''
116128
uses: quarkusio/action-helpers@main
117129
with:
118130
action: maintain-one-comment
@@ -121,12 +133,12 @@ jobs:
121133
🤖 This PR has been synchronized to the `${{ steps.cherry-pick.outputs.target_branch }}` branch. 🔄
122134
pr-number: ${{ github.event.pull_request.number }}
123135

124-
- name: Update PR comment with matched PR
125-
if: steps.cherry-pick.outputs.cherry_pick_failed == 'true'
136+
- name: Update PR comment on partial sync
137+
if: steps.cherry-pick.outputs.skipped_commits != ''
126138
uses: quarkusio/action-helpers@main
127139
with:
128140
action: maintain-one-comment
129141
github-token: ${{ secrets.GITHUB_TOKEN }}
130142
body: |
131-
🤖 It was too hard to synch this change to the `${{ steps.cherry-pick.outputs.target_branch }}` branch. 😢
143+
🤖 This PR has been partially synchronized to the `${{ steps.cherry-pick.outputs.target_branch }}` branch. Some commits were skipped due to conflicts and may need manual syncing. 🔄
132144
pr-number: ${{ github.event.pull_request.number }}

0 commit comments

Comments
 (0)