Skip to content

Commit d1e9da0

Browse files
authored
fix: fallback on unknown severities for code climate (#5350)
1 parent 0b0cc0e commit d1e9da0

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pkg/printers/codeclimate.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package printers
33
import (
44
"encoding/json"
55
"io"
6+
"slices"
67

78
"github.com/golangci/golangci-lint/pkg/result"
89
)
@@ -28,10 +29,15 @@ type CodeClimateIssue struct {
2829

2930
type CodeClimate struct {
3031
w io.Writer
32+
33+
allowedSeverities []string
3134
}
3235

3336
func NewCodeClimate(w io.Writer) *CodeClimate {
34-
return &CodeClimate{w: w}
37+
return &CodeClimate{
38+
w: w,
39+
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
40+
}
3541
}
3642

3743
func (p CodeClimate) Print(issues []result.Issue) error {
@@ -48,7 +54,7 @@ func (p CodeClimate) Print(issues []result.Issue) error {
4854
codeClimateIssue.Fingerprint = issue.Fingerprint()
4955
codeClimateIssue.Severity = defaultCodeClimateSeverity
5056

51-
if issue.Severity != "" {
57+
if slices.Contains(p.allowedSeverities, issue.Severity) {
5258
codeClimateIssue.Severity = issue.Severity
5359
}
5460

pkg/printers/codeclimate_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestCodeClimate_Print(t *testing.T) {
2626
},
2727
{
2828
FromLinter: "linter-b",
29-
Severity: "error",
29+
Severity: "major",
3030
Text: "another issue",
3131
SourceLines: []string{
3232
"func foo() {",
@@ -63,7 +63,7 @@ func TestCodeClimate_Print(t *testing.T) {
6363
err := printer.Print(issues)
6464
require.NoError(t, err)
6565

66-
expected := `[{"description":"linter-a: some issue","check_name":"linter-a","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","check_name":"linter-b","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","check_name":"linter-c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]
66+
expected := `[{"description":"linter-a: some issue","check_name":"linter-a","severity":"critical","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","check_name":"linter-b","severity":"major","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","check_name":"linter-c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]
6767
`
6868

6969
assert.Equal(t, expected, buf.String())

0 commit comments

Comments
 (0)