Skip to content

Commit 4a88507

Browse files
refactor(lint)!: add Commit interface
1 parent 03ff16b commit 4a88507

File tree

8 files changed

+80
-34
lines changed

8 files changed

+80
-34
lines changed

lint/lint.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package lint
22

3-
import (
4-
"github.com/conventionalcommit/parser"
5-
)
6-
73
// Rule Severity Constants
84
const (
95
SeverityWarn Severity = "warn"
@@ -24,13 +20,28 @@ func (s Severity) String() string {
2420
}
2521
}
2622

23+
// Note represent a footer note
24+
type Note interface {
25+
Token() string
26+
Value() string
27+
}
28+
2729
// Commit represent a commit message
28-
// for now it is an alias of parser.Commit
29-
type Commit = parser.Commit
30+
type Commit interface {
31+
Message() string
32+
Header() string
33+
Body() string
34+
Footer() string
35+
Type() string
36+
Scope() string
37+
Description() string
38+
Notes() []Note
39+
IsBreakingChange() bool
40+
}
3041

3142
// Parser parses given commit message
3243
type Parser interface {
33-
Parse(msg string) (*Commit, error)
44+
Parse(msg string) (Commit, error)
3445
}
3546

3647
// Formatter represent a lint result formatter
@@ -55,5 +66,5 @@ type Rule interface {
5566
// Validate validates the rule for given commit message
5667
// if given commit is valid, return true and messages slice are ignored
5768
// if invalid, return a error messages with false
58-
Validate(msg *Commit) (messages []string, isValid bool)
69+
Validate(msg Commit) (messages []string, isValid bool)
5970
}

lint/linter.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Package lint provides a simple linter for conventional commits
22
package lint
33

