Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
40 changes: 40 additions & 0 deletions pkg/printers/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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(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
}
27 changes: 27 additions & 0 deletions pkg/printers/github_test.go
Original file line number Diff line number Diff line change
@@ -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))
}