Skip to content

Commit

Permalink
fix: check empty commit-msg prior to parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Don Bowman <db@donbowman.ca>
  • Loading branch information
donbowman committed Mar 9, 2019
1 parent 1473b44 commit 8f7730e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions internal/policy/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ func (c *Commit) Compliance(options *policy.Options) (report policy.Report) {
ValidateGPGSign(&report, g)
}

word := firstWord(msg)
var word string
if word, err = firstWord(msg, &report); err != nil {
return
}

if c.Conventional != nil {
groups := parseHeader(msg)
if len(groups) != 6 {
report.Errors = append(report.Errors, errors.Errorf("Invalid conventional commits format: %s", msg))
return
}
word = firstWord(groups[4])
if word, err = firstWord(groups[4], &report); err != nil {
return
}

ValidateType(&report, groups, c.Conventional.Types)
ValidateScope(&report, groups, c.Conventional.Scopes)
Expand Down Expand Up @@ -209,10 +214,17 @@ func ValidateDescription(report *policy.Report, groups []string) {
report.Errors = append(report.Errors, errors.Errorf("Invalid description: %s", groups[4]))
}

func firstWord(msg string) string {
header := strings.Split(strings.TrimPrefix(msg, "\n"), "\n")[0]
groups := FirstWordRegex.FindStringSubmatch(header)
return groups[0]
func firstWord(msg string, report *policy.Report) (string, error) {
var header string
var groups []string
if header = strings.Split(strings.TrimPrefix(msg, "\n"), "\n")[0]; header == "" {
report.Errors = append(report.Errors, errors.Errorf("Invalid conventional commits (empty)"))
return "", errors.Errorf("Invalid msg: %s", msg)
}
if groups = FirstWordRegex.FindStringSubmatch(header); groups == nil {
return "", errors.Errorf("Invalid msg: %s", msg)
}
return groups[0], nil
}

func parseHeader(msg string) []string {
Expand Down

0 comments on commit 8f7730e

Please sign in to comment.