Skip to content

Commit 4c5acca

Browse files
committed
fix: github-action format
1 parent 4532eb9 commit 4c5acca

10 files changed

+209
-35
lines changed

.golangci.next.reference.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ run:
6060
# output configuration options
6161
output:
6262
# The formats used to render issues.
63-
# Format: `colored-line-number`, `line-number`, `json`, `colored-tab`, `tab`, `checkstyle`, `code-climate`, `junit-xml`, `github-actions`, `teamcity`
63+
# Formats:
64+
# - `colored-line-number`
65+
# - `line-number`
66+
# - `json`
67+
# - `colored-tab`
68+
# - `tab`
69+
# - `checkstyle`
70+
# - `code-climate`
71+
# - `junit-xml`
72+
# - `github-actions`
73+
# - `github-actions-problem-matchers`
74+
# - `teamcity`
6475
# Output path can be either `stdout`, `stderr` or path to the file to write to.
6576
#
6677
# For the CLI flag (`--out-format`), multiple formats can be specified by separating them by comma.

.golangci.reference.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ run:
6060
# output configuration options
6161
output:
6262
# The formats used to render issues.
63-
# Format: `colored-line-number`, `line-number`, `json`, `colored-tab`, `tab`, `checkstyle`, `code-climate`, `junit-xml`, `github-actions`, `teamcity`
63+
# Formats:
64+
# - `colored-line-number`
65+
# - `line-number`
66+
# - `json`
67+
# - `colored-tab`
68+
# - `tab`
69+
# - `checkstyle`
70+
# - `code-climate`
71+
# - `junit-xml`
72+
# - `github-actions`
73+
# - `github-actions-problem-matchers`
74+
# - `teamcity`
6475
# Output path can be either `stdout`, `stderr` or path to the file to write to.
6576
#
6677
# For the CLI flag (`--out-format`), multiple formats can be specified by separating them by comma.

jsonschema/golangci.jsonschema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@
432432
"code-climate",
433433
"junit-xml",
434434
"github-actions",
435+
"github-actions-problem-matchers",
435436
"teamcity"
436437
]
437438
}

jsonschema/golangci.next.jsonschema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@
432432
"code-climate",
433433
"junit-xml",
434434
"github-actions",
435+
"github-actions-problem-matchers",
435436
"teamcity"
436437
]
437438
}

pkg/config/output.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
)
99

1010
const (
11-
OutFormatJSON = "json"
12-
OutFormatLineNumber = "line-number"
13-
OutFormatColoredLineNumber = "colored-line-number"
14-
OutFormatTab = "tab"
15-
OutFormatColoredTab = "colored-tab"
16-
OutFormatCheckstyle = "checkstyle"
17-
OutFormatCodeClimate = "code-climate"
18-
OutFormatHTML = "html"
19-
OutFormatJunitXML = "junit-xml"
20-
OutFormatGithubActions = "github-actions"
21-
OutFormatTeamCity = "teamcity"
11+
OutFormatJSON = "json"
12+
OutFormatLineNumber = "line-number"
13+
OutFormatColoredLineNumber = "colored-line-number"
14+
OutFormatTab = "tab"
15+
OutFormatColoredTab = "colored-tab"
16+
OutFormatCheckstyle = "checkstyle"
17+
OutFormatCodeClimate = "code-climate"
18+
OutFormatHTML = "html"
19+
OutFormatJunitXML = "junit-xml"
20+
OutFormatGithubActions = "github-actions"
21+
OutFormatGithubActionsProblemMatchers = "github-actions-problem-matchers"
22+
OutFormatTeamCity = "teamcity"
2223
)
2324

2425
var AllOutputFormats = []string{
@@ -32,6 +33,7 @@ var AllOutputFormats = []string{
3233
OutFormatHTML,
3334
OutFormatJunitXML,
3435
OutFormatGithubActions,
36+
OutFormatGithubActionsProblemMatchers,
3537
OutFormatTeamCity,
3638
}
3739

pkg/printers/githubaction.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package printers
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"path/filepath"
7+
8+
"github.com/golangci/golangci-lint/pkg/result"
9+
)
10+
11+
const defaultGithubSeverity = "error"
12+
13+
type GitHubAction struct {
14+
w io.Writer
15+
}
16+
17+
// NewGitHubAction output format outputs issues according to GitHub actions.
18+
func NewGitHubAction(w io.Writer) *GitHubAction {
19+
return &GitHubAction{w: w}
20+
}
21+
22+
func (p *GitHubAction) Print(issues []result.Issue) error {
23+
for ind := range issues {
24+
_, err := fmt.Fprintln(p.w, formatIssueAsGithub(&issues[ind]))
25+
if err != nil {
26+
return err
27+
}
28+
}
29+
return nil
30+
}
31+
32+
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
33+
func formatIssueAsGithub(issue *result.Issue) string {
34+
severity := defaultGithubSeverity
35+
if issue.Severity != "" {
36+
severity = issue.Severity
37+
}
38+
39+
// Convert backslashes to forward slashes.
40+
// This is needed when running on windows.
41+
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
42+
file := filepath.ToSlash(issue.FilePath())
43+
44+
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
45+
if issue.Pos.Column != 0 {
46+
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
47+
}
48+
49+
ret += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
50+
return ret
51+
}

