Skip to content

Commit 186fe37

Browse files
Elliot Shepherdjirfag
Elliot Shepherd
authored andcommitted
add code-climate output format
Just the minimum of the format, to support GitLab CI Code Quality - https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html
1 parent 48bea8b commit 186fe37

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

.golangci.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ run:
4949

5050
# output configuration options
5151
output:
52-
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
52+
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
5353
format: colored-line-number
5454

5555
# print lines of code with issue, default is true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ Usage:
436436
golangci-lint run [flags]
437437
438438
Flags:
439-
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle (default "colored-line-number")
439+
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate (default "colored-line-number")
440440
--print-issued-lines Print lines of code with issue (default true)
441441
--print-linter-name Print linter name in issue line (default true)
442442
--issues-exit-code int Exit code when issues were found (default 1)
@@ -573,7 +573,7 @@ run:
573573
574574
# output configuration options
575575
output:
576-
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
576+
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
577577
format: colored-line-number
578578
579579
# print lines of code with issue, default is true

pkg/commands/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
363363
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child("tab_printer"))
364364
case config.OutFormatCheckstyle:
365365
p = printers.NewCheckstyle()
366+
case config.OutFormatCodeClimate:
367+
p = printers.NewCodeClimate()
366368
default:
367369
return nil, fmt.Errorf("unknown output format %s", format)
368370
}

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
OutFormatColoredLineNumber = "colored-line-number"
1111
OutFormatTab = "tab"
1212
OutFormatCheckstyle = "checkstyle"
13+
OutFormatCodeClimate = "code-climate"
1314
)
1415

1516
var OutFormats = []string{
@@ -18,6 +19,7 @@ var OutFormats = []string{
1819
OutFormatJSON,
1920
OutFormatTab,
2021
OutFormatCheckstyle,
22+
OutFormatCodeClimate,
2123
}
2224

2325
type ExcludePattern struct {

pkg/printers/codeclimate.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)