Skip to content

Commit

Permalink
refactor(lint)!: add Commit interface
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Feb 18, 2022
1 parent 03ff16b commit 4a88507
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 34 deletions.
27 changes: 19 additions & 8 deletions lint/lint.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package lint

import (
"github.com/conventionalcommit/parser"
)

// Rule Severity Constants
const (
SeverityWarn Severity = "warn"
Expand All @@ -24,13 +20,28 @@ func (s Severity) String() string {
}
}

// Note represent a footer note
type Note interface {
Token() string
Value() string
}

// Commit represent a commit message
// for now it is an alias of parser.Commit
type Commit = parser.Commit
type Commit interface {
Message() string
Header() string
Body() string
Footer() string
Type() string
Scope() string
Description() string
Notes() []Note
IsBreakingChange() bool
}

// Parser parses given commit message
type Parser interface {
Parse(msg string) (*Commit, error)
Parse(msg string) (Commit, error)
}

// Formatter represent a lint result formatter
Expand All @@ -55,5 +66,5 @@ type Rule interface {
// Validate validates the rule for given commit message
// if given commit is valid, return true and messages slice are ignored
// if invalid, return a error messages with false
Validate(msg *Commit) (messages []string, isValid bool)
Validate(msg Commit) (messages []string, isValid bool)
}
10 changes: 3 additions & 7 deletions lint/linter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Package lint provides a simple linter for conventional commits
package lint

import (
"github.com/conventionalcommit/parser"
)

