forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 167
319 lines (280 loc) · 12.1 KB
/
microsoft-backport.yml
File metadata and controls
319 lines (280 loc) · 12.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: Backport
# Creates backport PRs when someone comments "/backport <branch>" on a PR.
# Updates existing backport PRs when someone comments "/update-backports" on a PR.
on:
issue_comment:
types: [created]
permissions:
contents: read
jobs:
# ─── Job 1: Create backport PR(s) from a /backport comment ───
backport:
name: Create backport
if: >
github.event_name == 'issue_comment' &&
github.event.issue.pull_request != '' &&
startsWith(github.event.comment.body, '/backport ') &&
contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Generate GitHub App token
uses: actions/create-github-app-token@v2
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
- name: React to comment
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
gh api \
--method POST \
repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='+1'
- name: Parse target branches
id: parse
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
# Extract everything after "/backport " and split into branch names
BRANCHES=$(echo "$COMMENT_BODY" | head -1 | sed 's|^/backport ||' | xargs)
if [ -z "$BRANCHES" ]; then
echo "::error::No target branches specified"
exit 1
fi
echo "branches=$BRANCHES" >> "$GITHUB_OUTPUT"
- name: Get PR details
id: pr
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR_URL: ${{ github.event.issue.pull_request.url }}
run: |
PR_DATA=$(gh api "$PR_URL")
echo "number=$(echo "$PR_DATA" | jq -r '.number')" >> "$GITHUB_OUTPUT"
echo "title=$(echo "$PR_DATA" | jq -r '.title')" >> "$GITHUB_OUTPUT"
echo "head_branch=$(echo "$PR_DATA" | jq -r '.head.ref')" >> "$GITHUB_OUTPUT"
echo "merged=$(echo "$PR_DATA" | jq -r '.merged')" >> "$GITHUB_OUTPUT"
echo "state=$(echo "$PR_DATA" | jq -r '.state')" >> "$GITHUB_OUTPUT"
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Create backport PRs
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
BRANCHES: ${{ steps.parse.outputs.branches }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
PR_TITLE: ${{ steps.pr.outputs.title }}
HEAD_BRANCH: ${{ steps.pr.outputs.head_branch }}
PR_MERGED: ${{ steps.pr.outputs.merged }}
REPO: ${{ github.repository }}
run: |
RESULTS=""
ANY_FAILED=false
# Get commits from the PR
COMMITS=$(gh api "repos/$REPO/pulls/$PR_NUMBER/commits" --jq '.[].sha')
if [ -z "$COMMITS" ]; then
echo "::error::No commits found in PR #$PR_NUMBER"
exit 1
fi
for TARGET_BRANCH in $BRANCHES; do
echo "::group::Backporting to $TARGET_BRANCH"
# Extract version from branch name (e.g., 0.81-stable -> 0.81)
VERSION=$(echo "$TARGET_BRANCH" | sed 's/-stable$//')
BACKPORT_BRANCH="$VERSION/$HEAD_BRANCH"
# Transform PR title
if echo "$PR_TITLE" | grep -qP '^\w+\([^)]+\):'; then
# Has scope: type(scope): desc -> type(version, scope): desc
NEW_TITLE=$(echo "$PR_TITLE" | sed -E "s/^(\w+)\(([^)]+)\):/\1($VERSION, \2):/")
else
# No scope: type: desc -> type(version): desc
NEW_TITLE=$(echo "$PR_TITLE" | sed -E "s/^(\w+):/\1($VERSION):/")
fi
# Check if target branch exists
if ! git ls-remote --exit-code --heads origin "$TARGET_BRANCH" > /dev/null 2>&1; then
echo "::warning::Target branch $TARGET_BRANCH does not exist, skipping"
RESULTS="$RESULTS\n- :warning: \`$TARGET_BRANCH\`: branch does not exist"
ANY_FAILED=true
echo "::endgroup::"
continue
fi
# Create backport branch
git checkout "origin/$TARGET_BRANCH"
git checkout -B "$BACKPORT_BRANCH"
# Cherry-pick commits
CHERRY_PICK_FAILED=false
for COMMIT in $COMMITS; do
if ! git cherry-pick "$COMMIT" --no-edit; then
CHERRY_PICK_FAILED=true
git cherry-pick --abort || true
break
fi
done
if [ "$CHERRY_PICK_FAILED" = true ]; then
echo "::warning::Cherry-pick failed for $TARGET_BRANCH"
RESULTS="$RESULTS\n- :x: \`$TARGET_BRANCH\`: cherry-pick conflicts (manual backport needed)"
ANY_FAILED=true
echo "::endgroup::"
continue
fi
# Push the backport branch
git push -f origin "$BACKPORT_BRANCH"
# Check if a backport PR already exists
EXISTING_PR=$(gh pr list --repo "$REPO" --head "$BACKPORT_BRANCH" --base "$TARGET_BRANCH" --json number --jq '.[0].number // empty')
if [ -n "$EXISTING_PR" ]; then
echo "Backport PR #$EXISTING_PR already exists, updated via force-push"
RESULTS="$RESULTS\n- :arrows_counterclockwise: \`$TARGET_BRANCH\`: updated existing PR #$EXISTING_PR"
else
# Create backport PR
BACKPORT_PR_URL=$(gh pr create \
--repo "$REPO" \
--base "$TARGET_BRANCH" \
--head "$BACKPORT_BRANCH" \
--title "$NEW_TITLE" \
--body "$(cat <<EOF
## Summary
Backport of #$PR_NUMBER to \`$TARGET_BRANCH\`.
## Test Plan
Same as #$PR_NUMBER.
EOF
)")
echo "Created backport PR: $BACKPORT_PR_URL"
BACKPORT_PR_NUMBER=$(echo "$BACKPORT_PR_URL" | grep -oP '\d+$')
RESULTS="$RESULTS\n- :white_check_mark: \`$TARGET_BRANCH\`: #$BACKPORT_PR_NUMBER"
fi
echo "::endgroup::"
done
# Post summary comment on the original PR
COMMENT_BODY="## Backport results\n$RESULTS"
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$(echo -e "$COMMENT_BODY")"
if [ "$ANY_FAILED" = true ]; then
echo ""
echo "Some backports failed. To backport manually:"
echo " 1. git fetch origin <target-branch>"
echo " 2. git checkout -b <version>/<branch> origin/<target-branch>"
echo " 3. git cherry-pick <commits>"
echo " 4. git push -u origin <version>/<branch>"
echo " 5. gh pr create --base <target-branch>"
fi
# ─── Job 2: Update backport PRs via /update-backports comment ───
update-backport:
name: Update backport PRs
if: >
github.event_name == 'issue_comment' &&
github.event.issue.pull_request != '' &&
github.event.comment.body == '/update-backports' &&
contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Generate GitHub App token
uses: actions/create-github-app-token@v2
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
- name: React to comment
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
gh api \
--method POST \
repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions \
-f content='+1'
- name: Get PR details
id: pr
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR_URL: ${{ github.event.issue.pull_request.url }}
run: |
PR_DATA=$(gh api "$PR_URL")
echo "number=$(echo "$PR_DATA" | jq -r '.number')" >> "$GITHUB_OUTPUT"
echo "head_branch=$(echo "$PR_DATA" | jq -r '.head.ref')" >> "$GITHUB_OUTPUT"
- name: Find linked backport PRs
id: find-backports
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
HEAD_BRANCH: ${{ steps.pr.outputs.head_branch }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
REPO: ${{ github.repository }}
run: |
# Search for open PRs whose branch matches the pattern <version>/<head-branch>
# e.g., if head_branch is "fix-focus", look for "*/fix-focus"
BACKPORT_PRS=$(gh pr list \
--repo "$REPO" \
--state open \
--json number,headRefName,baseRefName \
--jq ".[] | select(.headRefName | test(\"^[0-9]+\\\\.[0-9]+/$HEAD_BRANCH\$\")) | \"\(.number) \(.headRefName) \(.baseRefName)\"")
if [ -z "$BACKPORT_PRS" ]; then
echo "No backport PRs found for branch $HEAD_BRANCH"
echo "found=false" >> "$GITHUB_OUTPUT"
gh pr comment "$PR_NUMBER" --repo "$REPO" \
--body ":information_source: No open backport PRs found for branch \`$HEAD_BRANCH\`."
else
echo "Found backport PRs:"
echo "$BACKPORT_PRS"
echo "found=true" >> "$GITHUB_OUTPUT"
# Write to a file to handle multiline
echo "$BACKPORT_PRS" > /tmp/backport-prs.txt
fi
- name: Checkout
if: steps.find-backports.outputs.found == 'true'
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0
- name: Configure git
if: steps.find-backports.outputs.found == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Update backport PRs
if: steps.find-backports.outputs.found == 'true'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
REPO: ${{ github.repository }}
run: |
# Get current commits from the source PR
COMMITS=$(gh api "repos/$REPO/pulls/$PR_NUMBER/commits" --jq '.[].sha')
RESULTS=""
while IFS= read -r LINE; do
BP_NUMBER=$(echo "$LINE" | awk '{print $1}')
BP_BRANCH=$(echo "$LINE" | awk '{print $2}')
BP_BASE=$(echo "$LINE" | awk '{print $3}')
echo "::group::Updating backport PR #$BP_NUMBER ($BP_BRANCH -> $BP_BASE)"
# Reset the backport branch to the target stable branch
git checkout "origin/$BP_BASE"
git checkout -B "$BP_BRANCH"
# Re-cherry-pick all commits
CHERRY_PICK_FAILED=false
for COMMIT in $COMMITS; do
if ! git cherry-pick "$COMMIT" --no-edit; then
CHERRY_PICK_FAILED=true
git cherry-pick --abort || true
break
fi
done
if [ "$CHERRY_PICK_FAILED" = true ]; then
echo "::warning::Cherry-pick failed while updating backport PR #$BP_NUMBER"
RESULTS="$RESULTS\n- :x: #$BP_NUMBER (\`$BP_BASE\`): cherry-pick conflicts (manual update needed)"
else
git push -f origin "$BP_BRANCH"
echo "Successfully updated backport PR #$BP_NUMBER"
RESULTS="$RESULTS\n- :white_check_mark: #$BP_NUMBER (\`$BP_BASE\`): updated"
fi
echo "::endgroup::"
done < /tmp/backport-prs.txt
# Post summary comment
COMMENT_BODY="## Backport update results\n$RESULTS"
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$(echo -e "$COMMENT_BODY")"