Skip to content

Commit d4e4973

Browse files
Merge pull request #18 from lumapps/feat/allow_for_more_options
Feat/allow for more options
2 parents 833c0d2 + 0579955 commit d4e4973

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ Then run `pre-commit install --hook-type commit-msg` to install the
311311
- if `allow-temp` is set, no validation is done on `fixup!` and `squash!`
312312
commits.
313313
- if `no-revert-sha1` is set, no validation is done on revert commits.
314+
- if `--jira-in-header` jira reference can be put in the commit header.
315+
- `--header-length` allow to override the max length of the header line.
316+
- `--jira-types` takes a space separated list `"feat fix"` as a parameter to override the default types requiring a jira
314317

315318
<!-- ROADMAP -->
316319

check_message.sh

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
set -eu
44

5-
OPTIONS=$(getopt --long no-jira allow-temp -- "$@")
6-
[ $? -eq 0 ] || {
7-
echo "Incorrect options provided"
8-
exit 1
9-
}
5+
OPTIONS=$(getopt --long no-jira,allow-temp,jira-in-header,header-length:,jira-types: -- "$@")
106

11-
COMMIT_VALIDATOR_ALLOW_TEMP=
12-
COMMIT_VALIDATOR_NO_JIRA=
13-
COMMIT_VALIDATOR_NO_REVERT_SHA1=
7+
unset COMMIT_VALIDATOR_ALLOW_TEMP COMMIT_VALIDATOR_NO_JIRA COMMIT_VALIDATOR_NO_REVERT_SHA1 GLOBAL_JIRA_IN_HEADER GLOBAL_MAX_LENGTH GLOBAL_JIRA_TYPES
148

9+
eval set -- $OPTIONS
1510
while true; do
1611
case "$1" in
1712
--no-jira ) COMMIT_VALIDATOR_NO_JIRA=1; shift ;;
1813
--allow-temp ) COMMIT_VALIDATOR_ALLOW_TEMP=1; shift ;;
1914
--no-revert-sha1 ) COMMIT_VALIDATOR_NO_REVERT_SHA1=1; shift ;;
15+
--jira-in-header ) GLOBAL_JIRA_IN_HEADER=1; shift ;;
16+
--header-length ) GLOBAL_MAX_LENGTH="$2"; shift 2 ;;
17+
--jira-types ) GLOBAL_JIRA_TYPES="$2"; shift 2 ;;
2018
-- ) shift; break ;;
2119
* ) break ;;
2220
esac
@@ -47,9 +45,9 @@ fi
4745
# print message so you don't lose it in case of errors
4846
# (in case you are not using `-m` option)
4947
echo "Options: "
50-
echo " JIRA=$COMMIT_VALIDATOR_NO_JIRA"
51-
echo " TEMP=$COMMIT_VALIDATOR_ALLOW_TEMP"
52-
echo " NO_REVERT_SHA1=$COMMIT_VALIDATOR_NO_REVERT_SHA1"
48+
echo " JIRA=${COMMIT_VALIDATOR_NO_JIRA:-}"
49+
echo " TEMP=${COMMIT_VALIDATOR_ALLOW_TEMP:-}"
50+
echo " NO_REVERT_SHA1=${COMMIT_VALIDATOR_NO_REVERT_SHA1:-}"
5351
printf "checking commit message:\n\n#BEGIN#\n%s\n#END#\n\n" "$MESSAGE"
5452

5553
validate "$MESSAGE"

validator.bats

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ ABC-1234"
9797
[[ $GLOBAL_FOOTER == "" ]]
9898
}
9999

100+
@test "structure: valid commit message with JIRA in header" {
101+
COMMIT="feat(abc): ABC-1234
102+
103+
plop"
104+
105+
GLOBAL_JIRA_IN_HEADER="allow" validate_overall_structure "$COMMIT"
106+
[[ $GLOBAL_HEADER == "feat(abc): ABC-1234" ]]
107+
[[ $GLOBAL_JIRA == "ABC-1234" ]]
108+
[[ $GLOBAL_BODY == "plop"$'\n' ]]
109+
[[ $GLOBAL_FOOTER == "" ]]
110+
}
111+
100112
@test "structure: valid commit message with header and multiple JIRA" {
101113
COMMIT="plop plop
102114
@@ -328,6 +340,10 @@ BROKEN:
328340
[ "$status" -eq $ERROR_HEADER_LENGTH ]
329341
}
330342

343+
@test "header length cannot be more than 150 with spaces. overriden" {
344+
GLOBAL_MAX_LENGTH=150 validate_header_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 1"
345+
}
346+
331347
@test "header length can be 70" {
332348
run validate_header_length "0123456789012345678901234567890123456789012345678901234567890123456789"
333349
[ "$status" -eq 0 ]
@@ -499,23 +515,23 @@ LUM-2345'
499515
}
500516

501517
@test "features and fixes commits need jira reference" {
502-
[[ `need_jira "feat"` -eq 1 ]]
503-
[[ `need_jira "fix"` -eq 1 ]]
518+
need_jira "feat"
519+
need_jira "fix"
504520
}
505521

506522
@test "features and fixes commits need jira reference if env empty" {
507-
[[ `COMMIT_VALIDATOR_NO_JIRA= need_jira "feat"` -eq 1 ]]
508-
[[ `COMMIT_VALIDATOR_NO_JIRA= need_jira "fix"` -eq 1 ]]
523+
COMMIT_VALIDATOR_NO_JIRA= need_jira "feat"
524+
COMMIT_VALIDATOR_NO_JIRA= need_jira "fix"
509525
}
510526

