Skip to content

dev: cleanup printers #5361

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 3 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
26 changes: 14 additions & 12 deletions pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,37 @@ func NewCodeClimate(log logutils.Log, w io.Writer) *CodeClimate {
}

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

for i := range issues {
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 = p.sanitizer.Sanitize(issue.Severity)
ccIssue := codeClimateIssue{
Description: issue.Description(),
CheckName: issue.FromLinter,
Severity: p.sanitizer.Sanitize(issue.Severity),
Fingerprint: issue.Fingerprint(),
}

codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
ccIssue.Location.Path = issue.Pos.Filename
ccIssue.Location.Lines.Begin = issue.Pos.Line

ccIssues = append(ccIssues, ccIssue)
}

err := p.sanitizer.Err()
if err != nil {
p.log.Infof("%v", err)
}

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

// CodeClimateIssue is a subset of the Code Climate spec.
// 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 {
type codeClimateIssue struct {
Description string `json:"description"`
CheckName string `json:"check_name"`
Severity string `json:"severity,omitempty"`
Expand Down
10 changes: 5 additions & 5 deletions pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// JunitXML prints issues in the Junit XML format.
// 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 {
type JUnitXML struct {
extended bool
w io.Writer
}

func NewJunitXML(w io.Writer, extended bool) *JunitXML {
return &JunitXML{
func NewJUnitXML(w io.Writer, extended bool) *JUnitXML {
return &JUnitXML{
extended: extended,
w: w,
}
}

func (p JunitXML) Print(issues []result.Issue) error {
func (p JUnitXML) Print(issues []result.Issue) error {
suites := make(map[string]testSuiteXML) // use a map to group by file

for ind := range issues {
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/junitxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Details: func foo() {
t.Parallel()

buf := new(bytes.Buffer)
printer := NewJunitXML(buf, test.extended)
printer := NewJUnitXML(buf, test.extended)

err := printer.Print(issues)
require.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
case config.OutFormatHTML:
p = NewHTML(w)
case config.OutFormatJunitXML, config.OutFormatJunitXMLExtended:
p = NewJunitXML(w, format == config.OutFormatJunitXMLExtended)
p = NewJUnitXML(w, format == config.OutFormatJunitXMLExtended)
case config.OutFormatGithubActions:
p = NewGitHubAction(w)
case config.OutFormatTeamCity:
Expand Down Expand Up @@ -168,11 +168,11 @@ func (s *severitySanitizer) Err() error {
return nil
}

var foo []string
var names []string
for k := range s.unsupportedSeverities {
foo = append(foo, "'"+k+"'")
names = append(names, "'"+k+"'")
}

return fmt.Errorf("some severities (%v) are not inside supported values (%v), fallback to '%s'",
strings.Join(foo, ", "), strings.Join(s.allowedSeverities, ", "), s.defaultSeverity)
return fmt.Errorf("severities (%v) are not inside supported values (%v), fallback to '%s'",
strings.Join(names, ", "), strings.Join(s.allowedSeverities, ", "), s.defaultSeverity)
}
2 changes: 1 addition & 1 deletion pkg/printers/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

// Tab prints issues using tabulation as field separator.
// Tab prints issues using tabulation as a field separator.
type Tab struct {
printLinterName bool
useColors bool
Expand Down
Loading