Skip to content

Commit 24b5903

Browse files
feat(ci): Improve backport script (#9254)
Use merge commit when individual commits are not present anymore.
1 parent 7817fcb commit 24b5903

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,43 @@ if [ -z "$PR_COMMITS" ]; then
5757
echo "PR $PR_NUMBER does not exist"
5858
exit 1
5959
fi
60-
# Check PR does not contain merge commit
61-
echo "- Checking PR does not contain merge commit"
60+
# Check all individual commits are still present
61+
USE_MERGE_COMMIT=0
62+
echo "- Checking all individual commits are still present"
6263
for PR_COMMIT in $PR_COMMITS; do
63-
PARENT_COUNT=$(git rev-list --parents -n 1 "$PR_COMMIT" | wc -w)
64-
if [ "$PARENT_COUNT" -gt 2 ]; then
65-
echo "PR $PR_NUMBER contains a merge commit: $PR_COMMIT"
66-
echo "Merge commit changes: https://github.com/DataDog/dd-trace-java/commit/${PR_COMMIT}"
67-
echo "PR commit list: https://github.com/DataDog/dd-trace-java/pull/${PR_NUMBER}/commits"
68-
echo -n "Would you like to cherry-pick the PR ${PR_NUMBER} merge commit instead of each of its commits individually? (y/n) "
69-
read -r ANSWER
70-
if [ "$ANSWER" == "y" ]; then
71-
PR_COMMITS=$(gh pr view "$PR_NUMBER" --json mergeCommit --jq '.mergeCommit.oid')
72-
else
73-
exit 1
74-
fi
64+
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."
67+
USE_MERGE_COMMIT=1
68+
break
7569
fi
7670
done
71+
if [ $USE_MERGE_COMMIT -eq 0 ]; then
72+
# Check PR does not contain merge commit
73+
echo "- Checking PR does not contain merge commit"
74+
for PR_COMMIT in $PR_COMMITS; do
75+
PARENT_COUNT=$(git rev-list --parents -n 1 "$PR_COMMIT" 2>/dev/null | wc -w)
76+
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"
80+
USE_MERGE_COMMIT=1
81+
break
82+
fi
83+
done
84+
fi
85+
# Ask to use merge commit rather than individual commits
86+
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) "
88+
read -r ANSWER
89+
if [ "$ANSWER" == "y" ]; then
90+
PR_COMMITS=$(gh pr view "$PR_NUMBER" --json mergeCommit --jq '.mergeCommit.oid')
91+
else
92+
echo "Aborting. Please back-port the PR manually then."
93+
exit 1
94+
fi
95+
fi
96+
# Fetch PR details
7797
PR_TITLE=$(gh pr view "$PR_NUMBER" --json title --jq '.title')
7898
PR_LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '[.labels[].name] | join(",")')
7999

0 commit comments

Comments
 (0)