Skip to content

Commit

Permalink
chore(validator): enforce JIRA references
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-boulle committed Aug 20, 2020
1 parent 98881cf commit cf974b9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
- [x] enforce the commit scope
- [x] enforce the commit subject
- [x] enforce the commit body length
- [ ] enforce the JIRA reference
- [x] enforce the JIRA reference
- [ ] enforce the BROKEN part
- [ ] avoid trailing space
- [ ] allow automated revert commit
Expand Down
36 changes: 36 additions & 0 deletions validator.bats
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,31 @@ LUM-2345'
[[ "$status" -eq 0 ]]
}

@test "features and fixes commits need jira reference" {
[[ `need_jira "feat"` -eq 1 ]]
[[ `need_jira "fix"` -eq 1 ]]
}

@test "other commits don't need jira reference" {
[[ `need_jira "docs"` -eq 0 ]]
[[ `need_jira "test"` -eq 0 ]]
}

@test "feat without jira ref should be rejected" {
run validate_jira "feat" ""
[[ "$status" -eq $ERROR_JIRA ]]
}

@test "lint without jira ref should be validated" {
run validate_jira "lint" ""
[[ "$status" -eq 0 ]]
}

@test "feat with jira ref should be validated" {
run validate_jira "feat" "ABC-123"
[[ "$status" -eq 0 ]]
}

@test "overall validation invalid structure" {
MESSAGE='plop
plop'
Expand Down Expand Up @@ -461,6 +486,17 @@ LUM-2345'
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
}

@test "overall validation missing jira" {
MESSAGE='feat(scope1): subject
Commit about stuff\"plop \"
2345'

run validate "$MESSAGE"
[[ "$status" -eq $ERROR_JIRA ]]
}

@test "overall validation" {
MESSAGE='feat(scope1): subject
Expand Down
28 changes: 28 additions & 0 deletions validator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readonly ERROR_TYPE=4
readonly ERROR_SCOPE=5
readonly ERROR_SUBJECT=6
readonly ERROR_BODY_LENGTH=7
readonly ERROR_JIRA=8

GLOBAL_HEADER=""
GLOBAL_BODY=""
Expand Down Expand Up @@ -176,6 +177,31 @@ validate_body_length() {
done <<< "$BODY"
}

need_jira() {
local TYPE=$1

case $TYPE in
feat)
echo 1
;;
fix)
echo 1
;;
*)
echo 0
esac
}

validate_jira() {
local TYPE=$1
local JIRA=$2

if [[ `need_jira "$TYPE"` -eq 1 && $JIRA = "" ]]; then
echo -e "${TYPE} need a jira reference"
exit $ERROR_JIRA
fi
}

validate() {
local COMMIT_MSG="$1"

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

validate_body_length "$BODY"

validate_jira "$TYPE" "$JIRA"
}

0 comments on commit cf974b9

Please sign in to comment.