-
Notifications
You must be signed in to change notification settings - Fork 353
/
backport.sh
executable file
·59 lines (47 loc) · 1.48 KB
/
backport.sh
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
#!/usr/bin/env bash
# Usage: ./backport.sh 'feature/im-play-nightly' 47bc4b2ade1baffbb6693ae149bca6126c2f352e 2.40 2.39 2.38 2.37
set -euo pipefail
if ! git diff --cached --exit-code; then
echo "Nothing should be staged when running this script."
exit 1
fi
if ! command -v git &> /dev/null; then
echo "git could not be found."
exit 1
fi
if ! command -v gh &> /dev/null; then
echo "gh could not be found."
exit 1
fi
if [[ -z "${1:-}" ]]; then
echo "No base branch specified."
exit 1
fi
base_branch="$1"
if [[ -z "${2:-}" ]]; then
echo "No commit specified."
exit 1
fi
commit="$2"
shift 2
if [[ -z "${*:-}" ]]; then
echo "No backport branches specified."
exit 1
fi
pr_id=$(gh pr list --search "$commit" --state merged --head "$base_branch" --json number --jq '.[0].number')
pr_url=$(gh pr view "$pr_id" --json url --jq '.url')
pr_title=$(gh pr view "$pr_id" --json title --jq '.title')
pr_reviewer=$(gh pr view "$pr_id" --json reviews --jq '.reviews | map(.author.login) | join(",")')
current_branch=$(git branch --show-current)
branches=("$@")
for branch in "${branches[@]}"; do
git checkout "$branch"
git pull origin "$branch"
git checkout -b "${base_branch}_${branch}"
git cherry-pick "$commit"
git push origin "${base_branch}_${branch}"
gh pr create --head "${base_branch}_${branch}" --base "$branch" --title "backport: $pr_title" --body "$pr_url" --reviewer "$pr_reviewer"
# clean up
git checkout "$current_branch"
git branch -D "${base_branch}_${branch}"
done