Skip to content

Commit

Permalink
fix: check empty commit-msg prior to parsing (#118)
Browse files Browse the repository at this point in the history
Signed-off-by: Don Bowman <[email protected]>
  • Loading branch information
donbowman authored and andrewrynhard committed Mar 12, 2019
1 parent 1473b44 commit 37e0e69
Show file tree
Hide file tree
Showing 2 changed files with 51 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
33 changes: 33 additions & 0 deletions internal/policy/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ func TestInvalidConventionalCommitPolicy(t *testing.T) {
}
}

func TestEmptyConventionalCommitPolicy(t *testing.T) {
dir, err := ioutil.TempDir("", "test")
if err != nil {
log.Fatal(err)
}
defer RemoveAll(dir)
err = os.Chdir(dir)
if err != nil {
t.Error(err)
}
err = initRepo()
if err != nil {
t.Error(err)
}
err = createEmptyCommit()
if err != nil {
t.Error(err)
}
report, err := runCompliance()
if err != nil {
t.Error(err)
}
if report.Valid() {
t.Error("Report is valid with invalid conventional commit")
}
}

func runCompliance() (*policy.Report, error) {
c := &Commit{
Conventional: &Conventional{
Expand Down Expand Up @@ -113,3 +140,9 @@ func createInvalidCommit() error {

return err
}

func createEmptyCommit() error {
_, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='[email protected]'", "commit", "--allow-empty-message", "-m", "").Output()

return err
}

0 comments on commit 37e0e69

Please sign in to comment.