From d7222c7d386b9502d8c93cc6a0c8094d928cbae9 Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 10 Apr 2020 23:46:19 +0300 Subject: [PATCH] Adding github actions output format --- pkg/commands/run.go | 2 ++ pkg/config/config.go | 2 ++ pkg/printers/github.go | 38 +++++++++++++++++++++++++++++++++++++ pkg/printers/github_test.go | 25 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 pkg/printers/github.go create mode 100644 pkg/printers/github_test.go diff --git a/pkg/commands/run.go b/pkg/commands/run.go index b306802a..b805995e 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -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) } diff --git a/pkg/config/config.go b/pkg/config/config.go index a4c7e453..f68925c7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 { diff --git a/pkg/printers/github.go b/pkg/printers/github.go new file mode 100644 index 00000000..c8205f45 --- /dev/null +++ b/pkg/printers/github.go @@ -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 +} diff --git a/pkg/printers/github_test.go b/pkg/printers/github_test.go new file mode 100644 index 00000000..5fee530d --- /dev/null +++ b/pkg/printers/github_test.go @@ -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)) +}