-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbackport-pr.sh
More file actions
executable file
·352 lines (309 loc) · 11.2 KB
/
Copy pathbackport-pr.sh
File metadata and controls
executable file
·352 lines (309 loc) · 11.2 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
set -euo pipefail
# --- Colors & helpers --------------------------------------------------------
if [ -t 1 ]; then
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; RESET='\033[0m'
else
RED=''; GREEN=''; YELLOW=''; CYAN=''; BOLD=''; RESET=''
fi
info() { echo -e "${GREEN}✓${RESET} $*"; }
warn() { echo -e "${YELLOW}⚠${RESET} $*"; }
error() { echo -e "${RED}✗${RESET} $*" >&2; }
step() { echo -e "${CYAN}→${RESET} ${BOLD}$*${RESET}"; }
# --- Cleanup trap ------------------------------------------------------------
CURRENT_BRANCH=""
BACKPORT_BRANCH=""
CHERRY_PICK_IN_PROGRESS=0
DRY_RUN=0
cleanup() {
local exit_code=$?
if [ $exit_code -ne 0 ]; then
if [ $CHERRY_PICK_IN_PROGRESS -eq 1 ]; then
echo ""
error "Cherry-pick failed — likely a conflict."
echo ""
echo -e " You have two options:"
echo ""
echo -e " ${BOLD}Option 1: Resolve manually${RESET}"
echo -e " 1. Fix the conflicts in the listed files"
echo -e " 2. ${CYAN}git add <resolved-files>${RESET}"
echo -e " 3. ${CYAN}git cherry-pick --continue${RESET}"
echo -e " 4. ${CYAN}git push -u origin $BACKPORT_BRANCH${RESET}"
echo -e " 5. Create the PR manually or re-run this script"
echo ""
echo -e " ${BOLD}Option 2: Abort and go back${RESET}"
echo -e " 1. ${CYAN}git cherry-pick --abort${RESET}"
echo -e " 2. ${CYAN}git checkout $CURRENT_BRANCH${RESET}"
echo -e " 3. ${CYAN}git branch -D $BACKPORT_BRANCH${RESET}"
echo ""
return
fi
# Generic failure — try to restore original branch
if [ -n "$CURRENT_BRANCH" ]; then
warn "Restoring original branch ($CURRENT_BRANCH)"
git checkout "$CURRENT_BRANCH" 2>/dev/null || true
fi
fi
}
trap cleanup EXIT
# --- Argument parsing --------------------------------------------------------
usage() {
echo -e "${BOLD}Usage:${RESET} $0 [--dry-run] [<release-name>] <pr-number-or-url>"
echo ""
echo " <release-name> e.g. 1.9._ (if omitted, you'll pick from a list)"
echo " <pr-number-or-url> PR number (420) or full URL (https://github.com/.../pull/420)"
echo " --dry-run Show what would happen without making changes"
exit 1
}
RELEASE_NAME=""
PR_INPUT=""
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=1 ;;
--help|-h) usage ;;
*)
if [ -z "$PR_INPUT" ] && [[ "$arg" =~ (^[0-9]+$|/pull/[0-9]+) ]]; then
PR_INPUT="$arg"
elif [ -z "$RELEASE_NAME" ]; then
RELEASE_NAME="$arg"
elif [ -z "$PR_INPUT" ]; then
PR_INPUT="$arg"
else
usage
fi
;;
esac
done
if [ -z "$PR_INPUT" ]; then
usage
fi
# Extract PR number from URL or plain number
if [[ "$PR_INPUT" =~ /pull/([0-9]+) ]]; then
PR_NUMBER="${BASH_REMATCH[1]}"
elif [[ "$PR_INPUT" =~ ^[0-9]+$ ]]; then
PR_NUMBER="$PR_INPUT"
else
error "Cannot parse PR number from: $PR_INPUT"
exit 1
fi
# --- Check requirements ------------------------------------------------------
step "Checking prerequisites"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
for cmd in gh jq; do
$cmd --version 1>/dev/null 2>&1 || { error "$cmd is not installed"; exit 1; }
done
info "gh and jq are available"
if [ -n "$(git status --porcelain)" ]; then
error "Working tree is not clean. Please stash or commit your changes first."
exit 1
fi
info "Working tree is clean"
git fetch --quiet
info "Fetched latest from origin"
# --- Release branch selection ------------------------------------------------
if [ -z "$RELEASE_NAME" ]; then
step "Select a release branch"
BRANCHES=()
while IFS= read -r ref; do
branch="${ref#refs/remotes/origin/release/}"
BRANCHES+=("$branch")
done < <(git for-each-ref --sort=-version:refname --format='%(refname)' 'refs/remotes/origin/release/*')
if [ ${#BRANCHES[@]} -eq 0 ]; then
error "No release branches found"
exit 1
fi
# Show the 10 most recent
SHOW_COUNT=10
if [ ${#BRANCHES[@]} -lt $SHOW_COUNT ]; then
SHOW_COUNT=${#BRANCHES[@]}
fi
echo ""
for i in $(seq 1 "$SHOW_COUNT"); do
echo -e " ${BOLD}$i)${RESET} release/${BRANCHES[$((i-1))]}"
done
echo ""
echo -n "Pick a branch [1-$SHOW_COUNT]: "
read -r PICK
if ! [[ "$PICK" =~ ^[0-9]+$ ]] || [ "$PICK" -lt 1 ] || [ "$PICK" -gt "$SHOW_COUNT" ]; then
error "Invalid selection"
exit 1
fi
RELEASE_NAME="${BRANCHES[$((PICK-1))]}"
fi
if [[ ! "$RELEASE_NAME" =~ ^[0-9]+\.[0-9]+\._ ]]; then
error "Release name should be in the format X.Y._ (e.g. 1.9._)"
exit 1
fi
RELEASE_BRANCH="release/$RELEASE_NAME"
git show-ref --verify --quiet "refs/remotes/origin/$RELEASE_BRANCH" 2>/dev/null || {
error "Branch $RELEASE_BRANCH does not exist on origin"
exit 1
}
info "Target branch: $RELEASE_BRANCH"
# --- Fetch PR details (single API call) --------------------------------------
step "Fetching PR #$PR_NUMBER details"
PR_DATA=$(gh pr view "$PR_NUMBER" --json commits,mergeCommit,title,labels,state)
PR_STATE=$(echo "$PR_DATA" | jq -r '.state')
if [ "$PR_STATE" == "null" ] || [ -z "$PR_STATE" ]; then
error "PR #$PR_NUMBER does not exist"
exit 1
fi
if [ "$PR_STATE" != "MERGED" ]; then
warn "PR #$PR_NUMBER is $PR_STATE (not merged). Proceed anyway? (y/n)"
read -r ANSWER
[ "$ANSWER" == "y" ] || { echo "Aborting."; exit 1; }
fi
PR_TITLE=$(echo "$PR_DATA" | jq -r '.title')
PR_LABELS=$(echo "$PR_DATA" | jq -r '[.labels[].name] | join(",")')
PR_COMMITS=$(echo "$PR_DATA" | jq -r '.commits[].oid')
PR_MERGE_COMMIT=$(echo "$PR_DATA" | jq -r '.mergeCommit.oid // empty')
info "PR: $PR_TITLE"
# --- Determine commits to cherry-pick ----------------------------------------
USE_MERGE_COMMIT=0
for PR_COMMIT in $PR_COMMITS; do
if ! git cat-file -e "$PR_COMMIT" 2>/dev/null; then
warn "Commit $PR_COMMIT is no longer present (garbage collected after squash)."
USE_MERGE_COMMIT=1
break
fi
done
if [ $USE_MERGE_COMMIT -eq 0 ]; then
for PR_COMMIT in $PR_COMMITS; do
PARENT_COUNT=$(git rev-list --parents -n 1 "$PR_COMMIT" 2>/dev/null | wc -w)
if [ "$PARENT_COUNT" -gt 2 ]; then
warn "PR contains a merge commit ($PR_COMMIT)."
USE_MERGE_COMMIT=1
break
fi
done
fi
if [ $USE_MERGE_COMMIT -eq 1 ]; then
if [ -z "$PR_MERGE_COMMIT" ]; then
error "Need merge commit but PR has not been merged yet."
exit 1
fi
echo -n "Cherry-pick the merge commit instead of individual commits? (y/n) "
read -r ANSWER
if [ "$ANSWER" == "y" ]; then
PR_COMMITS="$PR_MERGE_COMMIT"
else
echo "Aborting. Please backport manually."
exit 1
fi
fi
COMMIT_COUNT=$(echo "$PR_COMMITS" | wc -w | tr -d ' ')
info "Will cherry-pick $COMMIT_COUNT commit(s)"
# --- Handle existing backport branch -----------------------------------------
BACKPORT_BRANCH="$USER/backport-pr-$PR_NUMBER"
SKIP_CHERRY_PICK=0
EXISTING_REMOTE=0
EXISTING_LOCAL=0
EXISTING_PR=0
git show-ref --verify --quiet "refs/remotes/origin/$BACKPORT_BRANCH" 2>/dev/null && EXISTING_REMOTE=1
git show-ref --verify --quiet "refs/heads/$BACKPORT_BRANCH" 2>/dev/null && EXISTING_LOCAL=1
if [ $EXISTING_REMOTE -eq 1 ]; then
gh pr view "$BACKPORT_BRANCH" --json url 1>/dev/null 2>&1 && EXISTING_PR=1
fi
if [ $EXISTING_REMOTE -eq 1 ] && [ $EXISTING_PR -eq 1 ]; then
EXISTING_PR_URL=$(gh pr view "$BACKPORT_BRANCH" --json url --jq '.url')
error "A backport PR already exists: $EXISTING_PR_URL"
exit 1
fi
if [ $EXISTING_REMOTE -eq 1 ] && [ $EXISTING_PR -eq 0 ]; then
warn "Remote branch $BACKPORT_BRANCH exists but has no open PR."
echo -n " Create the PR from the existing branch? (y/n) "
read -r ANSWER
if [ "$ANSWER" == "y" ]; then
SKIP_CHERRY_PICK=1
info "Will reuse existing branch"
else
echo -n " Delete it and start fresh instead? (y/n) "
read -r ANSWER
if [ "$ANSWER" == "y" ]; then
if [ $DRY_RUN -eq 0 ]; then
git push origin --delete "$BACKPORT_BRANCH" 2>/dev/null || true
fi
info "Deleted remote branch $BACKPORT_BRANCH"
else
echo "Aborting."
exit 1
fi
fi
fi
if [ $SKIP_CHERRY_PICK -eq 0 ] && [ $EXISTING_LOCAL -eq 1 ]; then
warn "Local branch $BACKPORT_BRANCH already exists."
echo -n "Delete it and start fresh? (y/n) "
read -r ANSWER
if [ "$ANSWER" == "y" ]; then
if [ $DRY_RUN -eq 0 ]; then
git branch -D "$BACKPORT_BRANCH"
fi
info "Deleted local branch $BACKPORT_BRANCH"
else
echo "Aborting."
exit 1
fi
fi
# --- Dry-run summary ---------------------------------------------------------
if [ $DRY_RUN -eq 1 ]; then
echo ""
step "Dry-run summary (no changes will be made)"
echo ""
echo -e " PR: ${BOLD}#$PR_NUMBER${RESET} — $PR_TITLE"
echo -e " Target: ${BOLD}$RELEASE_BRANCH${RESET}"
echo -e " Branch: ${BOLD}$BACKPORT_BRANCH${RESET}"
echo -e " Commits: $COMMIT_COUNT"
for c in $PR_COMMITS; do
echo -e " $c"
done
echo -e " Labels: ${PR_LABELS:-<none>}"
echo -e " PR title: 🍒 $PR_NUMBER - $PR_TITLE"
if [ $SKIP_CHERRY_PICK -eq 1 ]; then
echo -e " Mode: ${YELLOW}Resume${RESET} (reuse existing remote branch)"
fi
echo ""
info "Dry run complete. Re-run without --dry-run to execute."
exit 0
fi
# --- Backport ----------------------------------------------------------------
if [ $SKIP_CHERRY_PICK -eq 1 ]; then
step "Skipping cherry-pick (reusing existing branch)"
else
step "Creating backport"
git checkout "$RELEASE_BRANCH"
git pull --quiet
git checkout -b "$BACKPORT_BRANCH"
CHERRY_PICK_IN_PROGRESS=1
for PR_COMMIT in $PR_COMMITS; do
git cherry-pick -x "$PR_COMMIT"
done
CHERRY_PICK_IN_PROGRESS=0
git push -u origin "$BACKPORT_BRANCH"
info "Pushed $BACKPORT_BRANCH"
fi
# --- Create PR ---------------------------------------------------------------
step "Creating pull request"
LABEL_ARGS=()
if [ -n "$PR_LABELS" ]; then
LABEL_ARGS=(--label "$PR_LABELS")
fi
BACKPORT_PR_URL=$(gh pr create --base "$RELEASE_BRANCH" \
--head "$BACKPORT_BRANCH" \
--title "🍒 $PR_NUMBER - $PR_TITLE" \
--body "Backport of #$PR_NUMBER to \`$RELEASE_BRANCH\`" \
"${LABEL_ARGS[@]+"${LABEL_ARGS[@]}"}")
if [ -z "$BACKPORT_PR_URL" ]; then
error "gh pr create did not return a URL"
exit 1
fi
info "Created: $BACKPORT_PR_URL"
# Comment on the original PR for traceability
gh pr comment "$PR_NUMBER" --body "Backported to \`$RELEASE_BRANCH\` via $BACKPORT_PR_URL" 2>/dev/null || true
# --- Restore ------------------------------------------------------------------
step "Restoring original branch"
git checkout "$CURRENT_BRANCH"
info "Back on $CURRENT_BRANCH"
echo ""
echo -e "${GREEN}${BOLD}Done!${RESET}"
echo -e " ${BOLD}Backport PR:${RESET} $BACKPORT_PR_URL"