From 9383d3ebab5b14c186fc84299c76cfa4d404a9c6 Mon Sep 17 00:00:00 2001 From: Andrew Rynhard Date: Sat, 22 Jul 2017 12:36:03 -0700 Subject: [PATCH] feat(policy): enforce 72 character limit on commit header (#29) --- .../policy/conventionalcommit/conventionalcommit.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/conform/policy/conventionalcommit/conventionalcommit.go b/conform/policy/conventionalcommit/conventionalcommit.go index 467cc57b..718e340b 100644 --- a/conform/policy/conventionalcommit/conventionalcommit.go +++ b/conform/policy/conventionalcommit/conventionalcommit.go @@ -18,6 +18,10 @@ type Conventional struct { Scopes []string `mapstructure:"scopes"` } +// MaxNumberOfCommitCharacters is the maximium number of characters allowed in +// a commit header. +const MaxNumberOfCommitCharacters = 72 + // HeaderRegex is the regular expression used for Conventional Commits // 1.0.0-beta.1. const HeaderRegex = `^(\w*)\(([^)]+)\):\s{1}(.*)($|\n{2})` @@ -41,6 +45,7 @@ func (c *Conventional) Compliance(metadata *metadata.Metadata, options ...policy report.Errors = append(report.Errors, fmt.Errorf("Invalid commit format")) return } + ValidateHeaderLength(&report, groups) ValidateType(&report, groups, c.Types) ValidateScope(&report, groups, c.Scopes) ValidateDescription(&report, groups) @@ -58,6 +63,13 @@ func (c *Conventional) Tasks(map[string]*task.Task) policy.Option { return func(args *policy.Options) {} } +// ValidateHeaderLength checks the header length. +func ValidateHeaderLength(report *policy.Report, groups []string) { + if len(groups[1]) > MaxNumberOfCommitCharacters { + report.Errors = append(report.Errors, fmt.Errorf("Commit header is %d characters", len(groups[1]))) + } +} + // ValidateType returns the commit type. func ValidateType(report *policy.Report, groups []string, types []string) { types = append(types, TypeFeat, TypeFix)