Skip to content

Commit 34828e2

Browse files
Saadnajmiclaude
andauthored
fix(ci): replace auto backport sync with /update-backports command (#2899)
## Summary - Removes the automatic `pull_request: synchronize` trigger from the backport workflow, which was failing on every fork PR due to missing secrets - Adds a new `/update-backports` comment command that maintainers can use to explicitly sync backport PRs with the source PR - The update job reacts to the comment, finds linked backport PRs, re-cherry-picks commits, and posts a summary comment with results ## Test plan - [ ] Push a commit to a fork PR — the "Update backport PRs" job should no longer appear - [ ] Comment `/backport <branch>` on a PR — the "Create backport" job should still work as before - [ ] Comment `/update-backports` on a PR with existing backport PRs — they should be re-synced - [ ] Comment `/update-backports` on a PR with no backport PRs — should get an informational comment 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 26c68ad commit 34828e2

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

.github/workflows/microsoft-backport.yml

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
name: Backport
22
# Creates backport PRs when someone comments "/backport <branch>" on a PR.
3-
# Also auto-updates existing backport PRs when the source PR is updated.
3+
# Updates existing backport PRs when someone comments "/update-backports" on a PR.
44

55
on:
66
issue_comment:
77
types: [created]
8-
pull_request:
9-
branches: [main]
10-
types: [synchronize]
118

129
permissions:
1310
contents: read
@@ -19,7 +16,8 @@ jobs:
1916
if: >
2017
github.event_name == 'issue_comment' &&
2118
github.event.issue.pull_request != '' &&
22-
startsWith(github.event.comment.body, '/backport ')
19+
startsWith(github.event.comment.body, '/backport ') &&
20+
contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)
2321
runs-on: ubuntu-latest
2422
permissions:
2523
contents: write
@@ -191,10 +189,14 @@ jobs:
191189
echo " 5. gh pr create --base <target-branch>"
192190
fi
193191
194-
# ─── Job 2: Auto-update backport PRs when source PR is updated ───
192+
# ─── Job 2: Update backport PRs via /update-backports comment ───
195193
update-backport:
196194
name: Update backport PRs
197-
if: github.event_name == 'pull_request' && github.event.action == 'synchronize'
195+
if: >
196+
github.event_name == 'issue_comment' &&
197+
github.event.issue.pull_request != '' &&
198+
github.event.comment.body == '/update-backports' &&
199+
contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)
198200
runs-on: ubuntu-latest
199201
permissions:
200202
contents: write
@@ -207,12 +209,31 @@ jobs:
207209
app-id: ${{ vars.APP_ID }}
208210
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
209211

212+
- name: React to comment
213+
env:
214+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
215+
run: |
216+
gh api \
217+
--method POST \
218+
repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
219+
-f content='+1'
220+
221+
- name: Get PR details
222+
id: pr
223+
env:
224+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
225+
PR_URL: ${{ github.event.issue.pull_request.url }}
226+
run: |
227+
PR_DATA=$(gh api "$PR_URL")
228+
echo "number=$(echo "$PR_DATA" | jq -r '.number')" >> "$GITHUB_OUTPUT"
229+
echo "head_branch=$(echo "$PR_DATA" | jq -r '.head.ref')" >> "$GITHUB_OUTPUT"
230+
210231
- name: Find linked backport PRs
211232
id: find-backports
212233
env:
213234
GH_TOKEN: ${{ steps.app-token.outputs.token }}
214-
HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
215-
PR_NUMBER: ${{ github.event.pull_request.number }}
235+
HEAD_BRANCH: ${{ steps.pr.outputs.head_branch }}
236+
PR_NUMBER: ${{ steps.pr.outputs.number }}
216237
REPO: ${{ github.repository }}
217238
run: |
218239
# Search for open PRs whose branch matches the pattern <version>/<head-branch>
@@ -226,6 +247,8 @@ jobs:
226247
if [ -z "$BACKPORT_PRS" ]; then
227248
echo "No backport PRs found for branch $HEAD_BRANCH"
228249
echo "found=false" >> "$GITHUB_OUTPUT"
250+
gh pr comment "$PR_NUMBER" --repo "$REPO" \
251+
--body ":information_source: No open backport PRs found for branch \`$HEAD_BRANCH\`."
229252
else
230253
echo "Found backport PRs:"
231254
echo "$BACKPORT_PRS"
@@ -251,12 +274,13 @@ jobs:
251274
if: steps.find-backports.outputs.found == 'true'
252275
env:
253276
GH_TOKEN: ${{ steps.app-token.outputs.token }}
254-
PR_NUMBER: ${{ github.event.pull_request.number }}
277+
PR_NUMBER: ${{ steps.pr.outputs.number }}
255278
REPO: ${{ github.repository }}
256279
run: |
257280
# Get current commits from the source PR
258281
COMMITS=$(gh api "repos/$REPO/pulls/$PR_NUMBER/commits" --jq '.[].sha')
259282
283+
RESULTS=""
260284
while IFS= read -r LINE; do
261285
BP_NUMBER=$(echo "$LINE" | awk '{print $1}')
262286
BP_BRANCH=$(echo "$LINE" | awk '{print $2}')
@@ -280,14 +304,16 @@ jobs:
280304
281305
if [ "$CHERRY_PICK_FAILED" = true ]; then
282306
echo "::warning::Cherry-pick failed while updating backport PR #$BP_NUMBER"
283-
gh pr comment "$PR_NUMBER" --repo "$REPO" \
284-
--body ":warning: Failed to auto-update backport PR #$BP_NUMBER to \`$BP_BASE\` due to conflicts. Manual update needed."
285-
gh pr comment "$BP_NUMBER" --repo "$REPO" \
286-
--body ":warning: Auto-update from source PR #$PR_NUMBER failed due to cherry-pick conflicts. Manual update needed."
307+
RESULTS="$RESULTS\n- :x: #$BP_NUMBER (\`$BP_BASE\`): cherry-pick conflicts (manual update needed)"
287308
else
288309
git push -f origin "$BP_BRANCH"
289310
echo "Successfully updated backport PR #$BP_NUMBER"
311+
RESULTS="$RESULTS\n- :white_check_mark: #$BP_NUMBER (\`$BP_BASE\`): updated"
290312
fi
291313
292314
echo "::endgroup::"
293315
done < /tmp/backport-prs.txt
316+
317+
# Post summary comment
318+
COMMENT_BODY="## Backport update results\n$RESULTS"
319+
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$(echo -e "$COMMENT_BODY")"

0 commit comments

Comments
 (0)