Skip to content

Commit cf974b9

Browse files
chore(validator): enforce JIRA references
1 parent 98881cf commit cf974b9

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
192192
- [x] enforce the commit scope
193193
- [x] enforce the commit subject
194194
- [x] enforce the commit body length
195-
- [ ] enforce the JIRA reference
195+
- [x] enforce the JIRA reference
196196
- [ ] enforce the BROKEN part
197197
- [ ] avoid trailing space
198198
- [ ] allow automated revert commit

validator.bats

+36
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,31 @@ LUM-2345'
395395
[[ "$status" -eq 0 ]]
396396
}
397397

398+
@test "features and fixes commits need jira reference" {
399+
[[ `need_jira "feat"` -eq 1 ]]
400+
[[ `need_jira "fix"` -eq 1 ]]
401+
}
402+
403+
@test "other commits don't need jira reference" {
404+
[[ `need_jira "docs"` -eq 0 ]]
405+
[[ `need_jira "test"` -eq 0 ]]
406+
}
407+
408+
@test "feat without jira ref should be rejected" {
409+
run validate_jira "feat" ""
410+
[[ "$status" -eq $ERROR_JIRA ]]
411+
}
412+
413+
@test "lint without jira ref should be validated" {
414+
run validate_jira "lint" ""
415+
[[ "$status" -eq 0 ]]
416+
}
417+
418+
@test "feat with jira ref should be validated" {
419+
run validate_jira "feat" "ABC-123"
420+
[[ "$status" -eq 0 ]]
421+
}
422+
398423
@test "overall validation invalid structure" {
399424
MESSAGE='plop
400425
plop'
@@ -461,6 +486,17 @@ LUM-2345'
461486
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
462487
}
463488

489+
@test "overall validation missing jira" {
490+
MESSAGE='feat(scope1): subject
491+
492+
Commit about stuff\"plop \"
493+
494+
2345'
495+
496+
run validate "$MESSAGE"
497+
[[ "$status" -eq $ERROR_JIRA ]]
498+
}
499+
464500
@test "overall validation" {
465501
MESSAGE='feat(scope1): subject
466502

validator.sh

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ readonly ERROR_TYPE=4
1414
readonly ERROR_SCOPE=5
1515
readonly ERROR_SUBJECT=6
1616
readonly ERROR_BODY_LENGTH=7
17+
readonly ERROR_JIRA=8
1718

1819
GLOBAL_HEADER=""
1920
GLOBAL_BODY=""
@@ -176,6 +177,31 @@ validate_body_length() {
176177
done <<< "$BODY"
177178
}
178179

180+
need_jira() {
181+
local TYPE=$1
182+
183+
case $TYPE in
184+
feat)
185+
echo 1
186+
;;
187+
fix)
188+
echo 1
189+
;;
190+
*)
191+
echo 0
192+
esac
193+
}
194+
195+
validate_jira() {
196+
local TYPE=$1
197+
local JIRA=$2
198+
199+
if [[ `need_jira "$TYPE"` -eq 1 && $JIRA = "" ]]; then
200+
echo -e "${TYPE} need a jira reference"
201+
exit $ERROR_JIRA
202+
fi
203+
}
204+
179205
validate() {
180206
local COMMIT_MSG="$1"
181207

@@ -198,4 +224,6 @@ validate() {
198224
validate_subject "$SUBJECT"
199225

200226
validate_body_length "$BODY"
227+
228+
validate_jira "$TYPE" "$JIRA"
201229
}

0 commit comments

Comments
 (0)