Skip to content

Commit a9a2f61

Browse files
PerfectSlayerdevflow.devflow-routing-intake
andauthored
feat(tooling): Improve backporting script (#12181)
feat(tooling): Improve backporting script Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 4373ec0 commit a9a2f61

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

tooling/backport-pr-to-patch-release.sh

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ fi
3939
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
4040
# Check gh is installed
4141
echo "- 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
4444
echo "- 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
4747
echo "- 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
5050
echo "- 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
5454
echo "- Checking PR exists"
5555
PR_COMMITS=$(gh pr view "$PR_NUMBER" --json commits --jq '.commits[].oid')
5656
if [ -z "$PR_COMMITS" ]; then
57-
echo "PR $PR_NUMBER does not exist"
57+
echo " PR $PR_NUMBER does not exist"
5858
exit 1
5959
fi
6060
# Check all individual commits are still present
6161
USE_MERGE_COMMIT=0
6262
echo "- Checking all individual commits are still present"
6363
for 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
8484
fi
8585
# Ask to use merge commit rather than individual commits
8686
if [ $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
9595
fi
@@ -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
108106
BRANCH_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
111109
for PR_COMMIT in $PR_COMMITS; do
112110
git cherry-pick -x "$PR_COMMIT"
113111
done
114112
# Push the branch
115113
git 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"
128127
git checkout "$CURRENT_BRANCH"

0 commit comments

Comments
 (0)