|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/conventionalcommit/commitlint/lint" |
| 11 | +) |
| 12 | + |
| 13 | +var _ lint.Rule = (*FooterTypeEnumRule)(nil) |
| 14 | + |
| 15 | +// FooterTypeEnumRule to validate footer tokens |
| 16 | +type FooterTypeEnumRule struct { |
| 17 | + Params []*FooterTypeEnumParam |
| 18 | +} |
| 19 | + |
| 20 | +// FooterTypeEnumParam represent a single footer type param |
| 21 | +type FooterTypeEnumParam struct { |
| 22 | + Token string |
| 23 | + Types []string |
| 24 | + Values []string |
| 25 | +} |
| 26 | + |
| 27 | +// Name return name of the rule |
| 28 | +func (r *FooterTypeEnumRule) Name() string { return "footer-type-enum" } |
| 29 | + |
| 30 | +// Apply sets the needed argument for the rule |
| 31 | +func (r *FooterTypeEnumRule) Apply(setting lint.RuleSetting) error { |
| 32 | + confParams, ok := setting.Argument.([]interface{}) |
| 33 | + if !ok { |
| 34 | + return errInvalidArg(r.Name(), fmt.Errorf("expects array of params, but got %#v", setting.Argument)) |
| 35 | + } |
| 36 | + |
| 37 | + params := make([]*FooterTypeEnumParam, 0, len(confParams)) |
| 38 | + |
| 39 | + for index, p := range confParams { |
| 40 | + v, ok := p.(map[interface{}]interface{}) |
| 41 | + if !ok { |
| 42 | + return errInvalidArg(r.Name()+": params", fmt.Errorf("expects key-value object, but got %#v", p)) |
| 43 | + } |
| 44 | + |
| 45 | + param, err := processParam(r, v, index) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + params = append(params, param) |
| 50 | + } |
| 51 | + |
| 52 | + for _, p := range params { |
| 53 | + // sorting the string elements for binary search |
| 54 | + sort.Strings(p.Types) |
| 55 | + sort.Strings(p.Values) |
| 56 | + } |
| 57 | + |
| 58 | + r.Params = params |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func processParam(r lint.Rule, val map[interface{}]interface{}, index int) (*FooterTypeEnumParam, error) { |
| 63 | + tok, ok := val["token"] |
| 64 | + if !ok { |
| 65 | + return nil, errMissingArg(r.Name(), "token in param "+strconv.Itoa(index+1)) |
| 66 | + } |
| 67 | + |
| 68 | + types, ok := val["types"] |
| 69 | + if !ok { |
| 70 | + return nil, errMissingArg(r.Name(), "types in param "+strconv.Itoa(index+1)) |
| 71 | + } |
| 72 | + |
| 73 | + values, ok := val["values"] |
| 74 | + if !ok { |
| 75 | + return nil, errMissingArg(r.Name(), "values in param "+strconv.Itoa(index+1)) |
| 76 | + } |
| 77 | + |
| 78 | + param := &FooterTypeEnumParam{} |
| 79 | + |
| 80 | + err := setStringArg(¶m.Token, tok) |
| 81 | + if err != nil { |
| 82 | + return nil, errInvalidArg(r.Name()+": token", err) |
| 83 | + } |
| 84 | + |
| 85 | + err = setStringArrArg(¶m.Types, types) |
| 86 | + if err != nil { |
| 87 | + return nil, errInvalidArg(r.Name()+": types", err) |
| 88 | + } |
| 89 | + |
| 90 | + err = setStringArrArg(¶m.Values, values) |
| 91 | + if err != nil { |
| 92 | + return nil, errInvalidArg(r.Name()+": values", err) |
| 93 | + } |
| 94 | + |
| 95 | + // validate the arguments |
| 96 | + if param.Token == "" { |
| 97 | + return nil, errInvalidArg(r.Name(), errors.New("token cannot be empty in param "+strconv.Itoa(index+1))) |
| 98 | + } |
| 99 | + |
| 100 | + if len(param.Types) < 1 { |
| 101 | + return nil, errNeedAtleastOneArg(r.Name(), "types in param "+strconv.Itoa(index+1)) |
| 102 | + } |
| 103 | + |
| 104 | + if len(param.Values) < 1 { |
| 105 | + return nil, errNeedAtleastOneArg(r.Name(), "values in param "+strconv.Itoa(index+1)) |
| 106 | + } |
| 107 | + |
| 108 | + return param, nil |
| 109 | +} |
| 110 | + |
| 111 | +// Validate validates FooterTypeEnumRule |
| 112 | +func (r *FooterTypeEnumRule) Validate(msg lint.Commit) (*lint.Issue, bool) { |
| 113 | + var invalids []string |
| 114 | + |
| 115 | + // find missing footer notes |
| 116 | + for _, param := range r.Params { |
| 117 | + isType := search(param.Types, msg.Type()) |
| 118 | + if !isType { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + isNote := searchNote(msg.Notes(), param.Token) |
| 123 | + if !isNote { |
| 124 | + a := fmt.Sprintf("'%s' should exist for type '%s'", param.Token, msg.Type()) |
| 125 | + invalids = append(invalids, a) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +outer: |
| 130 | + for _, note := range msg.Notes() { |
| 131 | + for _, param := range r.Params { |
| 132 | + isType := search(param.Types, msg.Type()) |
| 133 | + if !isType { |
| 134 | + // not applicable for current type |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + if note.Token() != param.Token { |
| 139 | + // not applicable for current token |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + for _, val := range param.Values { |
| 144 | + if strings.HasPrefix(note.Value(), val) { |
| 145 | + // has valid prefix, check next footer note |
| 146 | + continue outer |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // invalid - matches non of the mentioned prefix |
| 151 | + a := fmt.Sprintf("'%s' should have one of prefix [%s]", note.Token(), strings.Join(param.Values, ", ")) |
| 152 | + invalids = append(invalids, a) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if len(invalids) == 0 { |
| 157 | + return nil, true |
| 158 | + } |
| 159 | + |
| 160 | + desc := "footer token is invalid" |
| 161 | + return lint.NewIssue(desc, invalids...), false |
| 162 | +} |
0 commit comments