Skip to content

fix: sanitize severities by output format #5359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,23 @@ import (

const defaultCheckstyleSeverity = "error"

type checkstyleOutput struct {
XMLName xml.Name `xml:"checkstyle"`
Version string `xml:"version,attr"`
Files []*checkstyleFile `xml:"file"`
}

type checkstyleFile struct {
Name string `xml:"name,attr"`
Errors []*checkstyleError `xml:"error"`
}

type checkstyleError struct {
Column int `xml:"column,attr"`
Line int `xml:"line,attr"`
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
Source string `xml:"source,attr"`
}

// Checkstyle prints issues in the Checkstyle format.
// https://checkstyle.org/config.html
type Checkstyle struct {
w io.Writer
w io.Writer
sanitizer severitySanitizer
}

func NewCheckstyle(w io.Writer) *Checkstyle {
return &Checkstyle{w: w}
return &Checkstyle{
w: w,
sanitizer: severitySanitizer{
// https://checkstyle.org/config.html#Severity
// https://checkstyle.org/property_types.html#SeverityLevel
allowedSeverities: []string{"ignore", "info", "warning", defaultCheckstyleSeverity},
defaultSeverity: defaultCheckstyleSeverity,
},
}
}

func (p Checkstyle) Print(issues []result.Issue) error {
Expand All @@ -59,17 +51,12 @@ func (p Checkstyle) Print(issues []result.Issue) error {
files[issue.FilePath()] = file
}

severity := defaultCheckstyleSeverity
if issue.Severity != "" {
severity = issue.Severity
}

newError := &checkstyleError{
Column: issue.Column(),
Line: issue.Line(),
Message: issue.Text,
Source: issue.FromLinter,
Severity: severity,
Severity: p.sanitizer.Clean(issue.Severity),
}

file.Errors = append(file.Errors, newError)
Expand All @@ -93,3 +80,22 @@ func (p Checkstyle) Print(issues []result.Issue) error {

return nil
}

type checkstyleOutput struct {
XMLName xml.Name `xml:"checkstyle"`
Version string `xml:"version,attr"`
Files []*checkstyleFile `xml:"file"`
}

type checkstyleFile struct {
Name string `xml:"name,attr"`
Errors []*checkstyleError `xml:"error"`
}

type checkstyleError struct {
Column int `xml:"column,attr"`
Line int `xml:"line,attr"`
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
Source string `xml:"source,attr"`
}
58 changes: 29 additions & 29 deletions pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,63 @@ package printers
import (
"encoding/json"
"io"
"slices"

"github.com/golangci/golangci-lint/pkg/result"
)

const defaultCodeClimateSeverity = "critical"

// CodeClimateIssue is a subset of the Code Climate spec.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
// It is just enough to support GitLab CI Code Quality.
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#code-quality-report-format
type CodeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Fingerprint string `json:"fingerprint"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}

// CodeClimate prints issues in the Code Climate format.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md
type CodeClimate struct {
w io.Writer

allowedSeverities []string
w io.Writer
sanitizer severitySanitizer
}

func NewCodeClimate(w io.Writer) *CodeClimate {
return &CodeClimate{
w: w,
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
w: w,
sanitizer: severitySanitizer{
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
allowedSeverities: []string{"info", "minor", "major", defaultCodeClimateSeverity, "blocker"},
defaultSeverity: defaultCodeClimateSeverity,
},
}
}

func (p CodeClimate) Print(issues []result.Issue) error {
codeClimateIssues := make([]CodeClimateIssue, 0, len(issues))

for i := range issues {
issue := &issues[i]
issue := issues[i]

codeClimateIssue := CodeClimateIssue{}
codeClimateIssue.Description = issue.Description()
codeClimateIssue.CheckName = issue.FromLinter
codeClimateIssue.Location.Path = issue.Pos.Filename
codeClimateIssue.Location.Lines.Begin = issue.Pos.Line
codeClimateIssue.Fingerprint = issue.Fingerprint()
codeClimateIssue.Severity = defaultCodeClimateSeverity

if slices.Contains(p.allowedSeverities, issue.Severity) {
codeClimateIssue.Severity = issue.Severity
}
codeClimateIssue.Severity = p.sanitizer.Clean(issue.Severity)

codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
}

return json.NewEncoder(p.w).Encode(codeClimateIssues)
}

// CodeClimateIssue is a subset of the Code Climate spec.
// https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
// It is just enough to support GitLab CI Code Quality.
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#code-quality-report-format
type CodeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Fingerprint string `json:"fingerprint"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}
2 changes: 2 additions & 0 deletions pkg/printers/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ type htmlIssue struct {
Code string
}

// HTML prints issues in an HTML page.
// It uses the Cloudflare CDN (cdnjs) and React.
type HTML struct {
w io.Writer
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/printers/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// JSON prints issues in a JSON representation.
type JSON struct {
rd *report.Data // TODO(ldez) should be drop in v2. Only use by JSON reporter.
rd *report.Data
w io.Writer
}

Expand Down
60 changes: 32 additions & 28 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,10 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

type testSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []testSuiteXML
}

type testSuiteXML struct {
XMLName xml.Name `xml:"testsuite"`
Suite string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
TestCases []testCaseXML `xml:"testcase"`
}

type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
}

type failureXML struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Content string `xml:",cdata"`
}

// JunitXML prints issues in the Junit XML format.
// There is no official specification for the JUnit XML file format,
// and various tools generate and support different flavors of this format.
// https://github.com/testmoapp/junitxml
type JunitXML struct {
extended bool
w io.Writer
Expand Down Expand Up @@ -97,3 +73,31 @@ func (p JunitXML) Print(issues []result.Issue) error {
}
return nil
}

type testSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
TestSuites []testSuiteXML
}

type testSuiteXML struct {
XMLName xml.Name `xml:"testsuite"`
Suite string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
TestCases []testCaseXML `xml:"testcase"`
}

type testCaseXML struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Failure failureXML `xml:"failure"`
File string `xml:"file,attr,omitempty"`
Line int `xml:"line,attr,omitempty"`
}

type failureXML struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Content string `xml:",cdata"`
}
14 changes: 14 additions & 0 deletions pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"slices"

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

return p, nil
}

type severitySanitizer struct {
allowedSeverities []string
defaultSeverity string
}

func (s *severitySanitizer) Clean(severity string) string {
if slices.Contains(s.allowedSeverities, severity) {
return severity
}

return s.defaultSeverity
}
Loading
Loading