Skip to content

Commit 345edb6

Browse files
feat: allow empty scope in ScopeEnumRule
1 parent ed9f3fa commit 345edb6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

rule/enum.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ import (
1010
// ScopeEnumRule to validate max length of header
1111
type ScopeEnumRule struct {
1212
Scopes []string
13+
14+
AllowEmpty bool
1315
}
1416

1517
// Name return name of the rule
1618
func (r *ScopeEnumRule) Name() string { return "scope-enum" }
1719

1820
// Validate validates ScopeEnumRule
1921
func (r *ScopeEnumRule) Validate(msg *message.Commit) (string, bool) {
22+
if msg.Header.Scope == "" {
23+
if r.AllowEmpty {
24+
return "", true
25+
}
26+
errMsg := fmt.Sprintf("empty scope is not allowed, you can use one of %v", r.Scopes)
27+
return errMsg, false
28+
}
29+
2030
isFound := search(r.Scopes, msg.Header.Scope)
2131
if !isFound {
2232
errMsg := fmt.Sprintf("scope '%s' is not allowed, you can use one of %v", msg.Header.Scope, r.Scopes)
@@ -31,6 +41,15 @@ func (r *ScopeEnumRule) Apply(arg interface{}, flags map[string]interface{}) err
3141
if err != nil {
3242
return err
3343
}
44+
45+
allowEmpty, ok := flags["allow-empty"]
46+
if ok {
47+
err := setBoolArg(&r.AllowEmpty, allowEmpty, r.Name())
48+
if err != nil {
49+
return err
50+
}
51+
}
52+
3453
// sorting the string elements for binary search
3554
sort.Strings(r.Scopes)
3655
return nil

rule/util.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import (
44
"fmt"
55
)
66

7+
func setBoolArg(retVal *bool, arg interface{}, ruleName string) error {
8+
boolVal, err := toBool(arg, ruleName)
9+
if err != nil {
10+
return err
11+
}
12+
*retVal = boolVal
13+
return nil
14+
}
15+
716
func setIntArg(retVal *int, arg interface{}, ruleName string) error {
817
intVal, err := toInt(arg, ruleName)
918
if err != nil {
@@ -31,6 +40,14 @@ func setStringArrArg(retVal *[]string, arg interface{}, ruleName string) error {
3140
return nil
3241
}
3342

43+
func toBool(arg interface{}, ruleName string) (bool, error) {
44+
boolVal, ok := arg.(bool)
45+
if !ok {
46+
return false, fmt.Errorf("%s expects bool value, but got %#v", ruleName, arg)
47+
}
48+
return boolVal, nil
49+
}
50+
3451
func toInt(arg interface{}, ruleName string) (int, error) {
3552
intVal, ok := arg.(int)
3653
if !ok {

0 commit comments

Comments
 (0)