fix(lint): normalise report output levels
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e7b41af939
commit
7e32c0c21c
2 changed files with 70 additions and 5 deletions
|
|
@ -109,10 +109,7 @@ func WriteReportText(w io.Writer, report Report) {
|
|||
// lint.WriteReportGitHub(os.Stdout, report)
|
||||
func WriteReportGitHub(w io.Writer, report Report) {
|
||||
for _, finding := range report.Findings {
|
||||
level := finding.Severity
|
||||
if level == "" {
|
||||
level = "warning"
|
||||
}
|
||||
level := githubAnnotationLevel(finding.Severity)
|
||||
|
||||
location := ""
|
||||
if finding.File != "" {
|
||||
|
|
@ -193,7 +190,7 @@ func WriteReportSARIF(w io.Writer, report Report) error {
|
|||
|
||||
result := sarifResult{
|
||||
RuleID: ruleID,
|
||||
Level: finding.Severity,
|
||||
Level: sarifLevel(finding.Severity),
|
||||
Message: sarifMessage{Text: message},
|
||||
}
|
||||
if finding.File != "" {
|
||||
|
|
@ -216,3 +213,29 @@ func WriteReportSARIF(w io.Writer, report Report) error {
|
|||
Runs: []sarifRun{sarifRunValue},
|
||||
})
|
||||
}
|
||||
|
||||
func githubAnnotationLevel(severity string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(severity)) {
|
||||
case "error":
|
||||
return "error"
|
||||
case "info":
|
||||
return "notice"
|
||||
case "warning", "":
|
||||
return "warning"
|
||||
default:
|
||||
return "warning"
|
||||
}
|
||||
}
|
||||
|
||||
func sarifLevel(severity string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(severity)) {
|
||||
case "error":
|
||||
return "error"
|
||||
case "warning":
|
||||
return "warning"
|
||||
case "info":
|
||||
return "note"
|
||||
default:
|
||||
return "warning"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,3 +134,45 @@ func TestWriteText_Good_Empty(t *testing.T) {
|
|||
WriteText(&buf, nil)
|
||||
assert.Empty(t, buf.String())
|
||||
}
|
||||
|
||||
func TestWriteReportGitHub_Good_MapsInfoToNotice(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
WriteReportGitHub(&buf, Report{
|
||||
Findings: []Finding{{
|
||||
Tool: "demo",
|
||||
File: "example.go",
|
||||
Line: 7,
|
||||
Column: 3,
|
||||
Severity: "info",
|
||||
Code: "demo-rule",
|
||||
Message: "explanation",
|
||||
}},
|
||||
})
|
||||
|
||||
assert.Contains(t, buf.String(), "::notice file=example.go,line=7,col=3::[demo] explanation (demo-rule)")
|
||||
}
|
||||
|
||||
func TestWriteReportSARIF_Good_MapsInfoToNote(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
err := WriteReportSARIF(&buf, Report{
|
||||
Findings: []Finding{{
|
||||
Tool: "demo",
|
||||
File: "example.go",
|
||||
Line: 7,
|
||||
Column: 3,
|
||||
Severity: "info",
|
||||
Code: "demo-rule",
|
||||
Message: "explanation",
|
||||
}},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
var decoded map[string]any
|
||||
require.NoError(t, json.Unmarshal(buf.Bytes(), &decoded))
|
||||
|
||||
runs := decoded["runs"].([]any)
|
||||
results := runs[0].(map[string]any)["results"].([]any)
|
||||
assert.Equal(t, "note", results[0].(map[string]any)["level"])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue