Skip to content

Commit

Permalink
Merge pull request #58 from greg0ire/51--jira-ticket-support
Browse files Browse the repository at this point in the history
Add in Ticket ref from branch name to each commit
  • Loading branch information
greg0ire committed Oct 1, 2018
2 parents 468b569 + e3a5646 commit c8defe2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/hooks/ticketref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Description

Adds a ticket reference to the commit-msg, this allows for some workflows to
be able to correlate a number of commits against a specific
feature/ticket/incident/issue etc. once merged.

A branch of `ABC-1234-hello-world` would turn a commit of
`git commit -m "Hello World"` into `[ABC-1234] Hello World`. (by default)

# Activation

```sh
git config --add hooks.enabled-plugins ticketref
```

# Configuration

By default this hook will prefix the commit message with the ticket reference,
this can be configured to be added as a suffix via:
```sh
git config hooks.ticketref.position "POST"
```
9 changes: 9 additions & 0 deletions template/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
test -d "$GIT_DIR"/rebase-merge -o -d "$GIT_DIR"/rebase-apply && exit 0
for enabled_plugin in $(git config --get-all hooks.enabled-plugins)
do
if [ -f "$GIT_DIR/hooks/$enabled_plugin/commit-msg" ]
then
"$GIT_DIR/hooks/$enabled_plugin/commit-msg" "$1" "$2"
fi
done
21 changes: 21 additions & 0 deletions template/hooks/ticketref/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# shellcheck source=../git_config_wrapper.sh
GIT_DIR=$(git rev-parse --git-dir)
. "$GIT_DIR/hooks/git_config_wrapper.sh"

get_hook_config ticketref position REF_LOC optional PRE

# If commit message is a fixup message, ignore it
grep --quiet 'fixup!' "$1" && FIXUP="YES"

TICKET=$(git symbolic-ref HEAD | rev | cut -d/ -f1 | rev | grep -o -E "[A-Z]+-[0-9]+")
grep --quiet "${TICKET}" "$1" && TICKET_EXISTS="YES"

if [[ -n "${TICKET}" && -z "${TICKET_EXISTS}" && -z "${FIXUP}" ]]; then

if [[ "${REF_LOC}" == "PRE" ]]; then
sed -i.bak -e "1s/^/[${TICKET}] /" "$1"
else
printf '\n\nRef: %s' "${TICKET}" >> "$1"
fi
fi
53 changes: 53 additions & 0 deletions tests/ticketref_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

testAddsTicketRefToStartOfCommitMsg()
{
testRepo=$SHUNIT_TMPDIR/test_repo1
mkdir -p "${testRepo}"
cd "${testRepo}" || exit 1
git init --quiet .
git config --add hooks.enabled-plugins ticketref
git checkout -b ABC-1234-branch 2>/dev/null
date > dummyFile
git add dummyFile
git commit --quiet --message "Add dummyFile to repo"
git log -1 | grep 'ABC-1234'
rtrn=$?
assertEquals "ABC-1234 found within Commit " 0 $rtrn
}

testEnsureCommitMsgContainsOneTicketRef()
{
testRepo=$SHUNIT_TMPDIR/test_repo2
mkdir -p "${testRepo}"
cd "${testRepo}" || exit 1
git init --quiet .
git config hooks.enabled-plugins ticketref
git checkout -b ABC-1234-branch 2>/dev/null
date > dummyFile
git add dummyFile
git commit --quiet --message "ABC-1234 Add dummyFile to repo"
git log -1 | grep -E '^ABC-1234'
rtrn=$?
assertEquals "Single reference of ABC-1234 found within Commit " 1 $rtrn
}

testRefIsAddedasSuffixToCommit()
{
testRepo=$SHUNIT_TMPDIR/test_repo3
mkdir -p "${testRepo}"
cd "${testRepo}" || exit 1
git init --quiet .
git config hooks.enabled-plugins ticketref
git config hooks.ticketref.position "POST"
git checkout -b ABC-1234-branch 2>/dev/null
date > dummyFile
git add dummyFile
git commit --quiet --message "Add dummyFile to repo"
git log -1 | grep -E 'Ref: ABC-1234$'
rtrn=$?
assertEquals "Reference of ABC-1234 found within Commit at the end " 0 $rtrn
}

[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. "$(which shunit2)"

0 comments on commit c8defe2

Please sign in to comment.