Skip to content
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

PoC: Add Severity Levels, use it to return zero exit code #3184

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,20 @@ func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {
}

func (e *Executor) setExitCodeIfIssuesFound(issues []result.Issue) {
if len(issues) != 0 {
if len(issues) == 0 {
return
}

// Let's count how many Errors do we have overall?
errorsCount := 0
for _, i := range issues {
if i.Severity == config.SeverityErrorLevel {
errorsCount++
}
}

// Okay, we've got some errors, so we need to return some non-zero exit-code
if errorsCount > 0 {
e.exitCode = e.cfg.Run.ExitCodeIfIssuesFound
}
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/config/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ package config

const severityRuleMinConditionsCount = 1

type SeverityLevel string

const (
SeverityDebugLevel SeverityLevel = "debug"
SeverityInfoLevel SeverityLevel = "info"
SeverityWarningLevel SeverityLevel = "warning"
SeverityErrorLevel SeverityLevel = "error"
)

type Severity struct {
Default string `mapstructure:"default-severity"`
Default SeverityLevel `mapstructure:"default-severity"`
CaseSensitive bool `mapstructure:"case-sensitive"`
Rules []SeverityRule `mapstructure:"rules"`
}

type SeverityRule struct {
BaseRule `mapstructure:",squash"`
Severity string
Severity SeverityLevel
}

func (s *SeverityRule) Validate() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
}

return goanalysis.NewIssue(&result.Issue{
Severity: string(object.Severity),
Severity: config.SeverityLevel(object.Severity),
Text: fmt.Sprintf("%s: %s", object.RuleName, object.Failure.Failure),
Pos: token.Position{
Filename: object.Position.Start.Filename,
Expand Down
5 changes: 3 additions & 2 deletions pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-xmlfmt/xmlfmt"

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

Expand All @@ -31,7 +32,7 @@ type checkstyleError struct {
Source string `xml:"source,attr"`
}

const defaultCheckstyleSeverity = "error"
const defaultCheckstyleSeverity config.SeverityLevel = config.SeverityErrorLevel

type Checkstyle struct {
w io.Writer
Expand Down Expand Up @@ -69,7 +70,7 @@ func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
Line: issue.Line(),
Message: issue.Text,
Source: issue.FromLinter,
Severity: severity,
Severity: string(severity),
}

file.Errors = append(file.Errors, newError)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/codeclimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p CodeClimate) Print(ctx context.Context, issues []result.Issue) error {
codeClimateIssue.Fingerprint = issue.Fingerprint()

if issue.Severity != "" {
codeClimateIssue.Severity = issue.Severity
codeClimateIssue.Severity = string(issue.Severity)
}

codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
Expand Down
3 changes: 2 additions & 1 deletion pkg/printers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"
"io"

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

type github struct {
w io.Writer
}

const defaultGithubSeverity = "error"
const defaultGithubSeverity config.SeverityLevel = config.SeverityErrorLevel

// NewGithub output format outputs issues according to GitHub actions format:
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/junitxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p JunitXML) Print(ctx context.Context, issues []result.Issue) error {
Name: i.FromLinter,
ClassName: i.Pos.String(),
Failure: failureXML{
Type: i.Severity,
Type: string(i.Severity),
Message: i.Pos.String() + ": " + i.Text,
Content: fmt.Sprintf("%s: %s\nCategory: %s\nFile: %s\nLine: %d\nDetails: %s",
i.Severity, i.Text, i.FromLinter, i.Pos.Filename, i.Pos.Line, strings.Join(i.SourceLines, "\n")),
Expand Down
4 changes: 3 additions & 1 deletion pkg/result/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"go/token"

"golang.org/x/tools/go/packages"

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

type Range struct {
Expand All @@ -28,7 +30,7 @@ type Issue struct {
FromLinter string
Text string

Severity string
Severity config.SeverityLevel

// Source lines of a code with the issue to show
SourceLines []string
Expand Down
16 changes: 11 additions & 5 deletions pkg/result/processors/severity_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@ package processors
import (
"regexp"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

type severityRule struct {
baseRule
severity string
severity config.SeverityLevel
}

type SeverityRule struct {
BaseRule
Severity string
Severity config.SeverityLevel
}

type SeverityRules struct {
defaultSeverity string
defaultSeverity config.SeverityLevel
rules []severityRule
lineCache *fsutils.LineCache
log logutils.Log
}

func NewSeverityRules(defaultSeverity string, rules []SeverityRule, lineCache *fsutils.LineCache, log logutils.Log) *SeverityRules {
func NewSeverityRules(
defaultSeverity config.SeverityLevel,
rules []SeverityRule,
lineCache *fsutils.LineCache,
log logutils.Log,
) *SeverityRules {
r := &SeverityRules{
lineCache: lineCache,
log: log,
Expand Down Expand Up @@ -89,7 +95,7 @@ type SeverityRulesCaseSensitive struct {
*SeverityRules
}

func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
func NewSeverityRulesCaseSensitive(defaultSeverity config.SeverityLevel, rules []SeverityRule,
lineCache *fsutils.LineCache, log logutils.Log) *SeverityRulesCaseSensitive {
r := &SeverityRules{
lineCache: lineCache,
Expand Down
6 changes: 3 additions & 3 deletions pkg/result/processors/severity_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestSeverityRulesMultiple(t *testing.T) {
Linter: i.FromLinter,
Text: i.Text,
Line: i.Line(),
Severity: i.Severity,
Severity: string(i.Severity),
})
}
expectedCases := []issueTestCase{
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestSeverityRulesOnlyDefault(t *testing.T) {
Linter: i.FromLinter,
Text: i.Text,
Line: i.Line(),
Severity: i.Severity,
Severity: string(i.Severity),
})
}
expectedCases := []issueTestCase{
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestSeverityRulesCaseSensitive(t *testing.T) {
Linter: i.FromLinter,
Text: i.Text,
Line: i.Line(),
Severity: i.Severity,
Severity: string(i.Severity),
})
}
expectedCases := []issueTestCase{
Expand Down