Skip to content

Commit

Permalink
Option --use-label to use label
Browse files Browse the repository at this point in the history
  • Loading branch information
martingtheodo committed Mar 8, 2018
1 parent b716385 commit 331d33e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It uses the `hub` by Github program, available [here](https://hub.github.com/).
- Make sure the script is executable : `chmod +x /usr/local/bin/git-pretty-pull-request`
- In each of your projects, set the branches on which you want to open pull requests: `git config pretty-pull-request.pull-bases "integration preprod prod"` (or set it globally with the `--global` option)
- (optional) `git pretty-pull-request` is tedious to type, I'd advise to alias it: `git config --global alias.pr pretty-pull-request`.
- (optional) In each of your projects, set the labels you want to add to your pull requests: `git config pretty-pull-request.pull-bases.prod "Waiting for validation"` (or set it globally with the `--global` option)
- (optional) In each of your projects, set the labels you want to add to your pull requests: `git config pretty-pull-request.labels.prod "Waiting for PO validation"` (or set it globally with the `--global` option)

`git pr` is now an alias to `git pretty-pull-request`, which will call your git-pretty-pull-request script if it can be found in your $PATH!

Expand Down
55 changes: 43 additions & 12 deletions git-pretty-pull-request
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ function single_base() {
[ ${#branches[@]} -eq 1 ]
}

function set_options() {
while [ -n "$1" ]; do
case $1 in
--use-label)
useLabel=true
;;
-m)
if [ -n "$2" ]; then
msg=$(echo "$2" | trim)
shift
else
echo 'WARN: no message found for "-m" option.'
fi
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
}

function init() {
rootDir=$(git rev-parse --show-toplevel)
msgFile=$rootDir/.git/PRETTY_PR_EDITMSG
Expand Down Expand Up @@ -104,8 +132,7 @@ function should_open_editor() {

function get_pull_request_message() {
# If there's an argument, it's the PR message. Else, the message is the last commit message
[ -z "$1" ] && msg=$(git log -1 --pretty=%B)
[ -z "$msg" ] && msg=$1
[ -z "$msg" ] && msg=$(git log -1 --pretty=%B)

if ! should_open_editor; then
echo "$msg"
Expand All @@ -127,13 +154,14 @@ $template
sed -e '/^;.*$/d' $msgFile # remove comments
}


useLabel=false
init
set_options $@
sync_origin $head origin/$head & # in background
SYNC_PID=$!

# opens editor if necessary and trim
msg=$(get_pull_request_message "$1" | trim)
msg=$(get_pull_request_message)
[[ "$msg" = *[![:space:]]* ]] || no_pr_message
msgTitle=$(echo "$msg" | head -n 1)

Expand Down Expand Up @@ -172,19 +200,22 @@ wait $SYNC_PID || echo -e "${RED}Error running \`git fetch\`"
i=0
while [ $i -lt ${#branches[@]} ]; do
if single_base; then
label=$(git config pretty-pull-request.pull-bases.${branches[i]} || echo "error")
if [ "$label" = "error" ];then
hub pull-request -m "$msg" -b ${branches[i]} -h $head | print_and_copy || true
else
echo -n "[${branches[i]}]: "
label=$(git config pretty-pull-request.labels.${branches[i]} || echo "error")
if [ "$useLabel" = true -a "$label" != "error" ];then
echo -n "{Label=$label }"
hub pull-request -m "$msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true
else
hub pull-request -m "$msg" -b ${branches[i]} -h $head | print_and_copy || true
fi
else
echo -n "[${prefixes[i]}]: "
label=$(git config pretty-pull-request.pull-bases.${branches[i]} || echo "error")
if [ "$label" = "error" ];then
hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true
else
label=$(git config pretty-pull-request.labels.${branches[i]} || echo "error")
if [ "$useLabel" = true -a "$label" != "error" ];then
echo -n "{Label=$label }"
hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true
else
hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true
fi
fi
i=$[$i+1]
Expand Down

0 comments on commit 331d33e

Please sign in to comment.