| 1 |
#!/bin/bash |
| 2 |
set -e |
| 3 |
|
| 4 |
sha=$1 |
| 5 |
target=${2:-master} |
| 6 |
dirname=$(mktemp -d) |
| 7 |
user=$(git config github.user) |
| 8 |
|
| 9 |
if [ -z "$user" ]; then |
| 10 |
echo "Please set GitHub User" |
| 11 |
echo "git config --global github.user <github_handle>" |
| 12 |
exit 1 |
| 13 |
fi |
| 14 |
|
| 15 |
mkdir -p $dirname |
| 16 |
if [ -z "$AUTH_TOKEN" ]; then |
| 17 |
git clone [email protected]:mongodb/mongo-go-driver.git $dirname |
| 18 |
else |
| 19 |
echo "$AUTH_TOKEN" > mytoken.txt |
| 20 |
gh auth login --with-token < mytoken.txt |
| 21 |
git clone https://github.com/mongodb/mongo-go-driver.git $dirname |
| 22 |
fi |
| 23 |
|
| 24 |
cd $dirname |
| 25 |
if [ -z "$AUTH_TOKEN" ]; then |
| 26 |
git remote add $user [email protected]:$user/mongo-go-driver.git |
| 27 |
else |
| 28 |
git remote add $user https://$user:${AUTH_TOKEN}@github.com/$user/mongo-go-driver.git |
| 29 |
fi |
| 30 |
|
| 31 |
gh repo set-default mongodb/mongo-go-driver |
| 32 |
branch="cherry-pick-$sha" |
| 33 |
head="$user:$branch" |
| 34 |
git fetch origin |
| 35 |
git checkout -b $branch origin/$target |
| 36 |
git cherry-pick -x $sha || true |
| 37 |
|
| 38 |
files=$(git ls-files -m) |
| 39 |
if [ -n "${files}" ]; then |
| 40 |
EDITOR=${EDITOR:-$(git config core.editor)} |
| 41 |
EDITOR=${EDITOR:-vim} |
| 42 |
for fname in $files; do |
| 43 |
echo "Fixing $fname..." |
| 44 |
$EDITOR $fname |
| 45 |
git add $fname |
| 46 |
done |
| 47 |
echo "Finishing cherry pick." |
| 48 |
git cherry-pick --continue |
| 49 |
fi |
| 50 |
|
| 51 |
old_title=$(git --no-pager log -1 --pretty=%B | head -n 1) |
| 52 |
ticket=$(echo $old_title | sed -r 's/([A-Z]+-[0-9]+).*/\1/') |
| 53 |
text=$(echo $old_title | sed -r 's/([A-Z]+-[0-9]+) (.*) \(#[0-9]*\)/\2/') |
| 54 |
pr_number=$(echo $old_title| sed -r 's/.*(#[0-9]*)\)/\1/') |
| 55 |
|
| 56 |
title="$ticket [$target] $text" |
| 57 |
body="Cherry-pick of $pr_number to $target" |
| 58 |
|
| 59 |
echo |
| 60 |
echo "Creating PR..." |
| 61 |
echo "Title: $title" |
| 62 |
echo "Body: $body" |
| 63 |
echo "Base: $target" |
| 64 |
echo "Head: $head" |
| 65 |
echo |
| 66 |
|
| 67 |
if [ -n "$GITHUB_ACTOR" ]; then |
| 68 |
choice=Y |
| 69 |
else |
| 70 |
read -p 'Push changes? (Y/n) ' choice |
| 71 |
fi |
| 72 |
|
| 73 |
if [[ "$choice" == "Y" || "$choice" == "y" || -z "$choice" ]]; then |
| 74 |
if [ -n "$user" ]; then |
| 75 |
git push $user |
| 76 |
fi |
| 77 |
gh pr create --title "$title" --base $target --head $head --body "$body" |
| 78 |
fi |