Skip to content

Commit 72a4096

Browse files
authored
Fix checkstyle severity level mappings
Fixes #1225
1 parent be3a661 commit 72a4096

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

cmd/pint/tests/0194_checkstyle_lint.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ groups:
1313
<?xml version="1.0" encoding="UTF-8"?>
1414
<checkstyle version="4.3">
1515
<file name="rules/0001.yml">
16-
<error line="5" severity="Warning" message="always firing alert&#xA;Prometheus alerting rules will trigger an alert for each query that returns *any* result.&#xA;Unless you do want an alert to always fire you should write your query in a way that returns results only when some condition is met.&#xA;In most cases this can be achieved by having some condition in the query expression.&#xA;For example `up == 0` or `rate(error_total[2m]) &gt; 0`.&#xA;Be careful as some PromQL operations will cause the query to always return the results, for example using the [bool modifier](https://prometheus.io/docs/prometheus/latest/querying/operators/#comparison-binary-operators)." source="alerts/comparison"></error>
17-
<error line="7" severity="Fatal" message="PromQL syntax error&#xA;[Click here](https://prometheus.io/docs/prometheus/latest/querying/basics/) for PromQL documentation." source="promql/syntax"></error>
16+
<error line="5" severity="warning" message="always firing alert&#xA;Prometheus alerting rules will trigger an alert for each query that returns *any* result.&#xA;Unless you do want an alert to always fire you should write your query in a way that returns results only when some condition is met.&#xA;In most cases this can be achieved by having some condition in the query expression.&#xA;For example `up == 0` or `rate(error_total[2m]) &gt; 0`.&#xA;Be careful as some PromQL operations will cause the query to always return the results, for example using the [bool modifier](https://prometheus.io/docs/prometheus/latest/querying/operators/#comparison-binary-operators)." source="alerts/comparison"></error>
17+
<error line="7" severity="error" message="PromQL syntax error&#xA;[Click here](https://prometheus.io/docs/prometheus/latest/querying/basics/) for PromQL documentation." source="promql/syntax"></error>
1818
</file>
1919
</checkstyle>

cmd/pint/tests/0196_checkstyle_ci.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ parser {
4646
<?xml version="1.0" encoding="UTF-8"?>
4747
<checkstyle version="4.3">
4848
<file name="rules.yml">
49-
<error line="2" severity="Warning" message="always firing alert&#xA;Prometheus alerting rules will trigger an alert for each query that returns *any* result.&#xA;Unless you do want an alert to always fire you should write your query in a way that returns results only when some condition is met.&#xA;In most cases this can be achieved by having some condition in the query expression.&#xA;For example `up == 0` or `rate(error_total[2m]) &gt; 0`.&#xA;Be careful as some PromQL operations will cause the query to always return the results, for example using the [bool modifier](https://prometheus.io/docs/prometheus/latest/querying/operators/#comparison-binary-operators)." source="alerts/comparison"></error>
50-
<error line="3" severity="Information" message="redundant field with default value" source="alerts/for"></error>
49+
<error line="2" severity="warning" message="always firing alert&#xA;Prometheus alerting rules will trigger an alert for each query that returns *any* result.&#xA;Unless you do want an alert to always fire you should write your query in a way that returns results only when some condition is met.&#xA;In most cases this can be achieved by having some condition in the query expression.&#xA;For example `up == 0` or `rate(error_total[2m]) &gt; 0`.&#xA;Be careful as some PromQL operations will cause the query to always return the results, for example using the [bool modifier](https://prometheus.io/docs/prometheus/latest/querying/operators/#comparison-binary-operators)." source="alerts/comparison"></error>
50+
<error line="3" severity="info" message="redundant field with default value" source="alerts/for"></error>
5151
</file>
5252
</checkstyle>

internal/reporter/checkstyle.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"io"
88
"log/slog"
99
"strconv"
10+
11+
"github.com/cloudflare/pint/internal/checks"
1012
)
1113

