gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/output/junit_xml_format.go (about)

     1  package output
     2  
     3  import (
     4  	"encoding/xml"
     5  	htmlLib "html"
     6  	"strconv"
     7  
     8  	"github.com/securego/gosec"
     9  )
    10  
    11  type junitXMLReport struct {
    12  	XMLName    xml.Name    `xml:"testsuites"`
    13  	Testsuites []testsuite `xml:"testsuite"`
    14  }
    15  
    16  type testsuite struct {
    17  	XMLName   xml.Name   `xml:"testsuite"`
    18  	Name      string     `xml:"name,attr"`
    19  	Tests     int        `xml:"tests,attr"`
    20  	Testcases []testcase `xml:"testcase"`
    21  }
    22  
    23  type testcase struct {
    24  	XMLName xml.Name `xml:"testcase"`
    25  	Name    string   `xml:"name,attr"`
    26  	Failure failure  `xml:"failure"`
    27  }
    28  
    29  type failure struct {
    30  	XMLName xml.Name `xml:"failure"`
    31  	Message string   `xml:"message,attr"`
    32  	Text    string   `xml:",innerxml"`
    33  }
    34  
    35  func generatePlaintext(issue *gosec.Issue) string {
    36  	return "Results:\n" +
    37  		"[" + issue.File + ":" + issue.Line + "] - " +
    38  		issue.What + " (Confidence: " + strconv.Itoa(int(issue.Confidence)) +
    39  		", Severity: " + strconv.Itoa(int(issue.Severity)) + ")\n" + "> " + htmlLib.EscapeString(issue.Code)
    40  }
    41  
    42  func groupDataByRules(data *reportInfo) map[string][]*gosec.Issue {
    43  	groupedData := make(map[string][]*gosec.Issue)
    44  	for _, issue := range data.Issues {
    45  		if _, ok := groupedData[issue.What]; ok {
    46  			groupedData[issue.What] = append(groupedData[issue.What], issue)
    47  		} else {
    48  			groupedData[issue.What] = []*gosec.Issue{issue}
    49  		}
    50  	}
    51  	return groupedData
    52  }
    53  
    54  func createJUnitXMLStruct(groupedData map[string][]*gosec.Issue) junitXMLReport {
    55  	var xmlReport junitXMLReport
    56  	for what, issues := range groupedData {
    57  		testsuite := testsuite{
    58  			Name:  what,
    59  			Tests: len(issues),
    60  		}
    61  		for _, issue := range issues {
    62  			testcase := testcase{
    63  				Name: issue.File,
    64  				Failure: failure{
    65  					Message: "Found 1 vulnerability. See stacktrace for details.",
    66  					Text:    generatePlaintext(issue),
    67  				},
    68  			}
    69  			testsuite.Testcases = append(testsuite.Testcases, testcase)
    70  		}
    71  		xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
    72  	}
    73  	return xmlReport
    74  }