Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels to pull requests if configured #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.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
53 changes: 47 additions & 6 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,10 +200,23 @@ 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
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]}]: "
hub pull-request -m "[${prefixes[i]}] $msg" -b ${branches[i]} -h $head | print_and_copy || true
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]
done
Expand Down