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