// Linter is linter for commit message
type Linter struct {
conf *Config
Expand All @@ -18,7 +14,7 @@ func New(conf *Config, rules []Rule) (*Linter, error) {
l := &Linter{
conf: conf,
rules: rules,
parser: parser.New(),
parser: newParser(),
}
return l, nil
}
Expand All @@ -33,7 +29,7 @@ func (l *Linter) Lint(commitMsg string) (*Failure, error) {
}

// LintCommit checks the given Commit against rules
func (l *Linter) LintCommit(msg *Commit) (*Failure, error) {
func (l *Linter) LintCommit(msg Commit) (*Failure, error) {
res := newFailure(msg.Message())

for _, rule := range l.rules {
Expand All @@ -48,7 +44,7 @@ func (l *Linter) LintCommit(msg *Commit) (*Failure, error) {
return res, nil
}

func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailure, bool) {
func (l *Linter) runRule(rule Rule, severity Severity, msg Commit) (*RuleFailure, bool) {
failMsgs, isOK := rule.Validate(msg)
if isOK {
return nil, true
Expand Down
39 changes: 39 additions & 0 deletions lint/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lint

import "github.com/conventionalcommit/parser"

type defaultParser struct {
p *parser.Parser
}

func newParser() *defaultParser {
return &defaultParser{
p: parser.New(),
}
}

func (p defaultParser) Parse(input string) (Commit, error) {
c, err := p.p.Parse(input)
if err != nil {
return nil, err
}
wrapC := &defaultCommit{
Commit: c,
}
return wrapC, nil
}

type defaultCommit struct {
*parser.Commit
}

func (d *defaultCommit) Notes() []Note {
outNotes := d.Commit.Notes()
notes := make([]Note, len(outNotes))

for i := 0; i < len(outNotes); i++ {
notes[i] = &outNotes[i]
}

return notes
}
4 changes: 2 additions & 2 deletions rule/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ScopeCharsetRule struct {
func (r *ScopeCharsetRule) Name() string { return "scope-charset" }

// Validate validates ScopeCharsetRule
func (r *ScopeCharsetRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *ScopeCharsetRule) Validate(msg lint.Commit) ([]string, bool) {
invalidChar, isValid := checkCharset(r.Charset, msg.Scope())
if !isValid {
errMsg := fmt.Sprintf("scope contains invalid char '%s', allowed chars are [%s]", invalidChar, r.Charset)
Expand All @@ -43,7 +43,7 @@ type TypeCharsetRule struct {
func (r *TypeCharsetRule) Name() string { return "type-charset" }

// Validate validates TypeCharsetRule
func (r *TypeCharsetRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *TypeCharsetRule) Validate(msg lint.Commit) ([]string, bool) {
invalidChar, isValid := checkCharset(r.Charset, msg.Type())
if !isValid {
errMsg := fmt.Sprintf("type contains invalid char '%s', allowed chars are [%s]", invalidChar, r.Charset)
Expand Down
6 changes: 3 additions & 3 deletions rule/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ScopeEnumRule struct {
func (r *ScopeEnumRule) Name() string { return "scope-enum" }

// Validate validates ScopeEnumRule
func (r *ScopeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *ScopeEnumRule) Validate(msg lint.Commit) ([]string, bool) {
if msg.Scope() == "" {
if r.AllowEmpty {
return nil, true
Expand Down Expand Up @@ -64,7 +64,7 @@ type TypeEnumRule struct {
func (r *TypeEnumRule) Name() string { return "type-enum" }

// Validate validates TypeEnumRule
func (r *TypeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *TypeEnumRule) Validate(msg lint.Commit) ([]string, bool) {
isFound := search(r.Types, msg.Type())
if !isFound {
errMsg := fmt.Sprintf("type '%s' is not allowed, you can use one of %v", msg.Type(), r.Types)
Expand Down Expand Up @@ -93,7 +93,7 @@ type FooterEnumRule struct {
func (r *FooterEnumRule) Name() string { return "footer-enum" }

// Validate validates FooterEnumRule
func (r *FooterEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *FooterEnumRule) Validate(msg lint.Commit) ([]string, bool) {
msgs := []string{}

for _, note := range msg.Notes() {
Expand Down
12 changes: 6 additions & 6 deletions rule/max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HeadMaxLenRule struct {
func (r *HeadMaxLenRule) Name() string { return "header-max-length" }

// Validate validates HeadMaxLenRule
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *HeadMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header())
}

Expand All @@ -37,7 +37,7 @@ type BodyMaxLenRule struct {
func (r *BodyMaxLenRule) Name() string { return "body-max-length" }

// Validate validates BodyMaxLenRule
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *BodyMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Body())
}

Expand All @@ -59,7 +59,7 @@ type FooterMaxLenRule struct {
func (r *FooterMaxLenRule) Name() string { return "footer-max-length" }

// Validate validates FooterMaxLenRule
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *FooterMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Footer())
}

Expand All @@ -81,7 +81,7 @@ type TypeMaxLenRule struct {
func (r *TypeMaxLenRule) Name() string { return "type-max-length" }

// Validate validates TypeMaxLenRule
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *TypeMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Type())
}

Expand All @@ -103,7 +103,7 @@ type ScopeMaxLenRule struct {
func (r *ScopeMaxLenRule) Name() string { return "scope-max-length" }

// Validate validates ScopeMaxLenRule
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *ScopeMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Scope())
}

Expand All @@ -125,7 +125,7 @@ type DescriptionMaxLenRule struct {
func (r *DescriptionMaxLenRule) Name() string { return "description-max-length" }

// Validate validates DescriptionMaxLenRule
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *DescriptionMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Description())
}

Expand Down
4 changes: 2 additions & 2 deletions rule/max_line_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type BodyMaxLineLenRule struct {
func (r *BodyMaxLineLenRule) Name() string { return "body-max-line-length" }

// Validate validates BodyMaxLineLenRule rule
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *BodyMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Body())
}

Expand All @@ -38,7 +38,7 @@ type FooterMaxLineLenRule struct {
func (r *FooterMaxLineLenRule) Name() string { return "footer-max-line-length" }

// Validate validates FooterMaxLineLenRule rule
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *FooterMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Footer())
}

Expand Down
12 changes: 6 additions & 6 deletions rule/min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HeadMinLenRule struct {
func (r *HeadMinLenRule) Name() string { return "header-min-length" }

// Validate validates HeadMinLenRule
func (r *HeadMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *HeadMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Header())
}

Expand All @@ -37,7 +37,7 @@ type BodyMinLenRule struct {
func (r *BodyMinLenRule) Name() string { return "body-min-length" }

// Validate validates BodyMinLenRule
func (r *BodyMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *BodyMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Body())
}

Expand All @@ -59,7 +59,7 @@ type FooterMinLenRule struct {
func (r *FooterMinLenRule) Name() string { return "footer-min-length" }

// Validate validates FooterMinLenRule
func (r *FooterMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *FooterMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Footer())
}

Expand All @@ -81,7 +81,7 @@ type TypeMinLenRule struct {
func (r *TypeMinLenRule) Name() string { return "type-min-length" }

// Validate validates TypeMinLenRule
func (r *TypeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *TypeMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Type())
}

Expand All @@ -103,7 +103,7 @@ type ScopeMinLenRule struct {
func (r *ScopeMinLenRule) Name() string { return "scope-min-length" }

// Validate validates ScopeMinLenRule
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *ScopeMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Scope())
}

Expand All @@ -125,7 +125,7 @@ type DescriptionMinLenRule struct {
func (r *DescriptionMinLenRule) Name() string { return "description-min-length" }

// Validate validates DescriptionMinLenRule
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
func (r *DescriptionMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Description())
}

Expand Down

0 comments on commit 4a88507

Please sign in to comment.