1# #!/bin/bash
2set -euxo pipefail
3
4if [ "$#" -ne 3 ]; then
5 echo "Usage: $0 <branch-name> <commit-sha> <channel>"
6 exit 1
7fi
8
9BRANCH_NAME="$1"
10COMMIT_SHA="$2"
11CHANNEL="$3"
12
13SHORT_SHA="${COMMIT_SHA:0:8}"
14NEW_BRANCH="cherry-pick-${BRANCH_NAME}-${SHORT_SHA}"
15git fetch origin
16git checkout "$BRANCH_NAME"
17git checkout -B "$NEW_BRANCH"
18
19git cherry-pick "$COMMIT_SHA"
20
21git push origin -f "$NEW_BRANCH"
22COMMIT_TITLE=$(git log -1 --pretty=format:"%s" "$COMMIT_SHA")
23COMMIT_BODY=$(git log -1 --pretty=format:"%b" "$COMMIT_SHA")
24
25# Check if commit title ends with (#number)
26if [[ "$COMMIT_TITLE" =~ \(#([0-9]+)\)$ ]]; then
27 PR_NUMBER="${BASH_REMATCH[1]}"
28 PR_BODY="Cherry-pick of #${PR_NUMBER} to ${CHANNEL}"$'\n'$'\n'"----"$'\n'"${COMMIT_BODY}"
29else
30 PR_BODY="Cherry-pick of ${COMMIT_SHA} to ${CHANNEL}"$'\n'$'\n'"----"$'\n'"${COMMIT_BODY}"
31fi
32
33# Create a pull request
34gh pr create --base "$BRANCH_NAME" --head "$NEW_BRANCH" --title "$COMMIT_TITLE (cherry-pick to $CHANNEL)" --body "$PR_BODY"