Adding github actions output format
This commit is contained in:
parent
4958e50dfe
commit
d7222c7d38
@ -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 {
|
||||||
|
38
pkg/printers/github.go
Normal file
38
pkg/printers/github.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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 {
|
||||||
|
result := fmt.Sprintf("::error file=%s,line=%d", issue.FilePath(), issue.Line())
|
||||||
|
if issue.Pos.Column != 0 {
|
||||||
|
result += fmt.Sprintf(",col=%d", issue.Pos.Column)
|
||||||
|
}
|
||||||
|
|
||||||
|
result += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *github) Print(ctx context.Context, issues []result.Issue) error {
|
||||||
|
for _, issue := range issues {
|
||||||
|
_, err := fmt.Fprintln(logutils.StdOut, formatIssueAsGithub(issue))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
25
pkg/printers/github_test.go
Normal file
25
pkg/printers/github_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package printers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"go/token"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
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