Skip to content

Commit

Permalink
refactor(lint): add Description, Infos to Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Mar 6, 2022
1 parent e09d206 commit 37faae2
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 145 deletions.
10 changes: 5 additions & 5 deletions formatter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (f *DefaultFormatter) writeIssues(w *strings.Builder, sign, title string, i
func (f *DefaultFormatter) writeIssue(w *strings.Builder, sign string, issue *lint.Issue) {
space := " "

// ❌ rule-name:
// - message1
// - message2
// ❌ rule-name: description
// - info1
// - info2

fmt.Fprintf(w, "\n%s %s:", space+sign, issue.Name())
for _, msg := range issue.Message() {
fmt.Fprintf(w, "\n%s %s: %s", space+sign, issue.RuleName(), issue.Description())
for _, msg := range issue.Infos() {
fmt.Fprintf(w, "\n%s - %s", space+space, msg)
}
}
Expand Down
8 changes: 4 additions & 4 deletions formatter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (f *JSONFormatter) Format(result *lint.Result) (string, error) {
output := make(map[string]interface{}, 4)

output["input"] = result.Input()
output["valid"] = result.IsOK()
output["issues"] = f.formatIssue(result.Issues())

formatted, err := json.Marshal(output)
Expand All @@ -35,11 +34,12 @@ func (f *JSONFormatter) formatIssue(issues []*lint.Issue) []interface{} {
for _, issue := range issues {
output := make(map[string]interface{})

output["name"] = issue.Name()
output["name"] = issue.RuleName()
output["severity"] = issue.Severity()
output["description"] = issue.Description()

if len(issue.Message()) > 0 {
output["messages"] = issue.Message()
if len(issue.Infos()) > 0 {
output["infos"] = issue.Infos()
}

formattedIssues = append(formattedIssues, output)
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ func runLint(confFilePath, fileInput string) (lintResult string, hasError bool,
return "", false, err
}

res, err := linter.Lint(commitMsg)
result, err := linter.Lint(commitMsg)
if err != nil {
return "", false, err
}

resStr, err := format.Format(res)
output, err := format.Format(result)
if err != nil {
return "", false, err
}
return resStr, hasErrorSeverity(res), nil
return output, hasErrorSeverity(result), nil
}

func getLinter(confParam string) (*lint.Linter, lint.Formatter, error) {
Expand Down Expand Up @@ -136,9 +136,9 @@ func readStdInPipe() (string, error) {
return strings.TrimSpace(s), nil
}

func hasErrorSeverity(res *lint.Result) bool {
for _, r := range res.Issues() {
if r.Severity() == lint.SeverityError {
func hasErrorSeverity(result *lint.Result) bool {
for _, i := range result.Issues() {
if i.Severity() == lint.SeverityError {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,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) (issue *Issue, isValid bool)
}
35 changes: 17 additions & 18 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,42 @@ func New(conf *Config, rules []Rule) (*Linter, error) {
func (l *Linter) Lint(commitMsg string) (*Result, error) {
msg, err := l.parser.Parse(commitMsg)
if err != nil {
return l.parserErrorRule(commitMsg, err)
issues := l.parserErrorRule(commitMsg, err)
return newResult(commitMsg, issues...), nil
}
return l.LintCommit(msg)
}

// LintCommit checks the given Commit against rules
func (l *Linter) LintCommit(msg Commit) (*Result, error) {
res := newResult(msg.Message())
issues := make([]*Issue, 0, len(l.rules))

for _, rule := range l.rules {
currentRule := rule
severity := l.conf.GetSeverity(currentRule.Name())
ruleRes, isValid := l.runRule(currentRule, severity, msg)
issue, isValid := l.runRule(currentRule, severity, msg)
if !isValid {
res.add(ruleRes)
issues = append(issues, issue)
}
}

return res, nil
return newResult(msg.Message(), issues...), nil
}

func (l *Linter) runRule(rule Rule, severity Severity, msg Commit) (*Issue, bool) {
failMsgs, isOK := rule.Validate(msg)
if isOK {
issue, isValid := rule.Validate(msg)
if isValid {
return nil, true
}
res := newIssue(rule.Name(), failMsgs, severity)
return res, false
}

func (l *Linter) parserErrorRule(commitMsg string, err error) (*Result, error) {
res := newResult(commitMsg)

errMsg := err.Error()

ruleFail := newIssue("parser", []string{errMsg}, SeverityError)
res.add(ruleFail)
issue.ruleName = rule.Name()
issue.severity = severity
return issue, false
}

return res, nil
func (l *Linter) parserErrorRule(commitMsg string, err error) []*Issue {
issue := NewIssue(err.Error())
issue.ruleName = "parser"
issue.severity = SeverityError
return []*Issue{issue}
}
55 changes: 27 additions & 28 deletions lint/result.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
package lint

// Result holds Result of linter
// Result holds a linter result
type Result struct {
inputMsg string

input string
issues []*Issue
}

func newResult(inputMsg string) *Result {
func newResult(input string, issues ...*Issue) *Result {
return &Result{
inputMsg: inputMsg,
input: input,
issues: issues,
}
}

// AddError adds
func (res *Result) add(r *Issue) {
res.issues = append(res.issues, r)
}

// IsOK returns true if commit message passed all the rules
func (res *Result) IsOK() bool { return len(res.issues) == 0 }

// Input returns input commit message
func (res *Result) Input() string { return res.inputMsg }
// Input returns the input commit message
func (r *Result) Input() string { return r.input }

// Issues returns rule Issues
func (res *Result) Issues() []*Issue { return res.issues }
// Issues returns linter issues
func (r *Result) Issues() []*Issue { return r.issues }

// Issue holds Failure of a linter rule
// Issue holds a rule result
type Issue struct {
name string
ruleName string

severity Severity
messages []string

description string

additionalInfos []string
}

func newIssue(name string, msgs []string, severity Severity) *Issue {
// NewIssue returns a new issue
func NewIssue(desc string, infos ...string) *Issue {
return &Issue{
name: name,
messages: msgs,
severity: severity,
description: desc,
additionalInfos: infos,
}
}

// Name returns rule name
func (r *Issue) Name() string { return r.name }
// RuleName returns rule name
func (r *Issue) RuleName() string { return r.ruleName }

// Severity returns severity of the Rule Failure
func (r *Issue) Severity() Severity { return r.severity }

// Message returns the error messages of failed rule
func (r *Issue) Message() []string { return r.messages }
// Description returns description of the issue
func (r *Issue) Description() string { return r.description }

// Infos returns additional infos about the issue
func (r *Issue) Infos() []string { return r.additionalInfos }
4 changes: 2 additions & 2 deletions rule/body_max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *BodyMaxLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates BodyMaxLenRule
func (r *BodyMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Body())
func (r *BodyMaxLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLen("body", r.CheckLen, msg.Body())
}
4 changes: 2 additions & 2 deletions rule/body_max_line_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func (r *BodyMaxLineLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates BodyMaxLineLenRule rule
func (r *BodyMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Body())
func (r *BodyMaxLineLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLineLength("body", r.CheckLen, msg.Body())
}
4 changes: 2 additions & 2 deletions rule/body_min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *BodyMinLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates BodyMinLenRule
func (r *BodyMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Body())
func (r *BodyMinLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMinLen("body", r.CheckLen, msg.Body())
}
4 changes: 2 additions & 2 deletions rule/desc_max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *DescriptionMaxLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates DescriptionMaxLenRule
func (r *DescriptionMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Description())
func (r *DescriptionMaxLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLen("description", r.CheckLen, msg.Description())
}
4 changes: 2 additions & 2 deletions rule/desc_min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *DescriptionMinLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates DescriptionMinLenRule
func (r *DescriptionMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Description())
func (r *DescriptionMinLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMinLen("description", r.CheckLen, msg.Description())
}
14 changes: 8 additions & 6 deletions rule/footer_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rule
import (
"fmt"
"sort"
"strings"

"github.com/conventionalcommit/commitlint/lint"
)
Expand All @@ -29,20 +30,21 @@ func (r *FooterEnumRule) Apply(setting lint.RuleSetting) error {
}

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

for _, note := range msg.Notes() {
isFound := search(r.Tokens, note.Token())
if !isFound {
errMsg := fmt.Sprintf("footer token '%s' is not allowed, you can use one of %v", note.Token(), r.Tokens)
msgs = append(msgs, errMsg)
invalids = append(invalids, note.Token())
}
}

if len(msgs) == 0 {
if len(invalids) == 0 {
return nil, true
}

return msgs, false
desc := fmt.Sprintf("you can use one of %v", r.Tokens)
info := fmt.Sprintf("[%s] tokens are not allowed", strings.Join(invalids, ", "))
return lint.NewIssue(desc, info), false
}
4 changes: 2 additions & 2 deletions rule/footer_max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *FooterMaxLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates FooterMaxLenRule
func (r *FooterMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Footer())
func (r *FooterMaxLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLen("footer", r.CheckLen, msg.Footer())
}
4 changes: 2 additions & 2 deletions rule/footer_max_line_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *FooterMaxLineLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates FooterMaxLineLenRule rule
func (r *FooterMaxLineLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Footer())
func (r *FooterMaxLineLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLineLength("footer", r.CheckLen, msg.Footer())
}
4 changes: 2 additions & 2 deletions rule/footer_min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *FooterMinLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates FooterMinLenRule
func (r *FooterMinLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Footer())
func (r *FooterMinLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMinLen("footer", r.CheckLen, msg.Footer())
}
4 changes: 2 additions & 2 deletions rule/header_max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (r *HeadMaxLenRule) Apply(setting lint.RuleSetting) error {
}

// Validate validates HeadMaxLenRule
func (r *HeadMaxLenRule) Validate(msg lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header())
func (r *HeadMaxLenRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
return validateMaxLen("header", r.CheckLen, msg.Header())
}
4 changes: 2 additions & 2 deletions rule/header_min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func (r *HeadMinLenRule) Apply(setting lint.RuleSetting) error {
}

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

0 comments on commit 37faae2

Please sign in to comment.