diff --git a/internal/policy/commit/check_body.go b/internal/policy/commit/check_body.go new file mode 100644 index 00000000..a462a319 --- /dev/null +++ b/internal/policy/commit/check_body.go @@ -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 +} diff --git a/internal/policy/commit/commit.go b/internal/policy/commit/commit.go index f9d7d171..1640ae90 100644 --- a/internal/policy/commit/commit.go +++ b/internal/policy/commit/commit.go @@ -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"` @@ -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 }