1214
func NewCheckStyleReporter(output io.Writer) CheckStyleReporter {
@@ -69,6 +71,19 @@ func (r Report) MarshalXML(e *xml.Encoder, _ xml.StartElement) (err error) {
6971
if r.Problem.Details != "" {
7072
msg += "\n" + r.Problem.Details
7173
}
74+
75+
var checkstyleSeverity string
76+
switch r.Problem.Severity {
77+
case checks.Information:
78+
checkstyleSeverity = "info"
79+
case checks.Warning:
80+
checkstyleSeverity = "warning"
81+
case checks.Bug:
82+
checkstyleSeverity = "error"
83+
case checks.Fatal:
84+
checkstyleSeverity = "error"
85+
}
86+
7287
startel := xml.StartElement{
7388
Name: xml.Name{Local: "error"},
7489
Attr: []xml.Attr{
@@ -78,7 +93,7 @@ func (r Report) MarshalXML(e *xml.Encoder, _ xml.StartElement) (err error) {
7893
},
7994
{
8095
Name: xml.Name{Local: "severity"},
81-
Value: r.Problem.Severity.String(),
96+
Value: checkstyleSeverity,
8297
},
8398
{
8499
Name: xml.Name{Local: "message"},

internal/reporter/checkstyle_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestCheckstyleReporter(t *testing.T) {
7171
output: `<?xml version="1.0" encoding="UTF-8"?>
7272
<checkstyle version="4.3">
7373
<file name="foo.txt">
74-
<error line="5" severity="Information" message="mock text&#xA;mock details" source="mock"></error>
74+
<error line="5" severity="info" message="mock text&#xA;mock details" source="mock"></error>
7575
</file>
7676
</checkstyle>
7777
`,
@@ -100,7 +100,7 @@ func TestCheckstyleReporter(t *testing.T) {
100100
output: `<?xml version="1.0" encoding="UTF-8"?>
101101
<checkstyle version="4.3">
102102
<file name="foo.txt">
103-
<error line="5" severity="Bug" message="mock text" source="mock"></error>
103+
<error line="5" severity="error" message="mock text" source="mock"></error>
104104
</file>
105105
</checkstyle>
106106
`,
@@ -131,7 +131,7 @@ func TestCheckstyleReporter(t *testing.T) {
131131
output: `<?xml version="1.0" encoding="UTF-8"?>
132132
<checkstyle version="4.3">
133133
<file name="foo.txt">
134-
<error line="5" severity="Bug" message="mock text&#xA;&#x9;&#x9;with [new lines] and pipe| chars that are &#39;quoted&#39;&#xA;&#x9;&#x9;" source="mock"></error>
134+
<error line="5" severity="error" message="mock text&#xA;&#x9;&#x9;with [new lines] and pipe| chars that are &#39;quoted&#39;&#xA;&#x9;&#x9;" source="mock"></error>
135135
</file>
136136
</checkstyle>
137137
`,
@@ -171,10 +171,10 @@ func TestCheckstyleReporter(t *testing.T) {
171171
output: `<?xml version="1.0" encoding="UTF-8"?>
172172
<checkstyle version="4.3">
173173
<file name="foo.txt">
174-
<error line="1" severity="Warning" message="problem in foo" source="mock"></error>
174+
<error line="1" severity="warning" message="problem in foo" source="mock"></error>
175175
</file>
176176
<file name="bar.txt">
177-
<error line="2" severity="Fatal" message="problem in bar" source="mock"></error>
177+
<error line="2" severity="error" message="problem in bar" source="mock"></error>
178178
</file>
179179
</checkstyle>
180180
`,
@@ -215,8 +215,8 @@ func TestCheckstyleReporter(t *testing.T) {
215215
output: `<?xml version="1.0" encoding="UTF-8"?>
216216
<checkstyle version="4.3">
217217
<file name="foo.txt">
218-
<error line="1" severity="Information" message="first problem" source="mock1"></error>
219-
<error line="5" severity="Bug" message="second problem&#xA;with details" source="mock2"></error>
218+
<error line="1" severity="info" message="first problem" source="mock1"></error>
219+
<error line="5" severity="error" message="second problem&#xA;with details" source="mock2"></error>
220220
</file>
221221
</checkstyle>
222222
`,

0 commit comments

Comments
 (0)