-
-
Save mollifier/11360421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Description: | |
# Backlog + git-flow + WIP PR script | |
# Usage: | |
# wip-pr pj-123 | |
# | |
# Requirement: | |
# use Mac OS X | |
# install git | |
# install hub | |
# install [email protected] | |
# create master repository | |
# create backlog issue | |
# cd /path/to/your/repository | |
# git checkout develop | |
# | |
# Note: | |
# brew install git | |
# brew install hub | |
# npm install -g [email protected] | |
# | |
declare -r SCRIPT_NAME=${0##*/} | |
declare -r DEFAULT_BACKLOG_USERNAME="$WIP_PR_BACKLOG_USERNAME" | |
declare -r DEFAULT_GITHUB_USERNAME="$WIP_PR_GITHUB_USERNAME" | |
declare -r DEFAULT_GITHUB_UPSTREAM_USERNAME="$WIP_PR_GITHUB_UPSTREAM_USERNAME" | |
print_usage() | |
{ | |
cat << EOF | |
Usage: $SCRIPT_NAME [-f] [-b NAME1] [-g NAME2] [-u NAME3] PROJECT_NAME ISSUE_NUMBER | |
Backlog + git-flow + WIP pull request script. | |
This script performs following steps. | |
Create feature branch | |
Push empty commit, so create feature branch in origin repository | |
Send pull request to upstream repository | |
Update backlog issue | |
-b specify backlog username | |
-g specify github username | |
-u specify github upstream username | |
-f never prompt | |
-h display this help and exit | |
Examples: | |
$SCRIPT_NAME proj 120 | |
$SCRIPT_NAME -f proj 120 | |
ENVIRONMENT VARIABLES: | |
WIP_PR_BACKLOG_USERNAME specifies default backlog username | |
WIP_PR_GITHUB_USERNAME specifies default github username | |
WIP_PR_GITHUB_UPSTREAM_USERNAME specifies default github upstream username | |
EOF | |
} | |
print_error() | |
{ | |
echo "$SCRIPT_NAME: $@" 1>&2 | |
echo "Try \`-h' option for more information." 1>&2 | |
} | |
# $1 = $2 => 0 | |
# $1 > $2 => 1 | |
# $1 < $2 => 2 | |
compare_version() { | |
if [[ "$1" == "$2" ]]; then | |
return 0 | |
fi | |
local IFS=. | |
local i ver1=($1) ver2=($2) | |
# fill empty fields in ver1 with zeros | |
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do | |
ver1[i]=0 | |
done | |
for ((i=0; i<${#ver1[@]}; i++)); do | |
if [[ -z "${ver2[i]}" ]]; then | |
# fill empty fields in ver2 with zeros | |
ver2[i]=0 | |
fi | |
if ((10#${ver1[i]} > 10#${ver2[i]})); then | |
return 1 | |
fi | |
if ((10#${ver1[i]} < 10#${ver2[i]})); then | |
return 2 | |
fi | |
done | |
return 0 | |
} | |
required_commands_are_installed() | |
{ | |
installed=0 | |
if ! which git > /dev/null 2>&1; then | |
echo "git isn't installed" 1>&2 | |
echo "Try 'brew install git'" 1>&2 | |
installed=1 | |
fi | |
if ! which hub > /dev/null 2>&1; then | |
echo "hub isn't installed" 1>&2 | |
echo "Try 'brew install hub'" 1>&2 | |
installed=1 | |
fi | |
if ! which backlog > /dev/null 2>&1; then | |
echo "backlog isn't installed" 1>&2 | |
echo "Try 'npm install -g backlog-cli'" 1>&2 | |
installed=1 | |
else | |
local backlog_cli_version=$(backlog --version) | |
compare_version "$backlog_cli_version" "0.2.0" | |
if [[ $? -eq "2" ]]; then | |
echo "need backlog cli version 0.2.0 or later" 1>&2 | |
echo "Try 'npm install -g backlog-cli'" 1>&2 | |
installed=1 | |
fi | |
fi | |
return $installed | |
} | |
force=1 | |
backlog_username="$DEFAULT_BACKLOG_USERNAME" | |
github_username="$DEFAULT_GITHUB_USERNAME" | |
github_upstream_username="$DEFAULT_GITHUB_UPSTREAM_USERNAME" | |
while getopts ':b:g:u:fh' option; do | |
case $option in | |
b) | |
backlog_username="$OPTARG" | |
;; | |
g) | |
github_username="$OPTARG" | |
;; | |
u) | |
github_upstream_username="$OPTARG" | |
;; | |
f) | |
force=0 | |
;; | |
h) | |
print_usage | |
exit 0 | |
;; | |
:) | |
print_error "option requires an argument -- $OPTARG" | |
exit 1 | |
;; | |
*) | |
print_error "invalid option -- $OPTARG" | |
exit 1 | |
;; | |
esac | |
done | |
shift $(expr $OPTIND - 1) | |
if ! required_commands_are_installed; then | |
exit 1 | |
fi | |
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) != 'true' ]]; then | |
print_error "Not in a git repository" | |
exit 1 | |
fi | |
if [[ "$(git symbolic-ref HEAD 2>/dev/null)" != 'refs/heads/develop' ]]; then | |
print_error "Not in develop branch" | |
exit 1 | |
fi | |
if [[ -z "$backlog_username" ]]; then | |
print_error "You must specify backlog_username" | |
exit 1 | |
fi | |
if [[ -z "$github_username" ]]; then | |
print_error "You must specify github_username" | |
exit 1 | |
fi | |
if [[ -z "$github_upstream_username" ]]; then | |
print_error "You must specify github_upstream_username" | |
exit 1 | |
fi | |
# parse args | |
project=$(echo $1 | tr '[:lower:]' '[:upper:]') | |
issue_no=$2 | |
issue_key="${project}-${issue_no}" | |
if [[ -z "$project" ]]; then | |
print_error "You must specify project name" | |
exit 1 | |
fi | |
if [[ -z "$issue_no" ]]; then | |
print_error "You must specify issue number" | |
exit 1 | |
fi | |
# fetch issue info | |
issue_summary=$(backlog issue info ${issue_key} -f summary) | |
issue_url=$(backlog issue info ${issue_key} -f url) | |
test -n "${issue_summary}" || exit 1 | |
test -n "${issue_url}" || exit 1 | |
# prompt | |
if [[ "$force" == "0" ]]; then | |
echo ${issue_summary} 1>&2 | |
echo ${issue_url} 1>&2 | |
read -p 'OK? [yN]' yn | |
test "${yn}" = 'y' -o "${yn}" = 'Y' || exit 0 | |
fi | |
# exit immediately if any command return error | |
set -e | |
# commit & push | |
git checkout -b feature/${issue_no} | |
git commit --allow-empty -m "[WIP] ${issue_key} ${issue_summary}" | |
# TODO : ${github_username} is origin?? | |
git push ${github_username} feature/${issue_no} | |
# pull request | |
pull_request_url=$( | |
{ | |
echo "[WIP] ${issue_key} ${issue_summary}" | |
echo "" | |
echo "${issue_url}" | |
} | \ | |
hub pull-request \ | |
-F - \ | |
-b "${github_upstream_username}:develop" \ | |
) | |
# update issue | |
backlog issue status ${issue_key} 2 \ | |
--assigner ${backlog_username} \ | |
--comment ${pull_request_url} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment