1+ #! /bin/bash
2+
3+ # Reqires atleast one jira ticket in the format AUS-1234 on either a branch or commit message
4+
5+ # Checkout branch
6+ git checkout -q $1
7+
8+ # Set variables
9+ BASE_BRANCH=$2
10+ msg_regex=' (AAA|BBB|CCC)\-[0-9]+'
11+ skip_regex=' \[SKIP JIRA\]'
12+
13+ # Initialize invalidCommit as false, will be set to true by any invalid commits
14+ # Find current branch name
15+ CURRENT_BRANCH=$( git branch --show-current)
16+
17+ # echo "Current branch is:" $CURRENT_BRANCH
18+ # Find hash of commit most common ancestor, e.g. where branch began
19+ BRANCH_MERGE_BASE=$( git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH} )
20+ # echo "Branch merge base hash is:" $BRANCH_MERGE_BASE
21+ # Find all commits since common ancestor
22+ BRANCH_COMMITS=$( git rev-list ${BRANCH_MERGE_BASE} ..HEAD)
23+
24+ # Check every commit message since ancestor for regex matchs
25+ for commit in $BRANCH_COMMITS ; do
26+ COMMIT_MSG_UPPER=$( git log --max-count=1 --format=%B $commit | tr ' [a-z]' ' [A-Z]' )
27+
28+ if echo $COMMIT_MSG_UPPER | grep -iqE " $skip_regex " ; then
29+ echo " ************"
30+ echo " [skip jira] detected, skipping commit linting"
31+ echo " ************"
32+ exit 0
33+ fi
34+
35+ if echo $COMMIT_MSG_UPPER | grep -iqE " $msg_regex " ; then
36+ echo " ************"
37+ echo " Jira ticket # found in a commit mesage 👍🏻"
38+ echo " ************"
39+ exit 0
40+ fi
41+ done
42+
43+ CURRENT_BRANCH_UPPER=$( echo $CURRENT_BRANCH | tr ' [a-z]' ' [A-Z]' )
44+
45+ if echo $CURRENT_BRANCH_UPPER | grep -iqE " $skip_regex " ; then
46+ echo " ************"
47+ echo " [skip jira] detected, skipping commit linting"
48+ echo " ************"
49+ exit 0
50+ fi
51+
52+ if echo $CURRENT_BRANCH_UPPER | grep -iqE " $msg_regex " ; then
53+ echo " ************"
54+ echo " Jira ticket # found in a branch name 👍🏻"
55+ echo " ************"
56+ exit 0
57+ fi
58+
59+ # If we made it thsis far, no JIRA ticket # was detected in a commit or branch, print the reject message and fail the job
60+
61+ echo " ⛔️ Atleast one commit messages OR your branch name must include a JIRA ticket number e.g. \" AAA-1234\" . This can be anywhere in your commit"
62+ echo " You can skip this whole step if necessary by running \" git commit --amend\" and add \" [skip jira]\" in your last commit message, and force-push"
63+ echo " Please fix the commit message and push again."
64+ echo " https://help.github.com/en/articles/changing-a-commit-message"
65+ echo " ************"
66+ exit 1
0 commit comments