|
| 1 | +package printers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/md5" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/golangci/golangci-lint/pkg/logutils" |
| 10 | + "github.com/golangci/golangci-lint/pkg/result" |
| 11 | +) |
| 12 | + |
| 13 | +// CodeClimateIssue is a subset of the Code Climate spec - https://github.com/codeclimate/spec/blob/master/SPEC.md#data-types |
| 14 | +// It is just enough to support GitLab CI Code Quality - https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html |
| 15 | +type CodeClimateIssue struct { |
| 16 | + Description string `json:"description"` |
| 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 | +} |
| 28 | + |
| 29 | +func NewCodeClimate() *CodeClimate { |
| 30 | + return &CodeClimate{} |
| 31 | +} |
| 32 | + |
| 33 | +func (p CodeClimate) Print(ctx context.Context, issues <-chan result.Issue) error { |
| 34 | + allIssues := []CodeClimateIssue{} |
| 35 | + for i := range issues { |
| 36 | + var issue CodeClimateIssue |
| 37 | + issue.Description = i.FromLinter + ": " + i.Text |
| 38 | + issue.Location.Path = i.Pos.Filename |
| 39 | + issue.Location.Lines.Begin = i.Pos.Line |
| 40 | + |
| 41 | + // Need a checksum of the issue, so we use MD5 of the filename, text, and first line of source |
| 42 | + hash := md5.New() |
| 43 | + _, _ = hash.Write([]byte(i.Pos.Filename + i.Text + i.SourceLines[0])) |
| 44 | + issue.Fingerprint = fmt.Sprintf("%X", hash.Sum(nil)) |
| 45 | + |
| 46 | + allIssues = append(allIssues, issue) |
| 47 | + } |
| 48 | + |
| 49 | + outputJSON, err := json.Marshal(allIssues) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Fprint(logutils.StdOut, string(outputJSON)) |
| 55 | + return nil |
| 56 | +} |
0 commit comments