Skip to content

Instantly share code, notes, and snippets.

@tantofish
Created April 26, 2016 03:32
Show Gist options
  • Save tantofish/8acf32c259daa9c6c941662cd91e3fbf to your computer and use it in GitHub Desktop.
Save tantofish/8acf32c259daa9c6c941662cd91e3fbf to your computer and use it in GitHub Desktop.
Create git PR from command line
  1. Install jq
  2. Create git remote access token
  3. Add token to .gitconfig
[user]
   email = xxx
   user = xxx
   token = xxxxxxxxxx
  1. Define the PR function
#
## git pull request
#
function pr(){
    title=$1
    msg=$2
    whoami=`whoami`
 
    if [ -z "$title" -o -z "$msg" ]; then
        echo Usage: ${whoami}$ pr "\"Pull Request Title\"" "\"PR comment message (add 'reviewer: @yid' to hook with hipchat)\""
        return 0;
    fi
 
    headBranch=`git rev-parse --abbrev-ref HEAD`
    repo=`git remote -v | head -n 1 | awk -F'/' '{print $2}' | awk -F'.' '{print $1}'`
    token=`git config user.token`
    repos=`curl -s -u ${token}:x-oauth-basic -H "Accept: application/vnd.github.moondragon-preview+json" -H "Content-Type: application/json" -k https://git.corp.yahoo.com/api/v3/repos/$whoami/$repo | jq -r ".parent.full_name"`
    # get pull request result
    url=`curl -k -s -u $token:x-oauth-basic -H "Accept: application/vnd.github.moondragon-preview+json" -H "Content-Type: application/json" -d "{\"title\":\"${title}\",\"body\":\"${msg}\",\"head\":\"${whoami}:${headBranch}\",\"base\":\"master\"}" -k https://git.corp.yahoo.com/api/v3/repos/$repos/pulls | jq -r ".html_url"`
    if [ $url == "null" ]; then
        echo "execute command fail, please check your repos status."
    else
        echo $url
    fi
}
  1. Try to create PR:
>> git commit -am "update conf"; git push origin dev; pr "update conf" "hi @yutu"
https://github.com/<upstream>/<repo>/pull/96
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment