refactor(validation): simplify regex and error messages

This commit is contained in:
Jan K9f 2025-04-09 18:37:58 +02:00
parent 372ed39a62
commit 08b43cc51b
Signed by: jank
GPG key ID: B9F475106B20F144

View file

@ -13,33 +13,26 @@ func ValidateConventionalCommit(commit string) error {
// type!: description
// or
// type: description
re := regexp.MustCompile(`^(?P<type>[a-z]+)(?P<scope>\([a-z]+\))?(?P<breaking>!)?: (?P<description>[a-z].+)$`)
re := regexp.MustCompile(`^(?P<type>[a-z]+)(?P<scope>\([a-z]+\))?(?P<breaking>!)?: (?P<description>.+)$`)
match := re.FindStringSubmatch(commit)
if len(match) == 0 {
return fmt.Errorf("Invalid PR title")
return fmt.Errorf("invalid commit format")
}
typeIndex := re.SubexpIndex("type")
scopeIndex := re.SubexpIndex("scope")
breakingIndex := re.SubexpIndex("breaking")
descriptionIndex := re.SubexpIndex("description")
commitType := match[typeIndex]
scope := match[scopeIndex]
breaking := match[breakingIndex]
description := match[descriptionIndex]
// Type MUST be lowercase
if commitType != strings.ToLower(commitType) {
return fmt.Errorf("type must be lowercase")
}
// Description MUST start with lowercase
if description != strings.ToLower(description) {
return fmt.Errorf("description must start with lowercase")
}
// Scope MUST be lowercase
if scope != "" && scope != strings.ToLower(scope) {
return fmt.Errorf("scope must be lowercase")