From 097007758c55c302e446459b022762019149e3a4 Mon Sep 17 00:00:00 2001 From: martingtheodo Date: Tue, 6 Feb 2018 18:37:38 +0100 Subject: [PATCH 1/3] Add labels to the pull requests if configured with --- git-pretty-pull-request | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/git-pretty-pull-request b/git-pretty-pull-request index 95d56f7..0fb290d 100755 --- a/git-pretty-pull-request +++ b/git-pretty-pull-request @@ -172,10 +172,20 @@ wait $SYNC_PID || echo -e "${RED}Error running \`git fetch\`" i=0 while [ $i -lt ${#branches[@]} ]; do if single_base; then - hub pull-request -m "$msg" -b ${branches[i]} -h $head | print_and_copy || true + 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 + hub pull-request -m "$msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true + fi else echo -n "[${prefixes[i]}]: " - hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true + 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 + hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head -l "$label" | print_and_copy || true + fi fi i=$[$i+1] done From b716385e705056a09d820007a2804263712b5239 Mon Sep 17 00:00:00 2001 From: martingtheodo Date: Tue, 6 Feb 2018 18:46:38 +0100 Subject: [PATCH 2/3] Update the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2d2f6de..2227690 100644 --- a/README.md +++ b/README.md @@ -15,6 +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) `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! From 3a08fcc298b9748da15f4951c57ee2a2e0c17e0f Mon Sep 17 00:00:00 2001 From: martingtheodo Date: Thu, 8 Mar 2018 11:59:56 +0100 Subject: [PATCH 3/3] Option --use-label to use label --- README.md | 2 +- git-pretty-pull-request | 55 ++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2227690..78483eb 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/git-pretty-pull-request b/git-pretty-pull-request index 0fb290d..010d96a 100755 --- a/git-pretty-pull-request +++ b/git-pretty-pull-request @@ -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 @@ -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" @@ -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) @@ -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]