Skip to content

Commit 65d7b38

Browse files
committed
feat: sanitize severities by output format
1 parent 2602996 commit 65d7b38

File tree

7 files changed

+83
-59
lines changed

7 files changed

+83
-59
lines changed

pkg/printers/checkstyle.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ const defaultCheckstyleSeverity = "error"
1717
// Checkstyle prints issues in the Checkstyle format.
1818
// https://checkstyle.org/config.html
1919
type Checkstyle struct {
20-
w io.Writer
20+
w io.Writer
21+
sanitizer severitySanitizer
2122
}
2223

2324
func NewCheckstyle(w io.Writer) *Checkstyle {
24-
return &Checkstyle{w: w}
25+
return &Checkstyle{
26+
w: w,
27+
sanitizer: severitySanitizer{
28+
// https://checkstyle.org/config.html#Severity
29+
// https://checkstyle.org/property_types.html#SeverityLevel
30+
allowedSeverities: []string{"ignore", "info", "warning", defaultCheckstyleSeverity},
31+
defaultSeverity: defaultCheckstyleSeverity,
32+
},
33+
}
2534
}
2635

2736
func (p Checkstyle) Print(issues []result.Issue) error {
@@ -42,17 +51,12 @@ func (p Checkstyle) Print(issues []result.Issue) error {
4251
files[issue.FilePath()] = file
4352
}
4453

45-
severity := defaultCheckstyleSeverity
46-
if issue.Severity != "" {
47-
severity = issue.Severity
48-
}
49-
5054
newError := &checkstyleError{
5155
Column: issue.Column(),
5256
Line: issue.Line(),
5357
Message: issue.Text,
5458
Source: issue.FromLinter,
55-
Severity: severity,
59+
Severity: p.sanitizer.Clean(issue.Severity),
5660
}
5761

5862
file.Errors = append(file.Errors, newError)

pkg/printers/codeclimate.go

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

87
"github.com/golangci/golangci-lint/pkg/result"
98
)
@@ -13,15 +12,18 @@ const defaultCodeClimateSeverity = "critical"
1312
// CodeClimate prints issues in the Code Climate format.
1413
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md
1514
type CodeClimate struct {
16-
w io.Writer
17-
18-
allowedSeverities []string
15+
w io.Writer
16+
sanitizer severitySanitizer
1917
}
2018

2119
func NewCodeClimate(w io.Writer) *CodeClimate {
2220
return &CodeClimate{
23-
w: w,
24-
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
21+
w: w,
22+
sanitizer: severitySanitizer{
23+
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
24+
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
25+
defaultSeverity: defaultCodeClimateSeverity,
26+
},
2527
}
2628
}
2729

@@ -37,11 +39,7 @@ func (p CodeClimate) Print(issues []result.Issue) error {
3739
codeClimateIssue.Location.Path = issue.Pos.Filename
3840
codeClimateIssue.Location.Lines.Begin = issue.Pos.Line
3941
codeClimateIssue.Fingerprint = issue.Fingerprint()
40-
codeClimateIssue.Severity = defaultCodeClimateSeverity
41-
42-
if slices.Contains(p.allowedSeverities, issue.Severity) {
43-
codeClimateIssue.Severity = issue.Severity
44-
}
42+
codeClimateIssue.Severity = p.sanitizer.Clean(issue.Severity)
4543

4644
codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
4745
}

pkg/printers/printer.go

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9+
"slices"
910

1011
"github.com/golangci/golangci-lint/pkg/config"
1112
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -143,3 +144,16 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
143144

144145
return p, nil
145146
}
147+
148+
type severitySanitizer struct {
149+
allowedSeverities []string
150+
defaultSeverity string
151+
}
152+
153+
func (s *severitySanitizer) Clean(severity string) string {
154+
if slices.Contains(s.allowedSeverities, severity) {
155+
return severity
156+
}
157+
158+
return s.defaultSeverity
159+
}

pkg/printers/sarif.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ const (
1212
sarifSchemaURI = "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json"
1313
)
1414

15+
const defaultSarifSeverity = "error"
16+
1517
// Sarif prints issues in the SARIF format.
1618
// https://sarifweb.azurewebsites.net/
1719
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/
1820
type Sarif struct {
19-
w io.Writer
21+
w io.Writer
22+
sanitizer severitySanitizer
2023
}
2124

