 fa7adcbda9
			
		
	
	
		fa7adcbda9
		
			
		
	
	
	
	
		
			
			* add ability to set issue severity for out formats that support it based on severity rules * fix lint issues * change log child name * code climate omit severity if empty * add tests for severity rules, add support for case sensitive rules, fix lint issues, better doc comments, share processor test * deduplicated rule logic into a base rule that can be used by multiple rule types, moved severity config to it's own parent key named severity, reduced size of NewRunner function to make it easier to read * put validate function under base rule struct * better validation error wording * add Fingerprint and Description methods to Issue struct, made codeclimate reporter easier to read, checkstyle output is now pretty printed
		
			
				
	
	
		
			88 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package printers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/xml"
 | |
| 	"fmt"
 | |
| 
 | |
| 	"github.com/go-xmlfmt/xmlfmt"
 | |
| 
 | |
| 	"github.com/golangci/golangci-lint/pkg/logutils"
 | |
| 	"github.com/golangci/golangci-lint/pkg/result"
 | |
| )
 | |
| 
 | |
| type checkstyleOutput struct {
 | |
| 	XMLName xml.Name          `xml:"checkstyle"`
 | |
| 	Version string            `xml:"version,attr"`
 | |
| 	Files   []*checkstyleFile `xml:"file"`
 | |
| }
 | |
| 
 | |
| type checkstyleFile struct {
 | |
| 	Name   string             `xml:"name,attr"`
 | |
| 	Errors []*checkstyleError `xml:"error"`
 | |
| }
 | |
| 
 | |
| type checkstyleError struct {
 | |
| 	Column   int    `xml:"column,attr"`
 | |
| 	Line     int    `xml:"line,attr"`
 | |
| 	Message  string `xml:"message,attr"`
 | |
| 	Severity string `xml:"severity,attr"`
 | |
| 	Source   string `xml:"source,attr"`
 | |
| }
 | |
| 
 | |
| const defaultCheckstyleSeverity = "error"
 | |
| 
 | |
| type Checkstyle struct{}
 | |
| 
 | |
| func NewCheckstyle() *Checkstyle {
 | |
| 	return &Checkstyle{}
 | |
| }
 | |
| 
 | |
| func (Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
 | |
| 	out := checkstyleOutput{
 | |
| 		Version: "5.0",
 | |
| 	}
 | |
| 
 | |
| 	files := map[string]*checkstyleFile{}
 | |
| 
 | |
| 	for i := range issues {
 | |
| 		issue := &issues[i]
 | |
| 		file, ok := files[issue.FilePath()]
 | |
| 		if !ok {
 | |
| 			file = &checkstyleFile{
 | |
| 				Name: issue.FilePath(),
 | |
| 			}
 | |
| 
 | |
| 			files[issue.FilePath()] = file
 | |
| 		}
 | |
| 
 | |
| 		severity := defaultCheckstyleSeverity
 | |
| 		if issue.Severity != "" {
 | |
| 			severity = issue.Severity
 | |
| 		}
 | |
| 
 | |
| 		newError := &checkstyleError{
 | |
| 			Column:   issue.Column(),
 | |
| 			Line:     issue.Line(),
 | |
| 			Message:  issue.Text,
 | |
| 			Source:   issue.FromLinter,
 | |
| 			Severity: severity,
 | |
| 		}
 | |
| 
 | |
| 		file.Errors = append(file.Errors, newError)
 | |
| 	}
 | |
| 
 | |
| 	out.Files = make([]*checkstyleFile, 0, len(files))
 | |
| 	for _, file := range files {
 | |
| 		out.Files = append(out.Files, file)
 | |
| 	}
 | |
| 
 | |
| 	data, err := xml.Marshal(&out)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	fmt.Fprintf(logutils.StdOut, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", "  "))
 | |
| 	return nil
 | |
| }
 |