Skip to content

Commit 27594c4

Browse files
committed
feat: only one log
1 parent b3e7fcb commit 27594c4

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

pkg/printers/checkstyle.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ const defaultCheckstyleSeverity = "error"
1818
// Checkstyle prints issues in the Checkstyle format.
1919
// https://checkstyle.org/config.html
2020
type Checkstyle struct {
21+
log logutils.Log
2122
w io.Writer
2223
sanitizer severitySanitizer
2324
}
2425

2526
func NewCheckstyle(log logutils.Log, w io.Writer) *Checkstyle {
2627
return &Checkstyle{
27-
w: w,
28+
log: log.Child(logutils.DebugKeyCheckstylePrinter),
29+
w: w,
2830
sanitizer: severitySanitizer{
29-
log: log.Child(logutils.DebugKeyCheckstylePrinter),
3031
// https://checkstyle.org/config.html#Severity
3132
// https://checkstyle.org/property_types.html#SeverityLevel
3233
allowedSeverities: []string{"ignore", "info", "warning", defaultCheckstyleSeverity},
@@ -35,7 +36,7 @@ func NewCheckstyle(log logutils.Log, w io.Writer) *Checkstyle {
3536
}
3637
}
3738

38-
func (p Checkstyle) Print(issues []result.Issue) error {
39+
func (p *Checkstyle) Print(issues []result.Issue) error {
3940
out := checkstyleOutput{
4041
Version: "5.0",
4142
}
@@ -58,12 +59,17 @@ func (p Checkstyle) Print(issues []result.Issue) error {
5859
Line: issue.Line(),
5960
Message: issue.Text,
6061
Source: issue.FromLinter,
61-
Severity: p.sanitizer.Clean(issue.Severity),
62+
Severity: p.sanitizer.Sanitize(issue.Severity),
6263
}
6364

6465
file.Errors = append(file.Errors, newError)
6566
}
6667

68+
err := p.sanitizer.Err()
69+
if err != nil {
70+
p.log.Infof("%v", err)
71+
}
72+
6773
out.Files = maps.Values(files)
6874

6975
sort.Slice(out.Files, func(i, j int) bool {

pkg/printers/codeclimate.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ const defaultCodeClimateSeverity = "critical"
1313
// CodeClimate prints issues in the Code Climate format.
1414
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md
1515
type CodeClimate struct {
16+
log logutils.Log
1617
w io.Writer
1718
sanitizer severitySanitizer
1819
}
1920

2021
func NewCodeClimate(log logutils.Log, w io.Writer) *CodeClimate {
2122
return &CodeClimate{
22-
w: w,
23+
log: log.Child(logutils.DebugKeyCodeClimatePrinter),
24+
w: w,
2325
sanitizer: severitySanitizer{
24-
log: log.Child(logutils.DebugKeyCodeClimatePrinter),
2526
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
2627
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
2728
defaultSeverity: defaultCodeClimateSeverity,
2829
},
2930
}
3031
}
3132

32-
func (p CodeClimate) Print(issues []result.Issue) error {
33+
func (p *CodeClimate) Print(issues []result.Issue) error {
3334
codeClimateIssues := make([]CodeClimateIssue, 0, len(issues))
3435

3536
for i := range issues {
@@ -41,11 +42,16 @@ func (p CodeClimate) Print(issues []result.Issue) error {
4142
codeClimateIssue.Location.Path = issue.Pos.Filename
4243
codeClimateIssue.Location.Lines.Begin = issue.Pos.Line
4344
codeClimateIssue.Fingerprint = issue.Fingerprint()
44-
codeClimateIssue.Severity = p.sanitizer.Clean(issue.Severity)
45+
codeClimateIssue.Severity = p.sanitizer.Sanitize(issue.Severity)
4546

4647
codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
4748
}
4849

50+
err := p.sanitizer.Err()
51+
if err != nil {
52+
p.log.Infof("%v", err)
53+
}
54+
4955
return json.NewEncoder(p.w).Encode(codeClimateIssues)
5056
}
5157

pkg/printers/printer.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10+
"strings"
1011

1112
"github.com/golangci/golangci-lint/pkg/config"
1213
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -142,18 +143,36 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
142143
}
143144

144145
type severitySanitizer struct {
145-
log logutils.Log
146-
147146
allowedSeverities []string
148147
defaultSeverity string
148+
149+
unsupportedSeverities map[string]struct{}
149150
}
150151

151-
func (s *severitySanitizer) Clean(severity string) string {
152+
func (s *severitySanitizer) Sanitize(severity string) string {
152153
if slices.Contains(s.allowedSeverities, severity) {
153154
return severity
154155
}
155156

156-
s.log.Infof("severity '%s' is not inside %v, fallback to '%s'", severity, s.allowedSeverities, s.defaultSeverity)
157+
if s.unsupportedSeverities == nil {
158+
s.unsupportedSeverities = make(map[string]struct{})
159+
}
160+
161+
s.unsupportedSeverities[severity] = struct{}{}
157162

158163
return s.defaultSeverity
159164
}
165+
166+
func (s *severitySanitizer) Err() error {
167+
if len(s.unsupportedSeverities) == 0 {
168+
return nil
169+
}
170+
171+
var foo []string
172+
for k := range s.unsupportedSeverities {
173+
foo = append(foo, "'"+k+"'")
174+
}
175+
176+
return fmt.Errorf("some severities (%v) are not inside supported values (%v), fallback to '%s'",
177+
strings.Join(foo, ", "), strings.Join(s.allowedSeverities, ", "), s.defaultSeverity)
178+
}

pkg/printers/sarif.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ const defaultSarifSeverity = "error"
1919
// https://sarifweb.azurewebsites.net/
2020
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/
2121
type Sarif struct {
22+
log logutils.Log
2223
w io.Writer
2324
sanitizer severitySanitizer
2425
}
2526

2627
func NewSarif(log logutils.Log, w io.Writer) *Sarif {
2728
return &Sarif{
28-
w: w,
29+
log: log.Child(logutils.DebugKeySarifPrinter),
30+
w: w,
2931
sanitizer: severitySanitizer{
30-
log: log.Child(logutils.DebugKeySarifPrinter),
3132
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790898
3233
allowedSeverities: []string{"none", "note", "warning", defaultSarifSeverity},
3334
defaultSeverity: defaultSarifSeverity,
3435
},
3536
}
3637
}
3738

38-
func (p Sarif) Print(issues []result.Issue) error {
39+
func (p *Sarif) Print(issues []result.Issue) error {
3940
run := sarifRun{}
4041
run.Tool.Driver.Name = "golangci-lint"
4142
run.Results = make([]sarifResult, 0)
@@ -45,7 +46,7 @@ func (p Sarif) Print(issues []result.Issue) error {
4546

4647
sr := sarifResult{
4748
RuleID: issue.FromLinter,
48-
Level: p.sanitizer.Clean(issue.Severity),
49+
Level: p.sanitizer.Sanitize(issue.Severity),
4950
Message: sarifMessage{Text: issue.Text},
5051
Locations: []sarifLocation{
5152
{
@@ -65,6 +66,11 @@ func (p Sarif) Print(issues []result.Issue) error {
6566
run.Results = append(run.Results, sr)
6667
}
6768

69+
err := p.sanitizer.Err()
70+
if err != nil {
71+
p.log.Infof("%v", err)
72+
}
73+
6874
output := SarifOutput{
6975
Version: sarifVersion,
7076
Schema: sarifSchemaURI,

pkg/printers/teamcity.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const defaultTeamCitySeverity = "ERROR"
2020
// TeamCity prints issues in the TeamCity format.
2121
// https://www.jetbrains.com/help/teamcity/service-messages.html
2222
type TeamCity struct {
23+
log logutils.Log
2324
w io.Writer
2425
escaper *strings.Replacer
2526
sanitizer severitySanitizer
@@ -28,7 +29,8 @@ type TeamCity struct {
2829
// NewTeamCity output format outputs issues according to TeamCity service message format.
2930
func NewTeamCity(log logutils.Log, w io.Writer) *TeamCity {
3031
return &TeamCity{
31-
w: w,
32+
log: log.Child(logutils.DebugKeyTeamCityPrinter),
33+
w: w,
3234
// https://www.jetbrains.com/help/teamcity/service-messages.html#Escaped+Values
3335
escaper: strings.NewReplacer(
3436
"'", "|'",
@@ -39,7 +41,6 @@ func NewTeamCity(log logutils.Log, w io.Writer) *TeamCity {
3941
"]", "|]",
4042
),
4143
sanitizer: severitySanitizer{
42-
log: log.Child(logutils.DebugKeyTeamCityPrinter),
4344
// https://www.jetbrains.com/help/teamcity/service-messages.html#Inspection+Instance
4445
allowedSeverities: []string{"INFO", defaultTeamCitySeverity, "WARNING", "WEAK WARNING"},
4546
defaultSeverity: defaultTeamCitySeverity,
@@ -75,7 +76,7 @@ func (p *TeamCity) Print(issues []result.Issue) error {
7576
message: issue.Text,
7677
file: issue.FilePath(),
7778
line: issue.Line(),
78-
severity: p.sanitizer.Clean(strings.ToUpper(issue.Severity)),
79+
severity: p.sanitizer.Sanitize(strings.ToUpper(issue.Severity)),
7980
}
8081

8182
_, err := instance.Print(p.w, p.escaper)
@@ -84,6 +85,11 @@ func (p *TeamCity) Print(issues []result.Issue) error {
8485
}
8586
}
8687

88+
err := p.sanitizer.Err()
89+
if err != nil {
90+
p.log.Infof("%v", err)
91+
}
92+
8793
return nil
8894
}
8995

0 commit comments

Comments
 (0)