3939CURRENT_BRANCH=$( git rev-parse --abbrev-ref HEAD)
4040# Check gh is installed
4141echo " - Checking gh is installed"
42- gh --version 1> /dev/null 2>&1 || { echo " gh is not installed" ; exit 1; }
42+ gh --version 1> /dev/null 2>&1 || { echo " gh is not installed" ; exit 1; }
4343# Check jq is installed
4444echo " - Checking jq is installed"
45- jq --version 1> /dev/null 2>&1 || { echo " jq is not installed" ; exit 1; }
45+ jq --version 1> /dev/null 2>&1 || { echo " jq is not installed" ; exit 1; }
4646# Check there is no local changes
4747echo " - Checking there is no local changes"
48- git diff --exit-code || { echo " There are local changes" ; exit 1; }
48+ git diff --exit-code || { echo " There are local changes" ; exit 1; }
4949# Check remote branch exists
5050echo " - Checking remote release branch exists"
51- git fetch --quiet
52- git show-ref --verify --quiet " refs/remotes/origin/$PATCH_RELEASE_BRANCH " 1> /dev/null 2>&1 || { echo " Branch $PATCH_RELEASE_BRANCH does not exist" ; exit 1; }
51+ git fetch --quiet origin
52+ git show-ref --verify --quiet " refs/remotes/origin/$PATCH_RELEASE_BRANCH " 1> /dev/null 2>&1 || { echo " Branch $PATCH_RELEASE_BRANCH does not exist" ; exit 1; }
5353# Check PR exists
5454echo " - Checking PR exists"
5555PR_COMMITS=$( gh pr view " $PR_NUMBER " --json commits --jq ' .commits[].oid' )
5656if [ -z " $PR_COMMITS " ]; then
57- echo " PR $PR_NUMBER does not exist"
57+ echo " PR $PR_NUMBER does not exist"
5858 exit 1
5959fi
6060# Check all individual commits are still present
6161USE_MERGE_COMMIT=0
6262echo " - Checking all individual commits are still present"
6363for PR_COMMIT in $PR_COMMITS ; do
6464 if ! git cat-file -e " $PR_COMMIT " ; then
65- echo " Commit $PR_COMMIT from PR $PR_NUMBER is no longer present in the repository."
66- echo " This can happen when PR is squashed and remote branch is removed afterwards, original commits can be garbage collected."
65+ echo " Commit $PR_COMMIT from PR $PR_NUMBER is no longer present in the repository."
66+ echo " This can happen when PR is squashed and remote branch is removed afterwards, original commits can be garbage collected."
6767 USE_MERGE_COMMIT=1
6868 break
6969 fi
@@ -74,22 +74,22 @@ if [ $USE_MERGE_COMMIT -eq 0 ]; then
7474 for PR_COMMIT in $PR_COMMITS ; do
7575 PARENT_COUNT=$( git rev-list --parents -n 1 " $PR_COMMIT " 2> /dev/null | wc -w)
7676 if [ " $PARENT_COUNT " -gt 2 ]; then
77- echo " PR $PR_NUMBER contains a merge commit: $PR_COMMIT "
78- echo " Merge commit changes: https://github.com/DataDog/dd-trace-java/commit/${PR_COMMIT} "
79- echo " PR commit list: https://github.com/DataDog/dd-trace-java/pull/${PR_NUMBER} /commits"
77+ echo " PR $PR_NUMBER contains a merge commit: $PR_COMMIT "
78+ echo " Merge commit changes: https://github.com/DataDog/dd-trace-java/commit/${PR_COMMIT} "
79+ echo " PR commit list: https://github.com/DataDog/dd-trace-java/pull/${PR_NUMBER} /commits"
8080 USE_MERGE_COMMIT=1
8181 break
8282 fi
8383 done
8484fi
8585# Ask to use merge commit rather than individual commits
8686if [ $USE_MERGE_COMMIT -eq 1 ]; then
87- echo -n " Would you like to cherry-pick the PR ${PR_NUMBER} merge commit instead of each of its commits individually? (y/n) "
87+ echo -n " Would you like to cherry-pick the PR ${PR_NUMBER} merge commit instead of each of its commits individually? (y/n) "
8888 read -r ANSWER
8989 if [ " $ANSWER " == " y" ]; then
9090 PR_COMMITS=$( gh pr view " $PR_NUMBER " --json mergeCommit --jq ' .mergeCommit.oid' )
9191 else
92- echo " Aborting. Please back-port the PR manually then."
92+ echo " Aborting. Please back-port the PR manually then."
9393 exit 1
9494 fi
9595fi
@@ -100,29 +100,28 @@ PR_LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '[.labels[].name] | join(
100100#
101101# Backport PR to patch release branch.
102102#
103- # Checkout release branch
104- git checkout " $PATCH_RELEASE_BRANCH "
105- # Ensure the branch is up-to-date
106- git pull
103+ # Start backporting
104+ echo " - Backporting PR $PR_NUMBER to $PATCH_RELEASE_BRANCH "
107105# Create a new branch for the backport
108106BRANCH_NAME=" $USER /backport-pr-$PR_NUMBER "
109- git checkout -b " $BRANCH_NAME "
107+ git checkout -b " $BRANCH_NAME " " origin/ $PATCH_RELEASE_BRANCH "
110108# Cherry-pick PR commits
111109for PR_COMMIT in $PR_COMMITS ; do
112110 git cherry-pick -x " $PR_COMMIT "
113111done
114112# Push the branch
115113git push -u origin " $BRANCH_NAME " --no-verify
116114# Create a PR
117- gh pr create --base " $PATCH_RELEASE_BRANCH " \
115+ PR_URL= $( gh pr create --base " $PATCH_RELEASE_BRANCH " \
118116 --head " $BRANCH_NAME " \
119117 --title " 🍒 $PR_NUMBER - $PR_TITLE " \
120118 --body " Backport #$PR_NUMBER to $PATCH_RELEASE_BRANCH " \
121- --label " $PR_LABELS "
119+ --label " $PR_LABELS " )
120+ echo " Backport completed: $PR_URL "
122121
123122#
124123# Clean up.
125124#
126125# Restore current branch
127- echo " - Restoring original state"
126+ echo " - Restoring original working copy state"
128127git checkout " $CURRENT_BRANCH "
0 commit comments