github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/formatters/checkstyle.go (about)

     1  package formatters
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/khulnasoft-lab/defsec/pkg/severity"
     7  
     8  	"github.com/khulnasoft-lab/defsec/pkg/scan"
     9  )
    10  
    11  type checkstyleResult struct {
    12  	Source   string `xml:"source,attr"`
    13  	Line     int    `xml:"line,attr"`
    14  	Column   int    `xml:"column,attr"`
    15  	Severity string `xml:"severity,attr"`
    16  	Message  string `xml:"message,attr"`
    17  	Link     string `xml:"link,attr"`
    18  }
    19  
    20  type checkstyleFile struct {
    21  	Name   string             `xml:"name,attr"`
    22  	Errors []checkstyleResult `xml:"error"`
    23  }
    24  
    25  type checkstyleOutput struct {
    26  	XMLName xml.Name         `xml:"checkstyle"`
    27  	Version string           `xml:"version,attr"`
    28  	Files   []checkstyleFile `xml:"file"`
    29  }
    30  
    31  func outputCheckStyle(b ConfigurableFormatter, results scan.Results) error {
    32  
    33  	output := checkstyleOutput{
    34  		Version: "5.0",
    35  	}
    36  
    37  	files := make(map[string][]checkstyleResult)
    38  
    39  	for _, res := range results {
    40  
    41  		switch res.Status() {
    42  		case scan.StatusIgnored:
    43  			if !b.IncludeIgnored() {
    44  				continue
    45  			}
    46  		case scan.StatusPassed:
    47  			if !b.IncludePassed() {
    48  				continue
    49  			}
    50  		}
    51  
    52  		var link string
    53  		links := b.GetLinks(res)
    54  		if len(links) > 0 {
    55  			link = links[0]
    56  		}
    57  
    58  		rng := res.Range()
    59  
    60  		path := b.Path(res, res.Metadata())
    61  
    62  		files[path] = append(
    63  			files[path],
    64  			checkstyleResult{
    65  				Source:   res.Rule().LongID(),
    66  				Line:     rng.GetStartLine(),
    67  				Severity: convertSeverity(res.Severity()),
    68  				Message:  res.Description(),
    69  				Link:     link,
    70  			},
    71  		)
    72  	}
    73  
    74  	for name, fileResults := range files {
    75  		output.Files = append(
    76  			output.Files,
    77  			checkstyleFile{
    78  				Name:   name,
    79  				Errors: fileResults,
    80  			},
    81  		)
    82  	}
    83  
    84  	if _, err := b.Writer().Write([]byte(xml.Header)); err != nil {
    85  		return err
    86  	}
    87  
    88  	xmlEncoder := xml.NewEncoder(b.Writer())
    89  	xmlEncoder.Indent("", "\t")
    90  
    91  	return xmlEncoder.Encode(output)
    92  }
    93  
    94  func convertSeverity(s severity.Severity) string {
    95  	switch s {
    96  	case severity.Low:
    97  		return "info"
    98  	case severity.Medium:
    99  		return "warning"
   100  	case severity.High:
   101  		return "error"
   102  	case severity.Critical:
   103  		return "error"
   104  	}
   105  	return "error"
   106  }