Skip to content

Commit d7222c7

Browse files
committed
Adding github actions output format
1 parent 4958e50 commit d7222c7

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

pkg/commands/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) {
396396
p = printers.NewCodeClimate()
397397
case config.OutFormatJunitXML:
398398
p = printers.NewJunitXML()
399+
case config.OutFormatGithubActions:
400+
p = printers.NewGithub()
399401
default:
400402
return nil, fmt.Errorf("unknown output format %s", format)
401403
}

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
OutFormatCheckstyle = "checkstyle"
1616
OutFormatCodeClimate = "code-climate"
1717
OutFormatJunitXML = "junit-xml"
18+
OutFormatGithubActions = "github-actions"
1819
)
1920

2021
var OutFormats = []string{
@@ -25,6 +26,7 @@ var OutFormats = []string{
2526
OutFormatCheckstyle,
2627
OutFormatCodeClimate,
2728
OutFormatJunitXML,
29+
OutFormatGithubActions,
2830
}
2931

3032
type ExcludePattern struct {

pkg/printers/github.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package printers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/golangci/golangci-lint/pkg/logutils"
7+
"github.com/golangci/golangci-lint/pkg/result"
8+
)
9+
10+
type github struct {
11+
}
12+
13+
// Github output format outputs issues according to Github actions format:
14+
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
15+
func NewGithub() Printer {
16+
return &github{}
17+
}
18+
19+
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
20+
func formatIssueAsGithub(issue result.Issue) string {
21+
result := fmt.Sprintf("::error file=%s,line=%d", issue.FilePath(), issue.Line())
22+
if issue.Pos.Column != 0 {
23+
result += fmt.Sprintf(",col=%d", issue.Pos.Column)
24+
}
25+
26+
result += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
27+
return result
28+
}
29+
30+
func (g *github) Print(ctx context.Context, issues []result.Issue) error {
31+
for _, issue := range issues {
32+
_, err := fmt.Fprintln(logutils.StdOut, formatIssueAsGithub(issue))
33+
if err != nil {
34+
return err
35+
}
36+
}
37+
return nil
38+
}

pkg/printers/github_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package printers
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/result"
5+
"github.com/stretchr/testify/require"
6+
"go/token"
7+
"testing"
8+
)
9+
10+
func TestFormatGithubIssue(t *testing.T) {
11+
sampleIssue := result.Issue{
12+
FromLinter: "sample-linter",
13+
Text: "some issue",
14+
Pos: token.Position{
15+
Filename: "path/to/file.go",
16+
Offset: 2,
17+
Line: 10,
18+
Column: 4,
19+
},
20+
}
21+
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(sampleIssue))
22+
23+
sampleIssue.Pos.Column = 0
24+
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(sampleIssue))
25+
}

0 commit comments

Comments
 (0)