-
Notifications
You must be signed in to change notification settings - Fork 21
149 lines (123 loc) · 5.84 KB
/
syncbot.yml
File metadata and controls
149 lines (123 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Branch sync
on:
pull_request_target:
branches:
- main
- ootb
permissions:
contents: write
pull-requests: write
jobs:
mirror-changes:
runs-on: ubuntu-latest
if: github.actor != 'dependabot[bot]'
steps:
- uses: actions/checkout@v6
- name: Get target branch
id: target-branch
run: |
if [ "${{ github.base_ref }}" == "main" ]; then
echo "branch=ootb" >> $GITHUB_OUTPUT
else
echo "branch=main" >> $GITHUB_OUTPUT
fi
echo "Target branch for sync: $(cat $GITHUB_OUTPUT | grep branch | cut -d= -f2)"
- name: Cherry-pick commits to target branch
id: cherry-pick
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name syncbot
git config user.email github-actions@github.com
# Fetch all branches and the PR head (which may be from a fork)
git fetch origin
git fetch origin pull/${{ github.event.pull_request.number }}/head
# Get the source branch name and target branch
SOURCE_BRANCH="${{ github.head_ref }}"
TARGET_BRANCH="${{ steps.target-branch.outputs.branch }}"
SYNC_BRANCH="sync-${SOURCE_BRANCH}-to-${TARGET_BRANCH}"
echo "Source branch: $SOURCE_BRANCH"
echo "Target branch: $TARGET_BRANCH"
echo "Sync branch: $SYNC_BRANCH"
# Store branch names for next step
echo "source_branch=$SOURCE_BRANCH" >> $GITHUB_OUTPUT
echo "target_branch=$TARGET_BRANCH" >> $GITHUB_OUTPUT
echo "sync_branch=$SYNC_BRANCH" >> $GITHUB_OUTPUT
# Delete local sync branch if it exists
git branch -D $SYNC_BRANCH 2>/dev/null || true
# Create sync branch from target branch (will reset if remote exists)
git checkout -b $SYNC_BRANCH origin/$TARGET_BRANCH
# Get only the commits that are part of the PR
COMMITS=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits --jq '.[].sha')
# Cherry-pick each commit
echo "Cherry-picking commits..."
SKIPPED_COMMITS=""
for commit in $COMMITS; do
echo "Cherry-picking $commit"
git cherry-pick $commit || {
# If the cherry-pick is empty (change already exists on target), skip it
if git diff --cached --quiet; then
echo "Cherry-pick of $commit is empty (already applied), skipping"
git cherry-pick --skip
else
echo "Cherry-pick of $commit conflicted, skipping"
git cherry-pick --abort
SKIPPED_COMMITS="$SKIPPED_COMMITS $commit"
fi
}
done
if [ -n "$SKIPPED_COMMITS" ]; then
echo "skipped_commits=$SKIPPED_COMMITS" >> $GITHUB_OUTPUT
echo "Some commits were skipped due to conflicts:$SKIPPED_COMMITS"
fi
echo "Cherry-pick complete"
- name: Push sync branch and create PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SOURCE_BRANCH="${{ steps.cherry-pick.outputs.source_branch }}"
TARGET_BRANCH="${{ steps.cherry-pick.outputs.target_branch }}"
SYNC_BRANCH="${{ steps.cherry-pick.outputs.sync_branch }}"
SKIPPED="${{ steps.cherry-pick.outputs.skipped_commits }}"
# Push the sync branch
git push origin $SYNC_BRANCH --force
echo "Successfully pushed sync branch"
# Create PR using GitHub CLI
PR_TITLE="[Sync -> ${TARGET_BRANCH}] $(gh pr view ${{ github.event.pull_request.number }} --json title --jq .title)"
PR_BODY="Automated sync of PR #${{ github.event.pull_request.number }} from \`$SOURCE_BRANCH\` to \`$TARGET_BRANCH\`
Syncbot will not be offended if the sync isn't appropriate and you close this PR.
Original PR: #${{ github.event.pull_request.number }}"
if [ -n "$SKIPPED" ]; then
PR_BODY="$PR_BODY
**Note:** The following commits were skipped due to conflicts and may need manual syncing:
$(for sha in $SKIPPED; do echo "- \`$sha\`"; done)"
fi
# Check if PR already exists
EXISTING_PR=$(gh pr list --head $SYNC_BRANCH --base $TARGET_BRANCH --json number --jq '.[0].number' || echo "")
if [ -n "$EXISTING_PR" ]; then
echo "PR already exists: #$EXISTING_PR"
gh pr edit $EXISTING_PR --body "$PR_BODY"
else
NEW_PR=$(gh pr create --base $TARGET_BRANCH --head $SYNC_BRANCH --title "$PR_TITLE" --body "$PR_BODY" --label "sync")
echo "Created new PR: $NEW_PR"
fi
- name: Update PR comment on success
if: steps.cherry-pick.outputs.skipped_commits == ''
uses: quarkusio/action-helpers@main
with:
action: maintain-one-comment
github-token: ${{ secrets.GITHUB_TOKEN }}
body: |
🤖 This PR has been synchronized to the `${{ steps.cherry-pick.outputs.target_branch }}` branch. 🔄
pr-number: ${{ github.event.pull_request.number }}
- name: Update PR comment on partial sync
if: steps.cherry-pick.outputs.skipped_commits != ''
uses: quarkusio/action-helpers@main
with:
action: maintain-one-comment
github-token: ${{ secrets.GITHUB_TOKEN }}
body: |
🤖 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. 🔄
pr-number: ${{ github.event.pull_request.number }}