github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/printers/codeclimate.go (about)

     1  package printers
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  
     9  	"github.com/golangci/golangci-lint/pkg/result"
    10  )
    11  
    12  // CodeClimateIssue is a subset of the Code Climate spec - https://github.com/codeclimate/spec/blob/master/SPEC.md#data-types
    13  // It is just enough to support GitLab CI Code Quality - https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html
    14  type CodeClimateIssue struct {
    15  	Description string `json:"description"`
    16  	Severity    string `json:"severity,omitempty"`
    17  	Fingerprint string `json:"fingerprint"`
    18  	Location    struct {
    19  		Path  string `json:"path"`
    20  		Lines struct {
    21  			Begin int `json:"begin"`
    22  		} `json:"lines"`
    23  	} `json:"location"`
    24  }
    25  
    26  type CodeClimate struct {
    27  	w io.Writer
    28  }
    29  
    30  func NewCodeClimate(w io.Writer) *CodeClimate {
    31  	return &CodeClimate{w: w}
    32  }
    33  
    34  func (p CodeClimate) Print(ctx context.Context, issues []result.Issue) error {
    35  	codeClimateIssues := make([]CodeClimateIssue, 0, len(issues))
    36  	for i := range issues {
    37  		issue := &issues[i]
    38  		codeClimateIssue := CodeClimateIssue{}
    39  		codeClimateIssue.Description = issue.Description()
    40  		codeClimateIssue.Location.Path = issue.Pos.Filename
    41  		codeClimateIssue.Location.Lines.Begin = issue.Pos.Line
    42  		codeClimateIssue.Fingerprint = issue.Fingerprint()
    43  
    44  		if issue.Severity != "" {
    45  			codeClimateIssue.Severity = issue.Severity
    46  		}
    47  
    48  		codeClimateIssues = append(codeClimateIssues, codeClimateIssue)
    49  	}
    50  
    51  	outputJSON, err := json.Marshal(codeClimateIssues)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	_, err = fmt.Fprint(p.w, string(outputJSON))
    57  	if err != nil {
    58  		return err
    59  	}
    60  	return nil
    61  }