Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 01a6c61

Browse files
committedFeb 8, 2022
feat(rule): add FooterEnum rule
1 parent f6b07e2 commit 01a6c61

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
 

‎config/default.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ func Default() *lint.Config {
146146
Severity: lint.SeverityError,
147147
Argument: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/,",
148148
},
149+
150+
// Footer Enum Rule
151+
(&rule.FooterEnumRule{}).Name(): {
152+
Enabled: false,
153+
Severity: lint.SeverityError,
154+
Argument: []interface{}{},
155+
},
149156
},
150157
}
151158
return def

‎internal/registry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func newRegistry() *registry {
5151
&rule.BodyMinLenRule{}, &rule.BodyMaxLenRule{},
5252
&rule.FooterMinLenRule{}, &rule.FooterMaxLenRule{},
5353
&rule.HeadMaxLenRule{}, &rule.HeadMinLenRule{},
54-
&rule.TypeEnumRule{}, &rule.ScopeEnumRule{},
55-
5654
&rule.BodyMaxLineLenRule{}, &rule.FooterMaxLineLenRule{},
55+
56+
&rule.TypeEnumRule{}, &rule.ScopeEnumRule{}, &rule.FooterEnumRule{},
5757
&rule.TypeCharsetRule{}, &rule.ScopeCharsetRule{},
5858

5959
&rule.TypeMaxLenRule{}, &rule.ScopeMaxLenRule{}, &rule.DescriptionMaxLenRule{},

‎rule/enum.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,43 @@ func (r *TypeEnumRule) Apply(arg interface{}, flags map[string]interface{}) erro
8484
return nil
8585
}
8686

87+
// FooterEnumRule to validate footer tokens
88+
type FooterEnumRule struct {
89+
Tokens []string
90+
}
91+
92+
// Name return name of the rule
93+
func (r *FooterEnumRule) Name() string { return "footer-enum" }
94+
95+
// Validate validates FooterEnumRule
96+
func (r *FooterEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
97+
msgs := []string{}
98+
99+
for _, note := range msg.Notes() {
100+
isFound := search(r.Tokens, note.Token())
101+
if !isFound {
102+
errMsg := fmt.Sprintf("footer token '%s' is not allowed, you can use one of %v", note.Token(), r.Tokens)
103+
msgs = append(msgs, errMsg)
104+
}
105+
}
106+
107+
if len(msgs) == 0 {
108+
return nil, true
109+
}
110+
return msgs, false
111+
}
112+
113+
// Apply sets the needed argument for the rule
114+
func (r *FooterEnumRule) Apply(arg interface{}, flags map[string]interface{}) error {
115+
err := setStringArrArg(&r.Tokens, arg)
116+
if err != nil {
117+
return errInvalidArg(r.Name(), err)
118+
}
119+
// sorting the string elements for binary search
120+
sort.Strings(r.Tokens)
121+
return nil
122+
}
123+
87124
func search(arr []string, toFind string) bool {
88125
ind := sort.Search(len(arr), func(i int) bool {
89126
return arr[i] >= toFind

0 commit comments

Comments
 (0)
Please sign in to comment.