Skip to content

Commit

Permalink
fix(policy): update regex to allow optional scope (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Oct 4, 2018
1 parent 1933d19 commit aed2c22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ func New() (*Conform, error) {
// Enforce enforces all policies defined in the conform.yaml file.
func (c *Conform) Enforce() error {
for _, p := range c.Policies {
fmt.Printf("Enforcing policy %q: ", p.Type)
err := c.enforce(p)
if err != nil {
fmt.Printf("failed\n")
return err
}
fmt.Printf("passed\n")
}
err := c.Pipeline.Build(c.Metadata, c.Stages, c.Tasks)
if err != nil {
Expand Down
16 changes: 10 additions & 6 deletions pkg/policy/conventionalcommit/conventionalcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MaxNumberOfCommitCharacters = 72

// HeaderRegex is the regular expression used for Conventional Commits
// 1.0.0-beta.1.
const HeaderRegex = `^(\w*)\(([^)]+)\):\s{1}(.*)($|\n{2})`
const HeaderRegex = `^(\w*)(\(([^)]+)\))?:\s{1}(.*)($|\n{2})`

// TypeFeat is a commit of the type fix patches a bug in your codebase
// (this correlates with PATCH in semantic versioning).
Expand All @@ -41,7 +41,7 @@ func (c *Conventional) Compliance(metadata *metadata.Metadata, options ...policy
return
}
groups := parseHeader(metadata.Git.Message)
if len(groups) != 5 {
if len(groups) != 6 {
report.Errors = append(report.Errors, fmt.Errorf("Invalid commit format: %s", metadata.Git.Message))
return
}
Expand Down Expand Up @@ -83,20 +83,24 @@ func ValidateType(report *policy.Report, groups []string, types []string) {

// ValidateScope returns the commit scope.
func ValidateScope(report *policy.Report, groups []string, scopes []string) {
// Scope is optional.
if groups[3] == "" {
return
}
for _, scope := range scopes {
if scope == groups[2] {
if scope == groups[3] {
return
}
}
report.Errors = append(report.Errors, fmt.Errorf("Invalid scope: %s", groups[2]))
report.Errors = append(report.Errors, fmt.Errorf("Invalid scope: %s", groups[3]))
}

// ValidateDescription returns the commit description.
func ValidateDescription(report *policy.Report, groups []string) {
if len(groups[3]) <= 72 && len(groups[3]) != 0 {
if len(groups[4]) <= 72 && len(groups[4]) != 0 {
return
}
report.Errors = append(report.Errors, fmt.Errorf("Invalid description: %s", groups[3]))
report.Errors = append(report.Errors, fmt.Errorf("Invalid description: %s", groups[4]))
}

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

0 comments on commit aed2c22

Please sign in to comment.