gosec/output/junit_xml_format.go

75 lines
1.9 KiB
Go
Raw Normal View History

2018-01-27 04:14:35 +00:00
package output
import (
"encoding/xml"
2018-02-01 04:30:47 +00:00
htmlLib "html"
"strconv"
2018-01-27 04:14:35 +00:00
"github.com/securego/gosec"
2018-01-27 04:14:35 +00:00
)
type junitXMLReport struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"testsuites"`
Testsuites []testsuite `xml:"testsuite"`
2018-01-27 04:14:35 +00:00
}
type testsuite struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Testcases []testcase `xml:"testcase"`
2018-01-27 04:14:35 +00:00
}
type testcase struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Failure failure `xml:"failure"`
2018-01-27 04:14:35 +00:00
}
type failure struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"failure"`
Message string `xml:"message,attr"`
Text string `xml:",innerxml"`
}
func generatePlaintext(issue *gosec.Issue) string {
return "Results:\n" +
"[" + issue.File + ":" + issue.Line + "] - " +
issue.What + " (Confidence: " + strconv.Itoa(int(issue.Confidence)) +
2018-02-01 04:30:47 +00:00
", Severity: " + strconv.Itoa(int(issue.Severity)) + ")\n" + "> " + htmlLib.EscapeString(issue.Code)
}
func groupDataByRules(data *reportInfo) map[string][]*gosec.Issue {
groupedData := make(map[string][]*gosec.Issue)
2018-01-27 04:14:35 +00:00
for _, issue := range data.Issues {
if _, ok := groupedData[issue.What]; ok {
groupedData[issue.What] = append(groupedData[issue.What], issue)
} else {
groupedData[issue.What] = []*gosec.Issue{issue}
2018-01-27 04:14:35 +00:00
}
}
return groupedData
}
func createJUnitXMLStruct(groupedData map[string][]*gosec.Issue) junitXMLReport {
var xmlReport junitXMLReport
2018-01-27 04:14:35 +00:00
for what, issues := range groupedData {
testsuite := testsuite{
2018-01-27 04:14:35 +00:00
Name: what,
Tests: len(issues),
}
for _, issue := range issues {
testcase := testcase{
2018-01-27 04:14:35 +00:00
Name: issue.File,
Failure: failure{
2018-01-27 04:14:35 +00:00
Message: "Found 1 vulnerability. See stacktrace for details.",
2018-01-27 14:23:07 +00:00
Text: generatePlaintext(issue),
2018-01-27 04:14:35 +00:00
},
}
testsuite.Testcases = append(testsuite.Testcases, testcase)
}
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
}
return xmlReport
}