Merge pull request #1017 from viktorasm/add-github-output-format
Adding github actions output format
This commit is contained in:
commit
3fd6db779e
@ -511,7 +511,7 @@ Usage:
|
|||||||
golangci-lint run [flags]
|
golangci-lint run [flags]
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number")
|
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions (default "colored-line-number")
|
||||||
--print-issued-lines Print lines of code with issue (default true)
|
--print-issued-lines Print lines of code with issue (default true)
|
||||||
--print-linter-name Print linter name in issue line (default true)
|
--print-linter-name Print linter name in issue line (default true)
|
||||||
--uniq-by-line Make issues output unique by line (default true)
|
--uniq-by-line Make issues output unique by line (default true)
|
||||||
|
@ -396,6 +396,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
|
|||||||
p = printers.NewCodeClimate()
|
p = printers.NewCodeClimate()
|
||||||
case config.OutFormatJunitXML:
|
case config.OutFormatJunitXML:
|
||||||
p = printers.NewJunitXML()
|
p = printers.NewJunitXML()
|
||||||
|
case config.OutFormatGithubActions:
|
||||||
|
p = printers.NewGithub()
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown output format %s", format)
|
return nil, fmt.Errorf("unknown output format %s", format)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ const (
|
|||||||
OutFormatCheckstyle = "checkstyle"
|
OutFormatCheckstyle = "checkstyle"
|
||||||
OutFormatCodeClimate = "code-climate"
|
OutFormatCodeClimate = "code-climate"
|
||||||
OutFormatJunitXML = "junit-xml"
|
OutFormatJunitXML = "junit-xml"
|
||||||
|
OutFormatGithubActions = "github-actions"
|
||||||
)
|
)
|
||||||
|
|
||||||
var OutFormats = []string{
|
var OutFormats = []string{
|
||||||
@ -25,6 +26,7 @@ var OutFormats = []string{
|
|||||||
OutFormatCheckstyle,
|
OutFormatCheckstyle,
|
||||||
OutFormatCodeClimate,
|
OutFormatCodeClimate,
|
||||||
OutFormatJunitXML,
|
OutFormatJunitXML,
|
||||||
|
OutFormatGithubActions,
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExcludePattern struct {
|
type ExcludePattern struct {
|
||||||
|
39
pkg/printers/github.go
Normal file
39
pkg/printers/github.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
type github struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Github output format outputs issues according to Github actions format:
|
||||||
|
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
|
||||||
|
func NewGithub() Printer {
|
||||||
|
return &github{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
|
||||||
|
func formatIssueAsGithub(issue *result.Issue) string {
|
||||||
|
ret := fmt.Sprintf("::error file=%s,line=%d", issue.FilePath(), issue.Line())
|
||||||
|
if issue.Pos.Column != 0 {
|
||||||
|
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *github) Print(_ context.Context, issues []result.Issue) error {
|
||||||
|
for ind := range issues {
|
||||||
|
_, err := fmt.Fprintln(logutils.StdOut, formatIssueAsGithub(&issues[ind]))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
27
pkg/printers/github_test.go
Normal file
27
pkg/printers/github_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormatGithubIssue(t *testing.T) {
|
||||||
|
sampleIssue := result.Issue{
|
||||||
|
FromLinter: "sample-linter",
|
||||||
|
Text: "some issue",
|
||||||
|
Pos: token.Position{
|
||||||
|
Filename: "path/to/file.go",
|
||||||
|
Offset: 2,
|
||||||
|
Line: 10,
|
||||||
|
Column: 4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
|
||||||
|
|
||||||
|
sampleIssue.Pos.Column = 0
|
||||||
|
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user