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