Skip to content

Commit

Permalink
feat: add commit body check
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard committed Jul 4, 2019
1 parent e66888a commit adce353
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
64 changes: 64 additions & 0 deletions internal/policy/commit/check_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package commit

import (
"strings"

"github.com/autonomy/conform/internal/policy"
"github.com/pkg/errors"
)

// RequiredBodyThreshold is the default minimum number of line changes required
// to trigger the body check.
var RequiredBodyThreshold = 10

// Body enforces a maximum number of charcters on the commit
// header.
type Body struct {
errors []error
}

// Name returns the name of the check.
func (h Body) Name() string {
return "Commit Body"
}

// Message returns to check message.
func (h Body) Message() string {
return "Commit body is valid"
}

// Errors returns any violations of the check.
func (h Body) Errors() []error {
return h.errors
}

// ValidateBody checks the header length.
func (c Commit) ValidateBody() policy.Check {
check := &Body{}

if c.HeaderLength != 0 {
MaxNumberOfCommitCharacters = c.HeaderLength
}

lines := strings.Split(strings.TrimPrefix(c.msg, "\n"), "\n")
valid := false
for _, line := range lines[1:] {
if DCORegex.MatchString(strings.TrimSpace(line)) {
continue
}
if line != "" {
valid = true
break
}
}

if !valid {
check.errors = append(check.errors, errors.New("Commit body is empty"))
}

return check
}
6 changes: 6 additions & 0 deletions internal/policy/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Commit struct {
// MaximumOfOneCommit enforces that the current commit is only one commit
// ahead of a specified ref.
MaximumOfOneCommit bool `mapstructure:"maximumOfOneCommit"`
// RequireCommitBody enforces that the current commit has a body.
RequireCommitBody bool `mapstructure:"requireCommitBody"`
// Conventional is the user specified settings for conventional commits.
Conventional *Conventional `mapstructure:"conventional"`

Expand Down Expand Up @@ -89,6 +91,10 @@ func (c *Commit) Compliance(options *policy.Options) (*policy.Report, error) {
report.AddCheck(c.ValidateNumberOfCommits(g, "refs/heads/master"))
}

if c.RequireCommitBody {
report.AddCheck(c.ValidateBody())
}

return report, nil
}

Expand Down

0 comments on commit adce353

Please sign in to comment.