Skip to content

Commit e9a50c4

Browse files
authored
Try a new approach to sync-content-to-next.yml for merged PRs
1 parent 893bf44 commit e9a50c4

File tree

1 file changed

+105
-13
lines changed

1 file changed

+105
-13
lines changed

.github/workflows/sync-content-to-next.yml

+105-13
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ jobs:
138138
# Push branch
139139
git push origin next-port-pr${{ github.event.pull_request.number }}
140140
141-
# Create PR
141+
# Create PR with a special tag in the body to identify it later
142142
gh pr create --base next --head next-port-pr${{ github.event.pull_request.number }} \
143143
--title "[Port to next] ${{ github.event.pull_request.title }}" \
144-
--body "Automatic port of PR #${{ github.event.pull_request.number }} to next branch.\n\nOriginal PR: #${{ github.event.pull_request.number }}\nCreated automatically after adding the 'temp - port to docs-next' label."
144+
--body "Automatic port of PR #${{ github.event.pull_request.number }} to next branch.\n\nOriginal PR: #${{ github.event.pull_request.number }}\nCreated automatically after adding the 'temp - port to docs-next' label.\n\n<!-- SYNC_PR_MARKER -->"
145145
env:
146146
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
147147

148-
# Job for syncing merged PRs
148+
# Job for syncing merged PRs more intelligently
149149
sync-merged-pr:
150150
if: >-
151151
github.event_name == 'pull_request' &&
@@ -166,34 +166,126 @@ jobs:
166166
git config user.name "GitHub Actions Bot"
167167
git config user.email "[email protected]"
168168
169-
- name: Cherry-pick and create PR
169+
- name: Check for existing PR and decide strategy
170+
id: check-existing
171+
run: |
172+
PR_NUMBER="${{ github.event.pull_request.number }}"
173+
174+
# Check if there's an existing PR
175+
EXISTING_PR_NUMBER=$(gh pr list --base next --head "next-port-pr$PR_NUMBER" --state open --json number --jq '.[0].number')
176+
177+
if [ -n "$EXISTING_PR_NUMBER" ]; then
178+
echo "Existing PR found: #$EXISTING_PR_NUMBER"
179+
180+
# Check if the PR body contains our marker
181+
PR_BODY=$(gh pr view $EXISTING_PR_NUMBER --json body --jq '.body')
182+
183+
if [[ "$PR_BODY" == *"SYNC_PR_MARKER"* ]]; then
184+
echo "This is a PR created by our workflow, checking if it's been modified"
185+
186+
# Check if there are review comments or the PR has been approved
187+
REVIEW_COUNT=$(gh pr view $EXISTING_PR_NUMBER --json reviews --jq '.reviews | length')
188+
189+
if [ "$REVIEW_COUNT" -gt 0 ]; then
190+
echo "PR has reviews, will update the existing PR instead of replacing it"
191+
echo "strategy=update" >> $GITHUB_OUTPUT
192+
echo "existing_pr=$EXISTING_PR_NUMBER" >> $GITHUB_OUTPUT
193+
exit 0
194+
fi
195+
fi
196+
197+
echo "Will replace the existing PR with a new one based on the merge commit"
198+
echo "strategy=replace" >> $GITHUB_OUTPUT
199+
echo "existing_pr=$EXISTING_PR_NUMBER" >> $GITHUB_OUTPUT
200+
else
201+
echo "No existing PR found, will create a new one"
202+
echo "strategy=create" >> $GITHUB_OUTPUT
203+
fi
204+
env:
205+
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
206+
207+
- name: Create new branch from merge commit
208+
id: create-branch
170209
run: |
171210
# Variables
172211
PR_NUMBER="${{ github.event.pull_request.number }}"
173212
PR_TITLE="${{ github.event.pull_request.title }}"
174213
MERGE_COMMIT="${{ github.event.pull_request.merge_commit_sha }}"
175-
BRANCH_NAME="sync-merged-pr$PR_NUMBER"
214+
215+
# Use different branch name depending on strategy
216+
if [ "${{ steps.check-existing.outputs.strategy }}" = "update" ]; then
217+
BRANCH_NAME="next-port-pr$PR_NUMBER"
218+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
219+
else
220+
BRANCH_NAME="sync-merged-pr$PR_NUMBER"
221+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
222+
fi
176223
177224
# Create branch from next
178225
git fetch origin next
179-
git checkout -b $BRANCH_NAME origin/next
226+
227+
if [ "${{ steps.check-existing.outputs.strategy }}" = "update" ]; then
228+
echo "Checking out existing branch $BRANCH_NAME"
229+
git checkout $BRANCH_NAME || git checkout -b $BRANCH_NAME origin/next
230+
else
231+
echo "Creating new branch $BRANCH_NAME"
232+
git checkout -b $BRANCH_NAME origin/next
233+
fi
180234
181235
# Try to cherry-pick
182236
if git cherry-pick -m 1 $MERGE_COMMIT; then
183-
git push origin $BRANCH_NAME
184-
185-
# Create PR
186-
gh pr create --base next --head $BRANCH_NAME \
187-
--title "[Merged-sync] $PR_TITLE" \
188-
--body "Synchronization of merged PR #$PR_NUMBER to next branch"
237+
git push origin $BRANCH_NAME --force
238+
echo "cherry_pick_success=true" >> $GITHUB_OUTPUT
189239
else
190240
echo "Cherry-pick failed, manual intervention needed"
191241
git cherry-pick --abort
192-
exit 1
242+
echo "cherry_pick_success=false" >> $GITHUB_OUTPUT
193243
fi
194244
env:
195245
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
196246