511527
@test "features and fixes commits don't need jira reference if env non empty" {
512-
[[ `COMMIT_VALIDATOR_NO_JIRA=1 need_jira "feat"` -eq 0 ]]
513-
[[ `COMMIT_VALIDATOR_NO_JIRA=1 need_jira "fix"` -eq 0 ]]
528+
! COMMIT_VALIDATOR_NO_JIRA=1 need_jira "feat"
529+
! COMMIT_VALIDATOR_NO_JIRA=1 need_jira "fix"
514530
}
515531

516532
@test "other commits don't need jira reference" {
517-
[[ `need_jira "docs"` -eq 0 ]]
518-
[[ `need_jira "test"` -eq 0 ]]
533+
! need_jira "docs"
534+
! need_jira "test"
519535
}
520536

521537
@test "feat without jira ref should be rejected" {

validator.sh

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ if [[ -v ZSH_NAME ]]; then
55
fi
66

77
readonly HEADER_PATTERN="^([^\(]+)\(([^\)]+)\): (.+)$"
8-
readonly TYPE_PATTERN="^(feat|fix|docs|lint|refactor|test|chore)$"
8+
readonly TYPE_PATTERN="^(feat|fix|docs|gen|lint|refactor|test|chore)$"
99
readonly SCOPE_PATTERN="^([a-z][a-z0-9]*)(-[a-z0-9]+)*$"
1010
readonly SUBJECT_PATTERN="^([a-z0-9].*[^ ^\.])$"
11-
readonly JIRA_PATTERN="^([A-Z]{2,4}-[0-9]{1,6} ?)+$"
11+
readonly JIRA_PATTERN="^([A-Z]{3,4}-[0-9]{1,6} ?)+$"
12+
readonly JIRA_HEADER_PATTERN="^.*([A-Z]{3,4}-[0-9]{1,6}).*$"
1213
readonly BROKE_PATTERN="^BROKEN:$"
1314
readonly TRAILING_SPACE_PATTERN=" +$"
1415
readonly REVERT_HEADER_PATTERN="^[R|r]evert[: ].*$"
@@ -31,6 +32,11 @@ GLOBAL_BODY=""
3132
GLOBAL_JIRA=""
3233
GLOBAL_FOOTER=""
3334

35+
# Overridable variables
36+
GLOBAL_JIRA_TYPES="${GLOBAL_JIRA_TYPES:-feat fix}"
37+
GLOBAL_MAX_LENGTH="${GLOBAL_MAX_LENGTH:-70}"
38+
GLOBAL_JIRA_IN_HEADER="${GLOBAL_JIRA_IN_HEADER:-}"
39+
3440
GLOBAL_TYPE=""
3541
GLOBAL_SCOPE=""
3642
GLOBAL_SUBJECT=""
@@ -52,6 +58,9 @@ validate_overall_structure() {
5258
if [[ $STATE -eq $WAITING_HEADER ]]; then
5359
GLOBAL_HEADER="$LINE"
5460
STATE="$WAITING_EMPTY"
61+
if [[ -n "${GLOBAL_JIRA_IN_HEADER:-}" ]] && [[ $LINE =~ $JIRA_HEADER_PATTERN ]]; then
62+
GLOBAL_JIRA=${BASH_REMATCH[1]}
63+
fi
5564

5665
elif [[ $STATE -eq $WAITING_EMPTY ]]; then
5766
if [[ $LINE != "" ]]; then
@@ -141,12 +150,8 @@ validate_header() {
141150

142151
validate_header_length() {
143152
local HEADER="$1"
144-
local LENGTH
145-
146-
LENGTH="$(echo -n "$HEADER" | wc -c)"
147-
148-
if [[ $LENGTH -gt 70 ]]; then
149-
echo -e "commit header length is more than 70 charaters"
153+
if [[ ${#HEADER} -gt ${GLOBAL_MAX_LENGTH} ]]; then
154+
echo -e "commit header length is more than ${GLOBAL_MAX_LENGTH} characters"
150155
exit $ERROR_HEADER_LENGTH
151156
fi
152157
}
@@ -212,26 +217,24 @@ need_jira() {
212217
local TYPE=$1
213218

214219
if [[ ! -z "${COMMIT_VALIDATOR_NO_JIRA:-}" ]]; then
215-
echo 0
220+
return 1
216221
else
217-
case $TYPE in
218-
feat)
219-
echo 1
220-
;;
221-
fix)
222-
echo 1
223-
;;
224-
*)
225-
echo 0
226-
esac
222+
for type in ${GLOBAL_JIRA_TYPES}; do
223+
if [[ "${TYPE}" == "${type}" ]]; then
224+
return 0
225+
fi
226+
done
227+
return 1
227228
fi
228229
}
229230

230231
validate_jira() {
231232
local TYPE=$1
232233
local JIRA=$2
233234

234-
if [[ "$(need_jira "$TYPE")" -eq "1" && -z "${JIRA:-}" ]]; then
235+
236+
237+
if need_jira "$TYPE" && [[ -z "${JIRA:-}" ]]; then
235238
echo -e "commits with type '${TYPE}' need to include a reference to a JIRA ticket, by adding the project prefix and the issue number to the commit message, this could be done easily with: git commit -m 'feat(widget): add a wonderful widget' -m LUM-1234"
236239
exit $ERROR_JIRA
237240
fi

0 commit comments

Comments
 (0)