pkg/printers/github.go renamed to pkg/printers/githubaction_problem_matchers.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ const defaultGitHubSeverity = "error"
1414

1515
const filenameGitHubActionProblemMatchers = "golangci-lint-action-problem-matchers.json"
1616

17-
// GitHubProblemMatchers defines the root of problem matchers.
17+
// GHProblemMatchers defines the root of problem matchers.
1818
// - https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md
1919
// - https://github.com/actions/toolkit/blob/main/docs/commands.md#problem-matchers
20-
type GitHubProblemMatchers struct {
21-
Matchers []GitHubMatcher `json:"problemMatcher,omitempty"`
20+
type GHProblemMatchers struct {
21+
Matchers []GHMatcher `json:"problemMatcher,omitempty"`
2222
}
2323

24-
// GitHubMatcher defines a problem matcher.
25-
type GitHubMatcher struct {
24+
// GHMatcher defines a problem matcher.
25+
type GHMatcher struct {
2626
// Owner an ID field that can be used to remove or replace the problem matcher.
2727
// **required**
2828
Owner string `json:"owner,omitempty"`
2929
// Severity indicates the default severity, either 'warning' or 'error' case-insensitive.
3030
// Defaults to 'error'.
31-
Severity string `json:"severity,omitempty"`
32-
Pattern []GitHubPattern `json:"pattern,omitempty"`
31+
Severity string `json:"severity,omitempty"`
32+
Pattern []GHPattern `json:"pattern,omitempty"`
3333
}
3434

35-
// GitHubPattern defines a pattern for a problem matcher.
36-
type GitHubPattern struct {
35+
// GHPattern defines a pattern for a problem matcher.
36+
type GHPattern struct {
3737
// Regexp the regexp pattern that provides the groups to match against.
3838
// **required**
3939
Regexp string `json:"regexp,omitempty"`
@@ -58,20 +58,20 @@ type GitHubPattern struct {
5858
Loop bool `json:"loop,omitempty"`
5959
}
6060

61-
type GitHub struct {
61+
type GitHubActionProblemMatchers struct {
6262
tempPath string
6363
w io.Writer
6464
}
6565

66-
// NewGitHub output format outputs issues according to GitHub actions the problem matcher regexp.
67-
func NewGitHub(w io.Writer) *GitHub {
68-
return &GitHub{
66+
// NewGitHubActionProblemMatchers output format outputs issues according to GitHub actions the problem matcher regexp.
67+
func NewGitHubActionProblemMatchers(w io.Writer) *GitHubActionProblemMatchers {
68+
return &GitHubActionProblemMatchers{
6969
tempPath: filepath.Join(os.TempDir(), filenameGitHubActionProblemMatchers),
7070
w: w,
7171
}
7272
}
7373

74-
func (p *GitHub) Print(issues []result.Issue) error {
74+
func (p *GitHubActionProblemMatchers) Print(issues []result.Issue) error {
7575
// Note: the file with the problem matcher definition should not be removed.
7676
// A sleep can mitigate this problem but this will be flaky.
7777
//
@@ -99,7 +99,7 @@ func (p *GitHub) Print(issues []result.Issue) error {
9999
return nil
100100
}
101101

102-
func (p *GitHub) storeProblemMatcher() (string, error) {
102+
func (p *GitHubActionProblemMatchers) storeProblemMatcher() (string, error) {
103103
file, err := os.Create(p.tempPath)
104104
if err != nil {
105105
return "", err
@@ -115,13 +115,13 @@ func (p *GitHub) storeProblemMatcher() (string, error) {
115115
return file.Name(), nil
116116
}
117117

118-
func generateProblemMatcher() GitHubProblemMatchers {
119-
return GitHubProblemMatchers{
120-
Matchers: []GitHubMatcher{
118+
func generateProblemMatcher() GHProblemMatchers {
119+
return GHProblemMatchers{
120+
Matchers: []GHMatcher{
121121
{
122122
Owner: "golangci-lint-action",
123123
Severity: "error",
124-
Pattern: []GitHubPattern{
124+
Pattern: []GHPattern{
125125
{
126126
Regexp: `^([^\s]+)\s+([^:]+):(\d+):(?:(\d+):)?\s+(.+)$`,
127127
Severity: 1,

pkg/printers/github_test.go renamed to pkg/printers/githubaction_problem_matchers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestGitHub_Print(t *testing.T) {
4949

5050
buf := new(bytes.Buffer)
5151

52-
printer := NewGitHub(buf)
52+
printer := NewGitHubActionProblemMatchers(buf)
5353
printer.tempPath = filepath.Join(t.TempDir(), filenameGitHubActionProblemMatchers)
5454

5555
err := printer.Print(issues)
@@ -158,7 +158,7 @@ Message: Foo bar`,
158158
}
159159
}
160160

161-
func createReplacement(pattern *GitHubPattern) string {
161+
func createReplacement(pattern *GHPattern) string {
162162
var repl []string
163163

164164
if pattern.File > 0 {

pkg/printers/githubaction_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package printers
2+
3+
import (
4+
"bytes"
5+
"go/token"
6+
"runtime"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/golangci/golangci-lint/pkg/result"
13+
)
14+
15+
func TestGitHubAction_Print(t *testing.T) {
16+
issues := []result.Issue{
17+
{
18+
FromLinter: "linter-a",
19+
Severity: "warning",
20+
Text: "some issue",
21+
Pos: token.Position{
22+
Filename: "path/to/filea.go",
23+
Offset: 2,
24+
Line: 10,
25+
Column: 4,
26+
},
27+
},
28+
{
29+
FromLinter: "linter-b",
30+
Severity: "error",
31+
Text: "another issue",
32+
SourceLines: []string{
33+
"func foo() {",
34+
"\tfmt.Println(\"bar\")",
35+
"}",
36+
},
37+
Pos: token.Position{
38+
Filename: "path/to/fileb.go",
39+
Offset: 5,
40+
Line: 300,
41+
Column: 9,
42+
},
43+
},
44+
}
45+
46+
buf := new(bytes.Buffer)
47+
printer := NewGitHubAction(buf)
48+
49+
err := printer.Print(issues)
50+
require.NoError(t, err)
51+
52+
expected := `::warning file=path/to/filea.go,line=10,col=4::some issue (linter-a)
53+
::error file=path/to/fileb.go,line=300,col=9::another issue (linter-b)
54+
`
55+
56+
assert.Equal(t, expected, buf.String())
57+
}
58+
59+
func Test_formatIssueAsGithub(t *testing.T) {
60+
sampleIssue := result.Issue{
61+
FromLinter: "sample-linter",
62+
Text: "some issue",
63+
Pos: token.Position{
64+
Filename: "path/to/file.go",
65+
Offset: 2,
66+
Line: 10,
67+
Column: 4,
68+
},
69+
}
70+
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
71+
72+
sampleIssue.Pos.Column = 0
73+
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
74+
}
75+
76+
func Test_formatIssueAsGithub_Windows(t *testing.T) {
77+
if runtime.GOOS != "windows" {
78+
t.Skip("Skipping test on non Windows")
79+
}
80+
81+
sampleIssue := result.Issue{
82+
FromLinter: "sample-linter",
83+
Text: "some issue",
84+
Pos: token.Position{
85+
Filename: "path\\to\\file.go",
86+
Offset: 2,
87+
Line: 10,
88+
Column: 4,
89+
},
90+
}
91+
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
92+
93+
sampleIssue.Pos.Column = 0
94+
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
95+
}

pkg/printers/printer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func (c *Printer) createPrinter(format string, w io.Writer) (issuePrinter, error
132132
case config.OutFormatJunitXML:
133133
p = NewJunitXML(w)
134134
case config.OutFormatGithubActions:
135-
p = NewGitHub(w)
135+
p = NewGitHubAction(w)
136+
case config.OutFormatGithubActionsProblemMatchers:
137+
p = NewGitHubActionProblemMatchers(w)
136138
case config.OutFormatTeamCity:
137139
p = NewTeamCity(w)
138140
default:

0 commit comments

Comments
 (0)