247+
- name: Update existing PR
248+
if: steps.check-existing.outputs.strategy == 'update' && steps.create-branch.outputs.cherry_pick_success == 'true'
249+
run: |
250+
EXISTING_PR="${{ steps.check-existing.outputs.existing_pr }}"
251+
echo "Updating existing PR #$EXISTING_PR with latest merged changes"
252+
253+
# Add comment explaining the update
254+
gh pr comment $EXISTING_PR --body "This PR has been updated with the latest changes from the merged PR #${{ github.event.pull_request.number }}. The merge commit was cherry-picked to include all approved changes."
255+
env:
256+
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
257+
258+
- name: Close and replace existing PR
259+
if: steps.check-existing.outputs.strategy == 'replace' && steps.create-branch.outputs.cherry_pick_success == 'true'
260+
run: |
261+
EXISTING_PR="${{ steps.check-existing.outputs.existing_pr }}"
262+
BRANCH_NAME="${{ steps.create-branch.outputs.branch_name }}"
263+
PR_TITLE="${{ github.event.pull_request.title }}"
264+
265+
# Create the new PR first
266+
NEW_PR=$(gh pr create --base next --head $BRANCH_NAME \
267+
--title "[Merged-sync] $PR_TITLE" \
268+
--body "Synchronization of merged PR #${{ github.event.pull_request.number }} to next branch. This PR contains the final state of the changes as they were merged to main." \
269+
--json number --jq '.number')
270+
271+
# Now close the existing PR with a reference to the new one
272+
gh pr close $EXISTING_PR --comment "This PR is replaced by the direct synchronization of the merge commit in #$NEW_PR"
273+
env:
274+
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
275+
276+
- name: Create new PR
277+
if: steps.check-existing.outputs.strategy == 'create' && steps.create-branch.outputs.cherry_pick_success == 'true'
278+
run: |
279+
BRANCH_NAME="${{ steps.create-branch.outputs.branch_name }}"
280+
PR_TITLE="${{ github.event.pull_request.title }}"
281+
282+
# Create new PR
283+
gh pr create --base next --head $BRANCH_NAME \
284+
--title "[Merged-sync] $PR_TITLE" \
285+
--body "Synchronization of merged PR #${{ github.event.pull_request.number }} to next branch. This PR contains the final state of the changes as they were merged to main."
286+
env:
287+
GITHUB_TOKEN: ${{ secrets.SYNC_MAIN_TO_NEXT }}
288+
197289
# Job for automatic syncing of pushed commits
198290
cherry-pick-from-push:
199291
if: github.event_name == 'push'

0 commit comments

Comments
 (0)