mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 19:45:51 +00:00
Refactored code.
This commit is contained in:
parent
7539b3735f
commit
2c1a0b8732
2 changed files with 77 additions and 59 deletions
|
@ -38,8 +38,8 @@ const (
|
|||
// ReportCSV set the output format to csv
|
||||
ReportCSV // CSV format
|
||||
|
||||
// ReportXML set the output format to junit xml
|
||||
ReportXML // JUnit XML format
|
||||
// ReportJUnitXML set the output format to junit xml
|
||||
ReportJUnitXML // JUnit XML format
|
||||
)
|
||||
|
||||
var text = `Results:
|
||||
|
@ -61,30 +61,6 @@ type reportInfo struct {
|
|||
Stats *gas.Metrics
|
||||
}
|
||||
|
||||
type XMLReport struct {
|
||||
XMLName xml.Name `xml:"testsuites"`
|
||||
Testsuites []Testsuite `xml:"testsuite"`
|
||||
}
|
||||
|
||||
type Testsuite struct {
|
||||
XMLName xml.Name `xml:"testsuite"`
|
||||
Name string `xml:"name,attr"`
|
||||
Tests int `xml:"tests,attr"`
|
||||
Testcases []Testcase `xml:"testcase"`
|
||||
}
|
||||
|
||||
type Testcase struct {
|
||||
XMLName xml.Name `xml:"testcase"`
|
||||
Name string `xml:"name,attr"`
|
||||
Failure Failure `xml:"failure"`
|
||||
}
|
||||
|
||||
type Failure struct {
|
||||
XMLName xml.Name `xml:"failure"`
|
||||
Message string `xml:"message,attr"`
|
||||
Text string `xml:",innerxml"`
|
||||
}
|
||||
|
||||
// CreateReport generates a report based for the supplied issues and metrics given
|
||||
// the specified format. The formats currently accepted are: json, csv, html and text.
|
||||
func CreateReport(w io.Writer, format string, issues []*gas.Issue, metrics *gas.Metrics) error {
|
||||
|
@ -143,44 +119,16 @@ func reportCSV(w io.Writer, data *reportInfo) error {
|
|||
}
|
||||
|
||||
func reportXML(w io.Writer, data *reportInfo) error {
|
||||
testsuites := make(map[string][]Testcase)
|
||||
for _, issue := range data.Issues {
|
||||
stacktrace, err := json.MarshalIndent(issue, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
testcase := Testcase{
|
||||
Name: issue.File,
|
||||
Failure: Failure{
|
||||
Message: "Found 1 vulnerability. See stacktrace for details.",
|
||||
Text: string(stacktrace),
|
||||
},
|
||||
}
|
||||
if _, ok := testsuites[issue.What]; ok {
|
||||
testsuites[issue.What] = append(testsuites[issue.What], testcase)
|
||||
} else {
|
||||
testsuites[issue.What] = []Testcase{testcase}
|
||||
}
|
||||
}
|
||||
groupedData := groupDataByRules(data)
|
||||
junitXMLStruct := createJUnitXMLStruct(groupedData)
|
||||
|
||||
var xmlReport XMLReport
|
||||
for what, testcases := range testsuites {
|
||||
testsuite := Testsuite{
|
||||
Name: what,
|
||||
Tests: len(testcases),
|
||||
}
|
||||
for _, testcase := range testcases {
|
||||
testsuite.Testcases = append(testsuite.Testcases, testcase)
|
||||
}
|
||||
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
|
||||
}
|
||||
|
||||
raw, err := xml.Marshal(xmlReport)
|
||||
raw, err := xml.Marshal(junitXMLStruct)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
raw = append([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"), raw...)
|
||||
xmlHeader := []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
||||
raw = append(xmlHeader, raw...)
|
||||
_, err = w.Write(raw)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
70
output/junit_xml_format.go
Normal file
70
output/junit_xml_format.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/GoASTScanner/gas"
|
||||
)
|
||||
|
||||
type JUnitXMLReport struct {
|
||||
XMLName xml.Name `xml:"testsuites"`
|
||||
Testsuites []Testsuite `xml:"testsuite"`
|
||||
}
|
||||
|
||||
type Testsuite struct {
|
||||
XMLName xml.Name `xml:"testsuite"`
|
||||
Name string `xml:"name,attr"`
|
||||
Tests int `xml:"tests,attr"`
|
||||
Testcases []Testcase `xml:"testcase"`
|
||||
}
|
||||
|
||||
type Testcase struct {
|
||||
XMLName xml.Name `xml:"testcase"`
|
||||
Name string `xml:"name,attr"`
|
||||
Failure Failure `xml:"failure"`
|
||||
}
|
||||
|
||||
type Failure struct {
|
||||
XMLName xml.Name `xml:"failure"`
|
||||
Message string `xml:"message,attr"`
|
||||
Text string `xml:",innerxml"`
|
||||
}
|
||||
|
||||
func groupDataByRules(data *reportInfo) map[string][]*gas.Issue {
|
||||
groupedData := make(map[string][]*gas.Issue)
|
||||
for _, issue := range data.Issues {
|
||||
if _, ok := groupedData[issue.What]; ok {
|
||||
groupedData[issue.What] = append(groupedData[issue.What], issue)
|
||||
} else {
|
||||
groupedData[issue.What] = []*gas.Issue{issue}
|
||||
}
|
||||
}
|
||||
return groupedData
|
||||
}
|
||||
|
||||
func createJUnitXMLStruct(groupedData map[string][]*gas.Issue) JUnitXMLReport {
|
||||
var xmlReport JUnitXMLReport
|
||||
for what, issues := range groupedData {
|
||||
testsuite := Testsuite{
|
||||
Name: what,
|
||||
Tests: len(issues),
|
||||
}
|
||||
for _, issue := range issues {
|
||||
stacktrace, err := json.MarshalIndent(issue, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
testcase := Testcase{
|
||||
Name: issue.File,
|
||||
Failure: Failure{
|
||||
Message: "Found 1 vulnerability. See stacktrace for details.",
|
||||
Text: string(stacktrace),
|
||||
},
|
||||
}
|
||||
testsuite.Testcases = append(testsuite.Testcases, testcase)
|
||||
}
|
||||
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
|
||||
}
|
||||
return xmlReport
|
||||
}
|
Loading…
Reference in a new issue