Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active July 2, 2019 16:38
Show Gist options
  • Save ToniRib/6c87d63a19cdbf2f44c928f5fa5332fb to your computer and use it in GitHub Desktop.
Save ToniRib/6c87d63a19cdbf2f44c928f5fa5332fb to your computer and use it in GitHub Desktop.
Create a PR from the command line with fish!

Actions

  • Pushes the code to GitHub
  • Creates a pull request between the current branch and master
  • Includes the JIRA story link in the pull request description

Usage:

create_pull_request <story number:required> <title:optional>

Requirements

Requires jq to be installed via homebrew.

Information

Currently, this always creates a PR for the current branch, but could easily be modified to create a PR for a different branch. I figured we normally want to create a PR for whatever branch we are on, and that's one less argument to specify.

If no title is provided, it will use the branch name. If you provide a title that is more than one word, you need to put the whole string in quotes.

If you provide a story number with a pound sign, it will strip that before creating the PR.

Uses the current directory to pick which GitHub project you are pushing to. This will not work if you named your local directory something different than the project name.

This requires that you have the following variable in your fish config:

  • GITHUB_API_TOKEN created and obtained on GitHub in your personal account

File in ~/.config/fish/functions/create_pull_request.fish

function create_pull_request
  if test (count $argv) -eq 0
    echo "Must specify a story number"
  else
    set -l branch (git rev-parse --abbrev-ref HEAD)
    echo "Pushing and creating Pull Request for $branch..."
    git push --set-upstream origin $branch
    sleep 1

    set -l story (string replace -a '#' '' $argv[1])
    set -l title $branch
    if test (count $argv) -eq 2
      set title $argv[2]
    end

    set -l current_project (string split -r -m1 / (pwd))[2]

    set -l pr_url (curl -H "Authorization: bearer $GITHUB_API_TOKEN" -X POST -d "
    \
    { \
      \"title\": \"$title\",
      \"body\": \"**Story:** https://gsc.atlassian.net/browse/$story\",
      \"head\": \"$branch\",
      \"base\": \"master\"
    } " https://api.github.com/repos/gospotcheck/$current_project/pulls | jq -r '.html_url')

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $pr_url

    echo ""
    echo "PR created at: $pr_url"
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment