Skip to content

Adding github actions output format #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
OutFormatCheckstyle = "checkstyle"
OutFormatCodeClimate = "code-climate"
OutFormatJunitXML = "junit-xml"
OutFormatGithubActions = "github-actions"
)

var OutFormats = []string{
Expand All @@ -25,6 +26,7 @@ var OutFormats = []string{
OutFormatCheckstyle,
OutFormatCodeClimate,
OutFormatJunitXML,
OutFormatGithubActions,
}

type ExcludePattern struct {
Expand Down
38 changes: 38 additions & 0 deletions pkg/printers/github.go
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions pkg/printers/github_test.go
Original file line number Diff line number Diff line change
@@ -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))
}