|
| 1 | +#!/bin/bash |
| 2 | +# Format $ticketNumber $targetVersion[] |
| 3 | + |
| 4 | +sourceGh="GH-$1" |
| 5 | +branch=$(git branch --show-current) |
| 6 | +json=$(gh issue view $1 --json=title,labels) |
| 7 | + |
| 8 | +title=$(echo $json | jq -r '.title') |
| 9 | +labels=$(echo $json | jq -r '.labels[].name' | paste -sd ',' -) |
| 10 | + |
| 11 | +number=$1 |
| 12 | + |
| 13 | +# The SHAs of all commits associated with the source ticket |
| 14 | +shas=$(git log --grep="$sourceGh" --reverse --format="%H") |
| 15 | + |
| 16 | +# For each of the target versions |
| 17 | +for version in ${@:2} |
| 18 | +do |
| 19 | + # Turn 1.5.6 into 1.5.x |
| 20 | + targetBranch="$(echo "$version" | grep -oE '^[0-9]+\.[0-9]+').x" |
| 21 | + |
| 22 | + # Checkout target branch and cherry-pick commit |
| 23 | + echo "Checking out branch $targetBranch" |
| 24 | + git checkout $targetBranch |
| 25 | + |
| 26 | + # Cherry-pick all previously found SHAs |
| 27 | + while IFS= read -r sha |
| 28 | + do |
| 29 | + echo "Cherry-pick commit $sha from $branch" |
| 30 | + git cherry-pick $sha |
| 31 | + done <<< $shas |
| 32 | + |
| 33 | + echo "gh issue create --title \"$title\" --body \"Back-port of $sourceGh.\" --label \"$labels\" --assignee \"@me\" --milestone \"$version\"" |
| 34 | + number=$(gh issue create --title "$title" --body "Back-port of $sourceGh." --label "$labels" --assignee "@me" --milestone "$version" | awk -F '/' '{print $NF}') |
| 35 | + echo "New ticket number: $number" |
| 36 | + |
| 37 | + # Replace ticket reference with new one |
| 38 | + targetGh="GH-$number" |
| 39 | + expression="s/$sourceGh/$targetGh/g" |
| 40 | + message=$(git log -1 --pretty=format:"%s" | sed $expression) |
| 41 | + |
| 42 | + # Update commit message to refer to new ticket |
| 43 | + echo "Adapt commit message from $sourceGh to $targetGh" |
| 44 | + git commit --amend -m "$message" |
| 45 | +done |
| 46 | + |
| 47 | +# Return to original branch |
| 48 | +git checkout $branch |
0 commit comments