Adding github actions output format

This commit is contained in:
Viktoras Makauskas 2020-04-10 23:46:19 +03:00
parent 4958e50dfe
commit d7222c7d38
4 changed files with 67 additions and 0 deletions

View File

@ -396,6 +396,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
p = printers.NewCodeClimate()
case config.OutFormatJunitXML:
p = printers.NewJunitXML()
case config.OutFormatGithubActions:
p = printers.NewGithub()
default:
return nil, fmt.Errorf("unknown output format %s", format)
}

View File

@ -15,6 +15,7 @@ const (
OutFormatCheckstyle = "checkstyle"
OutFormatCodeClimate = "code-climate"
OutFormatJunitXML = "junit-xml"
OutFormatGithubActions = "github-actions"
)
var OutFormats = []string{
@ -25,6 +26,7 @@ var OutFormats = []string{
OutFormatCheckstyle,
OutFormatCodeClimate,
OutFormatJunitXML,
OutFormatGithubActions,
}
type ExcludePattern struct {

38
pkg/printers/github.go Normal file
View 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
}

View 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))
}