From 2b3aea9565360adb0a17d0af76f58a76ec69eac0 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 10 Dec 2024 18:07:15 +0100 Subject: [PATCH] GH-2259 - Add script to automatically backport commits. --- etc/backport-ticket.sh | 48 ++++++++++++++++++++++++++++++++++++++++++ etc/copy-ticket.sh | 13 ------------ 2 files changed, 48 insertions(+), 13 deletions(-) create mode 100755 etc/backport-ticket.sh delete mode 100755 etc/copy-ticket.sh diff --git a/etc/backport-ticket.sh b/etc/backport-ticket.sh new file mode 100755 index 00000000..3cf74a46 --- /dev/null +++ b/etc/backport-ticket.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# Format $ticketNumber $targetVersion[] + +sourceGh="GH-$1" +branch=$(git branch --show-current) +json=$(gh issue view $1 --json=title,labels) + +title=$(echo $json | jq -r '.title') +labels=$(echo $json | jq -r '.labels[].name' | paste -sd ',' -) + +number=$1 + +# The SHAs of all commits associated with the source ticket +shas=$(git log --grep="$sourceGh" --reverse --format="%H") + +# For each of the target versions +for version in ${@:2} +do + # Turn 1.5.6 into 1.5.x + targetBranch="$(echo "$version" | grep -oE '^[0-9]+\.[0-9]+').x" + + # Checkout target branch and cherry-pick commit + echo "Checking out branch $targetBranch" + git checkout $targetBranch + + # Cherry-pick all previously found SHAs + while IFS= read -r sha + do + echo "Cherry-pick commit $sha from $branch" + git cherry-pick $sha + done <<< $shas + + echo "gh issue create --title \"$title\" --body \"Back-port of $sourceGh.\" --label \"$labels\" --assignee \"@me\" --milestone \"$version\"" + number=$(gh issue create --title "$title" --body "Back-port of $sourceGh." --label "$labels" --assignee "@me" --milestone "$version" | awk -F '/' '{print $NF}') + echo "New ticket number: $number" + + # Replace ticket reference with new one + targetGh="GH-$number" + expression="s/$sourceGh/$targetGh/g" + message=$(git log -1 --pretty=format:"%s" | sed $expression) + + # Update commit message to refer to new ticket + echo "Adapt commit message from $sourceGh to $targetGh" + git commit --amend -m "$message" +done + +# Return to original branch +git checkout $branch diff --git a/etc/copy-ticket.sh b/etc/copy-ticket.sh deleted file mode 100755 index 26426401..00000000 --- a/etc/copy-ticket.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -sourceGh="GH-$1" -json=$(gh issue view $1 --json=title,labels) - -title=$(echo $json | jq -r '.title') -labels=$(echo $json | jq -r '.labels[].name' | paste -sd ',' -) - -gh issue create --title "$title" \ - --body "Back-port of $sourceGh." \ - --label "$labels" \ - --assignee "@me" \ - --milestone "$2"