4-
import (
5-
"github.com/conventionalcommit/parser"
6-
)
7-
84
// Linter is linter for commit message
95
type Linter struct {
106
conf *Config
@@ -18,7 +14,7 @@ func New(conf *Config, rules []Rule) (*Linter, error) {
1814
l := &Linter{
1915
conf: conf,
2016
rules: rules,
21-
parser: parser.New(),
17+
parser: newParser(),
2218
}
2319
return l, nil
2420
}
@@ -33,7 +29,7 @@ func (l *Linter) Lint(commitMsg string) (*Failure, error) {
3329
}
3430

3531
// LintCommit checks the given Commit against rules
36-
func (l *Linter) LintCommit(msg *Commit) (*Failure, error) {
32+
func (l *Linter) LintCommit(msg Commit) (*Failure, error) {
3733
res := newFailure(msg.Message())
3834

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

51-
func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailure, bool) {
47+
func (l *Linter) runRule(rule Rule, severity Severity, msg Commit) (*RuleFailure, bool) {
5248
failMsgs, isOK := rule.Validate(msg)
5349
if isOK {
5450
return nil, true

lint/parser.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lint
2+
3+
import "github.com/conventionalcommit/parser"
4+
5+
type defaultParser struct {
6+
p *parser.Parser
7+
}
8+
9+
func newParser() *defaultParser {
10+
return &defaultParser{
11+
p: parser.New(),
12+
}
13+
}
14+
15+
func (p defaultParser) Parse(input string) (Commit, error) {
16+
c, err := p.p.Parse(input)
17+
if err != nil {
18+
return nil, err
19+
}
20+
wrapC := &defaultCommit{
21+
Commit: c,
22+
}
23+
return wrapC, nil
24+
}
25+
26+
type defaultCommit struct {
27+
*parser.Commit
28+
}
29+
30+
func (d *defaultCommit) Notes() []Note {
31+
outNotes := d.Commit.Notes()
32+
notes := make([]Note, len(outNotes))
33+
34+
for i := 0; i < len(outNotes); i++ {
35+
notes[i] = &outNotes[i]
36+
}
37+
38+
return notes
39+
}

rule/charset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ScopeCharsetRule struct {
1616
func (r *ScopeCharsetRule) Name() string { return "scope-charset" }
1717

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

4545
// Validate validates TypeCharsetRule
46-
func (r *TypeCharsetRule) Validate(msg *lint.Commit) ([]string, bool) {
46+
func (r *TypeCharsetRule) Validate(msg lint.Commit) ([]string, bool) {
4747
invalidChar, isValid := checkCharset(r.Charset, msg.Type())
4848
if !isValid {
4949
errMsg := fmt.Sprintf("type contains invalid char '%s', allowed chars are [%s]", invalidChar, r.Charset)

rule/enum.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ScopeEnumRule struct {
1818
func (r *ScopeEnumRule) Name() string { return "scope-enum" }
1919

2020
// Validate validates ScopeEnumRule
21-
func (r *ScopeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
21+
func (r *ScopeEnumRule) Validate(msg lint.Commit) ([]string, bool) {
2222
if msg.Scope() == "" {
2323
if r.AllowEmpty {
2424
return nil, true
@@ -64,7 +64,7 @@ type TypeEnumRule struct {
6464
func (r *TypeEnumRule) Name() string { return "type-enum" }
6565

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

9595
// Validate validates FooterEnumRule
96-
func (r *FooterEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
96+
func (r *FooterEnumRule) Validate(msg lint.Commit) ([]string, bool) {
9797
msgs := []string{}
9898

9999
for _, note := range msg.Notes() {

rule/max_length.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HeadMaxLenRule struct {
1515
func (r *HeadMaxLenRule) Name() string { return "header-max-length" }
1616

1717
// Validate validates HeadMaxLenRule
18-
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
18+
func (r *HeadMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
1919
return checkMaxLen(r.CheckLen, msg.Header())
2020
}
2121

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

3939
// Validate validates BodyMaxLenRule
40-
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
40+
func (r *BodyMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
4141
return checkMaxLen(r.CheckLen, msg.Body())
4242
}
4343

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

6161
// Validate validates FooterMaxLenRule
62-
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
62+
func (r *FooterMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
6363
return checkMaxLen(r.CheckLen, msg.Footer())
6464
}
6565

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

8383
// Validate validates TypeMaxLenRule
84-
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
84+
func (r *TypeMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
8585
return checkMaxLen(r.CheckLen, msg.Type())
8686
}
8787

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

105105
// Validate validates ScopeMaxLenRule
106-
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
106+
func (r *ScopeMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
107107
return checkMaxLen(r.CheckLen, msg.Scope())
108108
}
109109

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

127127
// Validate validates DescriptionMaxLenRule
128-
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
128+
func (r *DescriptionMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
129129
return checkMaxLen(r.CheckLen, msg.Description())
130130
}
131131

rule/max_line_length.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type BodyMaxLineLenRule struct {
1616
func (r *BodyMaxLineLenRule) Name() string { return "body-max-line-length" }
1717

1818
// Validate validates BodyMaxLineLenRule rule
19-
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
19+
func (r *BodyMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
2020
return checkMaxLineLength(r.CheckLen, msg.Body())
2121
}
2222

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

4040
// Validate validates FooterMaxLineLenRule rule
41-
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
41+
func (r *FooterMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
4242
return checkMaxLineLength(r.CheckLen, msg.Footer())
4343
}
4444

rule/min_length.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HeadMinLenRule struct {
1515
func (r *HeadMinLenRule) Name() string { return "header-min-length" }
1616

1717
// Validate validates HeadMinLenRule
18-
func (r *HeadMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
18+
func (r *HeadMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
1919
return checkMinLen(r.CheckLen, msg.Header())
2020
}
2121

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

3939
// Validate validates BodyMinLenRule
40-
func (r *BodyMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
40+
func (r *BodyMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
4141
return checkMinLen(r.CheckLen, msg.Body())
4242
}
4343

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

6161
// Validate validates FooterMinLenRule
62-
func (r *FooterMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
62+
func (r *FooterMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
6363
return checkMinLen(r.CheckLen, msg.Footer())
6464
}
6565

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

8383
// Validate validates TypeMinLenRule
84-
func (r *TypeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
84+
func (r *TypeMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
8585
return checkMinLen(r.CheckLen, msg.Type())
8686
}
8787

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

105105
// Validate validates ScopeMinLenRule
106-
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
106+
func (r *ScopeMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
107107
return checkMinLen(r.CheckLen, msg.Scope())
108108
}
109109

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

127127
// Validate validates DescriptionMinLenRule
128-
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
128+
func (r *DescriptionMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
129129
return checkMinLen(r.CheckLen, msg.Description())
130130
}
131131

0 commit comments

Comments
 (0)