Skip to content

Commit

Permalink
fix(golangci-lint): ignore warning message
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Jun 21, 2024
1 parent c4ca1ce commit bde218a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
24 changes: 17 additions & 7 deletions internal/linters/go/golangci_lint/golangci_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,37 @@ func golangciLintHandler(log *xlog.Logger, a linters.Agent) error {
if linters.IsEmpty(a.LinterConfig.Args...) {
// refer: https://github.com/qiniu/reviewbot/issues/146
a.LinterConfig.Args = append([]string{}, "run", "--timeout=5m0s", "--allow-parallel-runners=true")
// recommend to use the line-number format and disable the issued lines
// since these are more friendly to the reviewbot
a.LinterConfig.Args = append(a.LinterConfig.Args, "--out-format=line-number", "--print-issued-lines=false")
}

// recommend to use the line-number format and disable the issued lines, since these are more friendly to the reviewbot
// checking on golangci-lint 1.59.0, there is no problem even with multiple --out-format and --print-issued-lines parameters,
// so we can add these parameters directly
a.LinterConfig.Args = append(a.LinterConfig.Args, "--out-format=line-number", "--print-issued-lines=false")

if a.LinterConfig.ConfigPath != "" {
a.LinterConfig.Args = append(a.LinterConfig.Args, "--config", a.LinterConfig.ConfigPath)
}

return linters.GeneralHandler(log, a, golangciLintParse)
return linters.GeneralHandler(log, a, parser)
}

func golangciLintParse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) {
var lineParse = func(line string) (*linters.LinterOutput, error) {
func parser(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) {
var lineParser = func(line string) (*linters.LinterOutput, error) {
if strings.HasSuffix(line, "(typecheck)") {
// refer: https://github.com/qiniu/reviewbot/issues/82#issuecomment-2002340788
return nil, fmt.Errorf("skip golangci-lint typecheck error: %s", line)

Check warning on line 41 in internal/linters/go/golangci_lint/golangci_lint.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/go/golangci_lint/golangci_lint.go#L41

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"skip golangci-lint typecheck error: %s\", line)" (err113)
}

// skip the warning level log
// example: level=warning msg="[linters_context] copyloopvar: this linter is disabled because the Go version (1.18) of your project is lower than Go 1.22"
// the warning level log is not a real lint error, so we need to skip it
if strings.Contains(line, "level=warning") {
log.Warnf("skip golangci-lint warning: %s", line)
return nil, nil

Check warning on line 49 in internal/linters/go/golangci_lint/golangci_lint.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/go/golangci_lint/golangci_lint.go#L49

return both the `nil` error and invalid value: use a sentinel error instead (nilnil)
}

return linters.GeneralLineParser(line)
}

return linters.Parse(log, output, lineParse)
return linters.Parse(log, output, lineParser)
}
90 changes: 90 additions & 0 deletions internal/linters/go/golangci_lint/golangci_lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package golangci_lint

Check warning on line 1 in internal/linters/go/golangci_lint/golangci_lint_test.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/go/golangci_lint/golangci_lint_test.go#L1

var-naming: don't use an underscore in package name (revive)

import (
"reflect"
"testing"

"github.com/qiniu/reviewbot/internal/linters"
"github.com/qiniu/x/xlog"
)

func TestParser(t *testing.T) {
cases := []struct {
output []byte
want map[string][]linters.LinterOutput
err error
}{
{
output: []byte(`
golangci_lint/golangci_lint.go:16:1: warning: (golint)
golangci_lint.go:18:3: error: (golint)
`),
want: map[string][]linters.LinterOutput{
"golangci_lint/golangci_lint.go": {
{
File: "golangci_lint/golangci_lint.go",
Line: 16,
Column: 1,
Message: "warning: (golint)",
},
},
"golangci_lint.go": {
{
File: "golangci_lint.go",
Line: 18,
Column: 3,
Message: "error: (golint)",
},
},
},
err: nil,
},
{
output: []byte(`
golangci_lint.go:16:1: error (typecheck)
golangci_lint.go:16:1: error (gochecknoglobals)
`),
want: map[string][]linters.LinterOutput{
"golangci_lint.go": {
{
File: "golangci_lint.go",
Line: 16,
Column: 1,
Message: "error (gochecknoglobals)",
},
},
},
err: nil,
},
{
output: []byte(`
level=warning msg="[linters_context] copyloopvar: this linter is disabled because the Go version (1.18) of your project is lower than Go 1.22"
golangci_lint.go:16:1: warning: (gochecknoglobals)
`),

want: map[string][]linters.LinterOutput{
"golangci_lint.go": {
{
File: "golangci_lint.go",
Line: 16,
Column: 1,
Message: "warning: (gochecknoglobals)",
},
},
},
err: nil,
},
}

for _, tt := range cases {
got, err := parser(xlog.New("UnitTest"), tt.output)
if err != tt.err {

Check warning on line 81 in internal/linters/go/golangci_lint/golangci_lint_test.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/go/golangci_lint/golangci_lint_test.go#L81

do not compare errors directly "err != tt.err", use "!errors.Is(err, tt.err)" instead (err113)
t.Errorf("parser() error = %v, wantErr %v", err, tt.err)
return
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parser() = %v, want %v", got, tt.want)
}
}
}

0 comments on commit bde218a

Please sign in to comment.