Skip to content

Commit

Permalink
Merge pull request #8 from CarlJi/github_app
Browse files Browse the repository at this point in the history
feat(staticcheck): adjust format pattern to adapt more scenarios
  • Loading branch information
CarlJi authored Dec 28, 2023
2 parents e733f9f + f3f223b commit bf78422
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
3 changes: 0 additions & 3 deletions linters/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,4 @@ type LinterOutput struct {
Column int
// Message is the staticcheck Message
Message string

// Label is the staticcheck Label
Label string
}
11 changes: 7 additions & 4 deletions linters/staticcheck/staticcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ func formatStaticcheckOutput(output []byte) (map[string][]linters.LinterOutput,
}

func formatStaticcheckLine(line string) (*linters.LinterOutput, error) {
pattern := `^(.*):(\d+):(\d+): (.*) \((.*)\)$`
regex := regexp.MustCompile(pattern)
pattern := `^(.*):(\d+):(\d+): (.*)$`
regex, err := regexp.Compile(pattern)
if err != nil {
log.Errorf("compile regex failed: %v", err)
return nil, err
}
matches := regex.FindStringSubmatch(line)
if len(matches) != 6 {
if len(matches) != 5 {
return nil, fmt.Errorf("unexpected format, original: %s", line)
}

Expand All @@ -163,6 +167,5 @@ func formatStaticcheckLine(line string) (*linters.LinterOutput, error) {
Line: int(lineNumber),
Column: int(columnNumber),
Message: matches[4],
Label: matches[5],
}, nil
}
58 changes: 58 additions & 0 deletions linters/staticcheck/staticcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package staticcheck

import (
"testing"

"github.com/cr-bot/linters"
)

func TestFormatStaticcheckLine(t *testing.T) {
tc := []struct {
input string
expected *linters.LinterOutput
}{
{"cdn-admin.v2/client/dns/dnsapi.go:59:3: assignment to err", &linters.LinterOutput{
File: "cdn-admin.v2/client/dns/dnsapi.go",
Line: 59,
Column: 3,
Message: "assignment to err",
}},
{"smart_scheduler/provider_scheduler/provider_manager/provider_manager.go:207:31: should use make([]float64, len(result.CDNLog.Points)) instead (S1019)", &linters.LinterOutput{
File: "smart_scheduler/provider_scheduler/provider_manager/provider_manager.go",
Line: 207,
Column: 31,
Message: "should use make([]float64, len(result.CDNLog.Points)) instead (S1019)",
}},
{"cdn-admin.v2/api/api_line.go:342:3: should replace loop with ret = append(ret, scope.EdgeNodes...) (S1011)", &linters.LinterOutput{
File: "cdn-admin.v2/api/api_line.go",
Line: 342,
Column: 3,
Message: "should replace loop with ret = append(ret, scope.EdgeNodes...) (S1011)",
}},
{"cdn-admin.v2/api/api_line.go:342 should replace loop with ret = append(ret, scope.EdgeNodes...)", nil},
}

for _, c := range tc {
output, err := formatStaticcheckLine(c.input)
if c.expected == nil && output != nil {
t.Errorf("expected error, got: %v", output)
} else {
continue
}

if err != nil {
t.Errorf("unexpected error: %v", err)
}

if output == nil {
if c.expected != nil {
t.Errorf("expected: %v, got: %v", c.expected, output)
}
continue
}

if output.File != c.expected.File || output.Line != c.expected.Line || output.Column != c.expected.Column || output.Message != c.expected.Message {
t.Errorf("expected: %v, got: %v", c.expected, output)
}
}
}
4 changes: 2 additions & 2 deletions pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func buildPullRequestCommentBody(linterName string, lintErrs map[string][]linter
return nil, err
}

message := fmt.Sprintf("[%s] %s (%s)",
linterName, lintErr.Message, lintErr.Label)
message := fmt.Sprintf("[%s] %s",
linterName, lintErr.Message)

comments = append(comments, &github.PullRequestComment{
Body: github.String(message),
Expand Down

0 comments on commit bf78422

Please sign in to comment.