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
This commit is contained in:
Elliot Shepherd 2019-02-06 09:21:39 +11:00 committed by Isaev Denis
parent 48bea8b6de
commit 186fe37bf8
5 changed files with 63 additions and 3 deletions

View File

@ -49,7 +49,7 @@ run:
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number
# print lines of code with issue, default is true

View File

@ -436,7 +436,7 @@ Usage:
golangci-lint run [flags]
Flags:
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle (default "colored-line-number")
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate (default "colored-line-number")
--print-issued-lines Print lines of code with issue (default true)
--print-linter-name Print linter name in issue line (default true)
--issues-exit-code int Exit code when issues were found (default 1)
@ -573,7 +573,7 @@ run:
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number
# print lines of code with issue, default is true

View File

@ -363,6 +363,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child("tab_printer"))
case config.OutFormatCheckstyle:
p = printers.NewCheckstyle()
case config.OutFormatCodeClimate:
p = printers.NewCodeClimate()
default:
return nil, fmt.Errorf("unknown output format %s", format)
}

View File

@ -10,6 +10,7 @@ const (
OutFormatColoredLineNumber = "colored-line-number"
OutFormatTab = "tab"
OutFormatCheckstyle = "checkstyle"
OutFormatCodeClimate = "code-climate"
)
var OutFormats = []string{
@ -18,6 +19,7 @@ var OutFormats = []string{
OutFormatJSON,
OutFormatTab,
OutFormatCheckstyle,
OutFormatCodeClimate,
}
type ExcludePattern struct {

View File

@ -0,0 +1,56 @@
package printers
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/golangci/golangci-lint/pkg/logutils"
"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"`
Fingerprint string `json:"fingerprint"`
Location struct {
Path string `json:"path"`
Lines struct {
Begin int `json:"begin"`
} `json:"lines"`
} `json:"location"`
}
type CodeClimate struct {
}
func NewCodeClimate() *CodeClimate {
return &CodeClimate{}
}
func (p CodeClimate) Print(ctx context.Context, issues <-chan result.Issue) error {
allIssues := []CodeClimateIssue{}
for i := range issues {
var issue CodeClimateIssue
issue.Description = i.FromLinter + ": " + i.Text
issue.Location.Path = i.Pos.Filename
issue.Location.Lines.Begin = i.Pos.Line
// Need a checksum of the issue, so we use MD5 of the filename, text, and first line of source
hash := md5.New()
_, _ = hash.Write([]byte(i.Pos.Filename + i.Text + i.SourceLines[0]))
issue.Fingerprint = fmt.Sprintf("%X", hash.Sum(nil))
allIssues = append(allIssues, issue)
}
outputJSON, err := json.Marshal(allIssues)
if err != nil {
return err
}
fmt.Fprint(logutils.StdOut, string(outputJSON))
return nil
}