#!/bin/bash

#
# Usage: gitpr "My PR title"
#
# If branch name follows this convention: anything_JIRAPROJECT-TASKID then link to JIRA
# task is added in PR's description.
#
# This script requires https://hub.github.com/ to be installed.
#

JIRA_PROJECT_URL="https://[YOUR-PROJECT].atlassian.net"
BRANCH=`git rev-parse --abbrev-ref HEAD`
TASK=`echo $BRANCH | perl -pe 's|.*?([A-Z]+-[0-9]+)|\1|'`
JIRA="JIRA: ${JIRA_PROJECT_URL}/browse/${TASK}"


if [ "$BRANCH" = "master" ]; then
  echo "Can't make a PR out of master, it's not a feature branch."
else
  if [ "$1" = "" ]; then
    echo "You have to specify a PR title."
  else
    PR_TITLE=$1
  fi
  if [ "$BRANCH" != "$TASK" ]; then
    PR_DESC=$JIRA
  fi
fi

if [ "$PR_TITLE" != "" ]; then
  git push origin ${BRANCH}
  echo -e "${PR_TITLE}\n\n${PR_DESC}" | hub pull-request -F -
fi