Skip to content

Commit 4f234cc

Browse files
kavuMax Riveiro
authored and
Max Riveiro
committed
Add Severity Levels, use it to return zero exit code
1 parent 58a3545 commit 4f234cc

File tree

10 files changed

+50
-18
lines changed

10 files changed

+50
-18
lines changed

pkg/commands/run.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,20 @@ func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {
380380
}
381381

382382
func (e *Executor) setExitCodeIfIssuesFound(issues []result.Issue) {
383-
if len(issues) != 0 {
383+
if len(issues) == 0 {
384+
return
385+
}
386+
387+
// Let's count how many Errors do we have overall?
388+
errorsCount := 0
389+
for _, i := range issues {
390+
if i.Severity == config.SeverityErrorLevel {
391+
errorsCount++
392+
}
393+
}
394+
395+
// Okay, we've got some errors, so we need to return some non-zero exit-code
396+
if errorsCount > 0 {
384397
e.exitCode = e.cfg.Run.ExitCodeIfIssuesFound
385398
}
386399
}

pkg/config/severity.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ package config
22

33
const severityRuleMinConditionsCount = 1
44

5+
type SeverityLevel string
6+
7+
const (
8+
SeverityDebugLevel SeverityLevel = "debug"
9+
SeverityInfoLevel SeverityLevel = "info"
10+
SeverityWarningLevel SeverityLevel = "warning"
11+
SeverityErrorLevel SeverityLevel = "error"
12+
)
13+
514
type Severity struct {
6-
Default string `mapstructure:"default-severity"`
15+
Default SeverityLevel `mapstructure:"default-severity"`
716
CaseSensitive bool `mapstructure:"case-sensitive"`
817
Rules []SeverityRule `mapstructure:"rules"`
918
}
1019

1120
type SeverityRule struct {
1221
BaseRule `mapstructure:",squash"`
13-
Severity string
22+
Severity SeverityLevel
1423
}
1524

1625
func (s *SeverityRule) Validate() error {

pkg/golinters/revive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
142142
}
143143

144144
return goanalysis.NewIssue(&result.Issue{
145-
Severity: string(object.Severity),
145+
Severity: config.SeverityLevel(object.Severity),
146146
Text: fmt.Sprintf("%s: %s", object.RuleName, object.Failure.Failure),
147147
Pos: token.Position{
148148
Filename: object.Position.Start.Filename,

pkg/printers/checkstyle.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/go-xmlfmt/xmlfmt"
1111

12+
"github.com/golangci/golangci-lint/pkg/config"
1213
"github.com/golangci/golangci-lint/pkg/result"
1314
)
1415

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

34-
const defaultCheckstyleSeverity = "error"
35+
const defaultCheckstyleSeverity config.SeverityLevel = config.SeverityErrorLevel
3536

3637
type Checkstyle struct {
3738
w io.Writer
@@ -69,7 +70,7 @@ func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
6970
Line: issue.Line(),
7071
Message: issue.Text,
7172
Source: issue.FromLinter,
72-
Severity: severity,
73+
Severity: string(severity),
7374
}
7475

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

pkg/printers/codeclimate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (p CodeClimate) Print(ctx context.Context, issues []result.Issue) error {
4242
codeClimateIssue.Fingerprint = issue.Fingerprint()
4343

4444
if issue.Severity != "" {
45-
codeClimateIssue.Severity = issue.Severity
45+
codeClimateIssue.Severity = string(issue.Severity)
4646
}
4747

4848
codeClimateIssues = append(codeClimateIssues, codeClimateIssue)

pkg/printers/github.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"fmt"
66
"io"
77

8+
"github.com/golangci/golangci-lint/pkg/config"
89
"github.com/golangci/golangci-lint/pkg/result"
910
)
1011

1112
type github struct {
1213
w io.Writer
1314
}
1415

15-
const defaultGithubSeverity = "error"
16+
const defaultGithubSeverity config.SeverityLevel = config.SeverityErrorLevel
1617

1718
// NewGithub output format outputs issues according to GitHub actions format:
1819
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message

pkg/printers/junitxml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (p JunitXML) Print(ctx context.Context, issues []result.Issue) error {
6060
Name: i.FromLinter,
6161
ClassName: i.Pos.String(),
6262
Failure: failureXML{
63-
Type: i.Severity,
63+
Type: string(i.Severity),
6464
Message: i.Pos.String() + ": " + i.Text,
6565
Content: fmt.Sprintf("%s: %s\nCategory: %s\nFile: %s\nLine: %d\nDetails: %s",
6666
i.Severity, i.Text, i.FromLinter, i.Pos.Filename, i.Pos.Line, strings.Join(i.SourceLines, "\n")),

pkg/result/issue.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"go/token"
77

88
"golang.org/x/tools/go/packages"
9+
10+
"github.com/golangci/golangci-lint/pkg/config"
911
)
1012

1113
type Range struct {
@@ -28,7 +30,7 @@ type Issue struct {
2830
FromLinter string
2931
Text string
3032

31-
Severity string
33+
Severity config.SeverityLevel
3234

3335
// Source lines of a code with the issue to show
3436
SourceLines []string

pkg/result/processors/severity_rules.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,35 @@ package processors
33
import (
44
"regexp"
55

6+
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/fsutils"
78
"github.com/golangci/golangci-lint/pkg/logutils"
89
"github.com/golangci/golangci-lint/pkg/result"
910
)
1011

1112
type severityRule struct {
1213
baseRule
13-
severity string
14+
severity config.SeverityLevel
1415
}
1516

1617
type SeverityRule struct {
1718
BaseRule
18-
Severity string
19+
Severity config.SeverityLevel
1920
}
2021

2122
type SeverityRules struct {
22-
defaultSeverity string
23+
defaultSeverity config.SeverityLevel
2324
rules []severityRule
2425
lineCache *fsutils.LineCache
2526
log logutils.Log
2627
}
2728

28-
func NewSeverityRules(defaultSeverity string, rules []SeverityRule, lineCache *fsutils.LineCache, log logutils.Log) *SeverityRules {
29+
func NewSeverityRules(
30+
defaultSeverity config.SeverityLevel,
31+
rules []SeverityRule,
32+
lineCache *fsutils.LineCache,
33+
log logutils.Log,
34+
) *SeverityRules {
2935
r := &SeverityRules{
3036
lineCache: lineCache,
3137
log: log,
@@ -89,7 +95,7 @@ type SeverityRulesCaseSensitive struct {
8995
*SeverityRules
9096
}
9197

92-
func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
98+
func NewSeverityRulesCaseSensitive(defaultSeverity config.SeverityLevel, rules []SeverityRule,
9399
lineCache *fsutils.LineCache, log logutils.Log) *SeverityRulesCaseSensitive {
94100
r := &SeverityRules{
95101
lineCache: lineCache,

pkg/result/processors/severity_rules_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestSeverityRulesMultiple(t *testing.T) {
8888
Linter: i.FromLinter,
8989
Text: i.Text,
9090
Line: i.Line(),
91-
Severity: i.Severity,
91+
Severity: string(i.Severity),
9292
})
9393
}
9494
expectedCases := []issueTestCase{
@@ -153,7 +153,7 @@ func TestSeverityRulesOnlyDefault(t *testing.T) {
153153
Linter: i.FromLinter,
154154
Text: i.Text,
155155
Line: i.Line(),
156-
Severity: i.Severity,
156+
Severity: string(i.Severity),
157157
})
158158
}
159159
expectedCases := []issueTestCase{
@@ -194,7 +194,7 @@ func TestSeverityRulesCaseSensitive(t *testing.T) {
194194
Linter: i.FromLinter,
195195
Text: i.Text,
196196
Line: i.Line(),
197-
Severity: i.Severity,
197+
Severity: string(i.Severity),
198198
})
199199
}
200200
expectedCases := []issueTestCase{

0 commit comments

Comments
 (0)