Skip to content

Commit 3fd6db7

Browse files
authored
Merge pull request #1017 from viktorasm/add-github-output-format
Adding github actions output format
2 parents 1905465 + b7dada2 commit 3fd6db7

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ Usage:
511511
golangci-lint run [flags]
512512
513513
Flags:
514-
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number")
514+
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions (default "colored-line-number")
515515
--print-issued-lines Print lines of code with issue (default true)
516516
--print-linter-name Print linter name in issue line (default true)
517517
--uniq-by-line Make issues output unique by line (default true)

pkg/commands/run.go

+2
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

+2
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

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

pkg/printers/github_test.go

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

0 commit comments

Comments
 (0)