Skip to content

Commit 98881cf

Browse files
chore(validator): enforce body length
1 parent 9cbdc58 commit 98881cf

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,13 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
188188
- [x] enforce the overall commit message structure
189189
- [x] enforce the overall commit header structure
190190
- [x] enforce the overall commit header lenght
191-
- [ ] enforce the commit body length
192-
- [ ] enforce the JIRA reference
193-
- [ ] enforce the BROKEN part
194191
- [x] enforce the commit type
195192
- [x] enforce the commit scope
196193
- [x] enforce the commit subject
194+
- [x] enforce the commit body length
195+
- [ ] enforce the JIRA reference
196+
- [ ] enforce the BROKEN part
197+
- [ ] avoid trailing space
197198
- [ ] allow automated revert commit
198199
- [ ] allow fixup! and squash! commit with an option
199200

validator.bats

+33
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,28 @@ BROKEN:
373373
[ "$status" -eq 0 ]
374374
}
375375

376+
@test "body with 101 line length should be rejected" {
377+
MESSAGE='
378+
12345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 01
379+
380+
LUM-2345'
381+
382+
run validate_body_length "$MESSAGE"
383+
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
384+
}
385+
@test "body with 100 line length should be valid" {
386+
MESSAGE='
387+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
388+
12345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 0
389+
390+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
391+
392+
LUM-2345'
393+
394+
run validate_body_length "$MESSAGE"
395+
[[ "$status" -eq 0 ]]
396+
}
397+
376398
@test "overall validation invalid structure" {
377399
MESSAGE='plop
378400
plop'
@@ -428,6 +450,17 @@ LUM-2345'
428450
[[ "$status" -eq $ERROR_SUBJECT ]]
429451
}
430452

453+
@test "overall validation invalid body length" {
454+
MESSAGE='feat(scope1): subject
455+
456+
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
457+
458+
LUM-2345'
459+
460+
run validate "$MESSAGE"
461+
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
462+
}
463+
431464
@test "overall validation" {
432465
MESSAGE='feat(scope1): subject
433466

validator.sh

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly ERROR_HEADER_LENGTH=3
1313
readonly ERROR_TYPE=4
1414
readonly ERROR_SCOPE=5
1515
readonly ERROR_SUBJECT=6
16+
readonly ERROR_BODY_LENGTH=7
1617

1718
GLOBAL_HEADER=""
1819
GLOBAL_BODY=""
@@ -160,6 +161,21 @@ validate_subject() {
160161
fi
161162
}
162163

164+
validate_body_length() {
165+
local BODY=$1
166+
local LINE=""
167+
168+
while IFS= read -r LINE ;
169+
do
170+
local LENGTH=`echo -n "$LINE" | wc -c`
171+
172+
if [ $LENGTH -gt 100 ]; then
173+
echo -e "body message line length is more than 100 charaters"
174+
exit $ERROR_BODY_LENGTH
175+
fi
176+
done <<< "$BODY"
177+
}
178+
163179
validate() {
164180
local COMMIT_MSG="$1"
165181

@@ -180,4 +196,6 @@ validate() {
180196
validate_type "$TYPE"
181197
validate_scope "$SCOPE"
182198
validate_subject "$SUBJECT"
199+
200+
validate_body_length "$BODY"
183201
}

0 commit comments

Comments
 (0)