2225
func NewSarif(w io.Writer) *Sarif {
23-
return &Sarif{w: w}
26+
return &Sarif{
27+
w: w,
28+
sanitizer: severitySanitizer{
29+
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
30+
allowedSeverities: []string{"none", "note", "warning", defaultSarifSeverity},
31+
defaultSeverity: defaultSarifSeverity,
32+
},
33+
}
2434
}
2535

2636
func (p Sarif) Print(issues []result.Issue) error {
@@ -31,19 +41,9 @@ func (p Sarif) Print(issues []result.Issue) error {
3141
for i := range issues {
3242
issue := issues[i]
3343

34-
severity := issue.Severity
35-
36-
switch severity {
37-
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
38-
case "none", "note", "warning", "error":
39-
// Valid levels.
40-
default:
41-
severity = "error"
42-
}
43-
4444
sr := sarifResult{
4545
RuleID: issue.FromLinter,
46-
Level: severity,
46+
Level: p.sanitizer.Clean(issue.Severity),
4747
Message: sarifMessage{Text: issue.Text},
4848
Locations: []sarifLocation{
4949
{

pkg/printers/teamcity.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ const (
1414
largeLimit = 4000
1515
)
1616

17+
const defaultTeamCitySeverity = "ERROR"
18+
1719
// TeamCity prints issues in the TeamCity format.
1820
// https://www.jetbrains.com/help/teamcity/service-messages.html
1921
type TeamCity struct {
20-
w io.Writer
21-
escaper *strings.Replacer
22+
w io.Writer
23+
escaper *strings.Replacer
24+
sanitizer severitySanitizer
2225
}
2326

2427
// NewTeamCity output format outputs issues according to TeamCity service message format.
@@ -34,6 +37,11 @@ func NewTeamCity(w io.Writer) *TeamCity {
3437
"[", "|[",
3538
"]", "|]",
3639
),
40+
sanitizer: severitySanitizer{
41+
// https://www.jetbrains.com/help/teamcity/service-messages.html#Inspection+Instance
42+
allowedSeverities: []string{"INFO", defaultTeamCitySeverity, "WARNING", "WEAK WARNING"},
43+
defaultSeverity: defaultTeamCitySeverity,
44+
},
3745
}
3846
}
3947

@@ -65,7 +73,7 @@ func (p *TeamCity) Print(issues []result.Issue) error {
6573
message: issue.Text,
6674
file: issue.FilePath(),
6775
line: issue.Line(),
68-
severity: issue.Severity,
76+
severity: p.sanitizer.Clean(strings.ToUpper(issue.Severity)),
6977
}
7078

7179
_, err := instance.Print(p.w, p.escaper)
@@ -108,7 +116,7 @@ func (i InspectionInstance) Print(w io.Writer, replacer *strings.Replacer) (int,
108116
cutVal(i.typeID, smallLimit),
109117
cutVal(replacer.Replace(i.message), largeLimit),
110118
cutVal(i.file, largeLimit),
111-
i.line, strings.ToUpper(i.severity))
119+
i.line, i.severity)
112120
}
113121

114122
func cutVal(s string, limit int) string {

pkg/printers/teamcity_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ func TestTeamCity_Print(t *testing.T) {
5757
require.NoError(t, err)
5858

5959
expected := `##teamcity[inspectionType id='linter-a' name='linter-a' description='linter-a' category='Golangci-lint reports']
60-
##teamcity[inspection typeId='linter-a' message='warning issue' file='path/to/filea.go' line='10' SEVERITY='']
60+
##teamcity[inspection typeId='linter-a' message='warning issue' file='path/to/filea.go' line='10' SEVERITY='ERROR']
6161
##teamcity[inspection typeId='linter-a' message='error issue' file='path/to/filea.go' line='10' SEVERITY='ERROR']
6262
##teamcity[inspectionType id='linter-b' name='linter-b' description='linter-b' category='Golangci-lint reports']
63-
##teamcity[inspection typeId='linter-b' message='info issue' file='path/to/fileb.go' line='300' SEVERITY='']
63+
##teamcity[inspection typeId='linter-b' message='info issue' file='path/to/fileb.go' line='300' SEVERITY='ERROR']
6464
`
6565

6666
assert.Equal(t, expected, buf.String())
+21-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
##teamcity[inspectionType id='gochecknoinits' name='gochecknoinits' description='gochecknoinits' category='Golangci-lint reports']
2-
##teamcity[inspection typeId='gochecknoinits' message='don|'t use `init` function' file='pkg/experimental/myplugin/myplugin.go' line='13' SEVERITY='']
2+
##teamcity[inspection typeId='gochecknoinits' message='don|'t use `init` function' file='pkg/experimental/myplugin/myplugin.go' line='13' SEVERITY='ERROR']
33
##teamcity[inspectionType id='gocritic' name='gocritic' description='gocritic' category='Golangci-lint reports']
4-
##teamcity[inspection typeId='gocritic' message='hugeParam: settings is heavy (80 bytes); consider passing it by pointer' file='pkg/lint/lintersdb/builder_plugin.go' line='59' SEVERITY='']
4+
##teamcity[inspection typeId='gocritic' message='hugeParam: settings is heavy (80 bytes); consider passing it by pointer' file='pkg/lint/lintersdb/builder_plugin.go' line='59' SEVERITY='ERROR']
55
##teamcity[inspectionType id='goimports' name='goimports' description='goimports' category='Golangci-lint reports']
6-
##teamcity[inspection typeId='goimports' message='File is not `goimports`-ed with -local github.com/golangci/golangci-lint' file='pkg/printers/printer_test.go' line='6' SEVERITY='']
6+
##teamcity[inspection typeId='goimports' message='File is not `goimports`-ed with -local github.com/golangci/golangci-lint' file='pkg/printers/printer_test.go' line='6' SEVERITY='ERROR']
77
##teamcity[inspectionType id='maligned' name='maligned' description='maligned' category='Golangci-lint reports']
8-
##teamcity[inspection typeId='maligned' message='struct of size 144 bytes could be of size 128 bytes' file='pkg/config/issues.go' line='107' SEVERITY='']
9-
##teamcity[inspection typeId='maligned' message='struct of size 3144 bytes could be of size 3096 bytes' file='pkg/config/linters_settings.go' line='200' SEVERITY='']
10-
##teamcity[inspection typeId='maligned' message='struct of size 72 bytes could be of size 64 bytes' file='pkg/config/linters_settings.go' line='383' SEVERITY='']
11-
##teamcity[inspection typeId='maligned' message='struct of size 72 bytes could be of size 56 bytes' file='pkg/config/linters_settings.go' line='470' SEVERITY='']
12-
##teamcity[inspection typeId='maligned' message='struct of size 136 bytes could be of size 128 bytes' file='pkg/config/linters_settings.go' line='482' SEVERITY='']
13-
##teamcity[inspection typeId='maligned' message='struct of size 64 bytes could be of size 56 bytes' file='pkg/config/linters_settings.go' line='584' SEVERITY='']
14-
##teamcity[inspection typeId='maligned' message='struct of size 88 bytes could be of size 80 bytes' file='pkg/config/linters_settings.go' line='591' SEVERITY='']
15-
##teamcity[inspection typeId='maligned' message='struct of size 40 bytes could be of size 32 bytes' file='pkg/config/linters_settings.go' line='710' SEVERITY='']
16-
##teamcity[inspection typeId='maligned' message='struct of size 112 bytes could be of size 104 bytes' file='pkg/config/linters_settings.go' line='762' SEVERITY='']
17-
##teamcity[inspection typeId='maligned' message='struct of size 32 bytes could be of size 24 bytes' file='pkg/config/linters_settings.go' line='787' SEVERITY='']
18-
##teamcity[inspection typeId='maligned' message='struct of size 40 bytes could be of size 32 bytes' file='pkg/config/linters_settings.go' line='817' SEVERITY='']
19-
##teamcity[inspection typeId='maligned' message='struct of size 80 bytes could be of size 72 bytes' file='pkg/config/linters_settings.go' line='902' SEVERITY='']
20-
##teamcity[inspection typeId='maligned' message='struct of size 112 bytes could be of size 96 bytes' file='pkg/config/linters_settings.go' line='928' SEVERITY='']
21-
##teamcity[inspection typeId='maligned' message='struct of size 168 bytes could be of size 160 bytes' file='pkg/config/run.go' line='6' SEVERITY='']
22-
##teamcity[inspection typeId='maligned' message='struct of size 128 bytes could be of size 120 bytes' file='pkg/lint/linter/config.go' line='36' SEVERITY='']
23-
##teamcity[inspection typeId='maligned' message='struct of size 96 bytes could be of size 88 bytes' file='pkg/golinters/govet_test.go' line='70' SEVERITY='']
24-
##teamcity[inspection typeId='maligned' message='struct of size 64 bytes could be of size 56 bytes' file='pkg/result/processors/diff.go' line='17' SEVERITY='']
8+
##teamcity[inspection typeId='maligned' message='struct of size 144 bytes could be of size 128 bytes' file='pkg/config/issues.go' line='107' SEVERITY='ERROR']
9+
##teamcity[inspection typeId='maligned' message='struct of size 3144 bytes could be of size 3096 bytes' file='pkg/config/linters_settings.go' line='200' SEVERITY='ERROR']
10+
##teamcity[inspection typeId='maligned' message='struct of size 72 bytes could be of size 64 bytes' file='pkg/config/linters_settings.go' line='383' SEVERITY='ERROR']
11+
##teamcity[inspection typeId='maligned' message='struct of size 72 bytes could be of size 56 bytes' file='pkg/config/linters_settings.go' line='470' SEVERITY='ERROR']
12+
##teamcity[inspection typeId='maligned' message='struct of size 136 bytes could be of size 128 bytes' file='pkg/config/linters_settings.go' line='482' SEVERITY='ERROR']
13+
##teamcity[inspection typeId='maligned' message='struct of size 64 bytes could be of size 56 bytes' file='pkg/config/linters_settings.go' line='584' SEVERITY='ERROR']
14+
##teamcity[inspection typeId='maligned' message='struct of size 88 bytes could be of size 80 bytes' file='pkg/config/linters_settings.go' line='591' SEVERITY='ERROR']
15+
##teamcity[inspection typeId='maligned' message='struct of size 40 bytes could be of size 32 bytes' file='pkg/config/linters_settings.go' line='710' SEVERITY='ERROR']
16+
##teamcity[inspection typeId='maligned' message='struct of size 112 bytes could be of size 104 bytes' file='pkg/config/linters_settings.go' line='762' SEVERITY='ERROR']
17+
##teamcity[inspection typeId='maligned' message='struct of size 32 bytes could be of size 24 bytes' file='pkg/config/linters_settings.go' line='787' SEVERITY='ERROR']
18+
##teamcity[inspection typeId='maligned' message='struct of size 40 bytes could be of size 32 bytes' file='pkg/config/linters_settings.go' line='817' SEVERITY='ERROR']
19+
##teamcity[inspection typeId='maligned' message='struct of size 80 bytes could be of size 72 bytes' file='pkg/config/linters_settings.go' line='902' SEVERITY='ERROR']
20+
##teamcity[inspection typeId='maligned' message='struct of size 112 bytes could be of size 96 bytes' file='pkg/config/linters_settings.go' line='928' SEVERITY='ERROR']
21+
##teamcity[inspection typeId='maligned' message='struct of size 168 bytes could be of size 160 bytes' file='pkg/config/run.go' line='6' SEVERITY='ERROR']
22+
##teamcity[inspection typeId='maligned' message='struct of size 128 bytes could be of size 120 bytes' file='pkg/lint/linter/config.go' line='36' SEVERITY='ERROR']
23+
##teamcity[inspection typeId='maligned' message='struct of size 96 bytes could be of size 88 bytes' file='pkg/golinters/govet_test.go' line='70' SEVERITY='ERROR']
24+
##teamcity[inspection typeId='maligned' message='struct of size 64 bytes could be of size 56 bytes' file='pkg/result/processors/diff.go' line='17' SEVERITY='ERROR']
2525
##teamcity[inspectionType id='revive' name='revive' description='revive' category='Golangci-lint reports']
2626
##teamcity[inspection typeId='revive' message='unused-parameter: parameter |'pass|' seems to be unused, consider removing or renaming it as _' file='pkg/experimental/myplugin/myplugin.go' line='49' SEVERITY='WARNING']
2727
##teamcity[inspectionType id='unused' name='unused' description='unused' category='Golangci-lint reports']
28-
##teamcity[inspection typeId='unused' message='const `defaultFileMode` is unused' file='pkg/commands/run.go' line='47' SEVERITY='']
28+
##teamcity[inspection typeId='unused' message='const `defaultFileMode` is unused' file='pkg/commands/run.go' line='47' SEVERITY='ERROR']

0 commit comments

Comments
 (0)