Skip to content

Commit 2fa6aa0

Browse files
committed
feat: use problem matchers for GitHub Action format
1 parent 87db2a3 commit 2fa6aa0

File tree

4 files changed

+259
-47
lines changed

4 files changed

+259
-47
lines changed

pkg/printers/github.go

+119-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,102 @@
11
package printers
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
7+
"os"
68
"path/filepath"
79

810
"github.com/golangci/golangci-lint/pkg/result"
911
)
1012

13+
const defaultGitHubSeverity = "error"
14+
15+
const filenameGitHubActionProblemMatchers = "golangci-lint-action-problem-matchers.json"
16+
17+
// GitHubProblemMatchers defines the root of problem matchers.
18+
// - https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md
19+
// - https://github.com/actions/toolkit/blob/main/docs/commands.md#problem-matchers
20+
type GitHubProblemMatchers struct {
21+
Matchers []GitHubMatcher `json:"problemMatcher,omitempty"`
22+
}
23+
24+
// GitHubMatcher defines a problem matcher.
25+
type GitHubMatcher struct {
26+
// Owner an ID field that can be used to remove or replace the problem matcher.
27+
// **required**
28+
Owner string `json:"owner,omitempty"`
29+
// Severity indicates the default severity, either 'warning' or 'error' case-insensitive.
30+
// Defaults to 'error'.
31+
Severity string `json:"severity,omitempty"`
32+
Pattern []GitHubPattern `json:"pattern,omitempty"`
33+
}
34+
35+
// GitHubPattern defines a pattern for a problem matcher.
36+
type GitHubPattern struct {
37+
// Regexp the regexp pattern that provides the groups to match against.
38+
// **required**
39+
Regexp string `json:"regexp,omitempty"`
40+
// File a group number containing the file name.
41+
File int `json:"file,omitempty"`
42+
// FromPath a group number containing a filepath used to root the file (e.g. a project file).
43+
FromPath int `json:"fromPath,omitempty"`
44+
// Line a group number containing the line number.
45+
Line int `json:"line,omitempty"`
46+
// Column a group number containing the column information.
47+
Column int `json:"column,omitempty"`
48+
// Severity a group number containing either 'warning' or 'error' case-insensitive.
49+
// Defaults to `error`.
50+
Severity int `json:"severity,omitempty"`
51+
// Code a group number containing the error code.
52+
Code int `json:"code,omitempty"`
53+
// Message a group number containing the error message.
54+
// **required** at least one pattern must set the message.
55+
Message int `json:"message,omitempty"`
56+
// Loop whether to loop until a match is not found,
57+
// only valid on the last pattern of a multi-pattern matcher.
58+
Loop bool `json:"loop,omitempty"`
59+
}
60+
1161
type GitHub struct {
1262
w io.Writer
1363
}
1464

15-
const defaultGithubSeverity = "error"
16-
17-
// NewGitHub output format outputs issues according to GitHub actions format:
18-
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
65+
// NewGitHub output format outputs issues according to GitHub actions the problem matcher regexp.
1966
func NewGitHub(w io.Writer) *GitHub {
2067
return &GitHub{w: w}
2168
}
2269

23-
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
24-
func formatIssueAsGithub(issue *result.Issue) string {
25-
severity := defaultGithubSeverity
70+
func (p *GitHub) Print(issues []result.Issue) error {
71+
// Note: the file with the problem matcher definition should not be removed.
72+
// A sleep can mitigate this problem but this will be flaky.
73+
//
74+
// Error: Unable to process command '::add-matcher::/tmp/golangci-lint-action-problem-matchers.json' successfully.
75+
// Error: Could not find file '/tmp/golangci-lint-action-problem-matchers.json'.
76+
//
77+
filename, err := storeProblemMatcher()
78+
if err != nil {
79+
return err
80+
}
81+
82+
_, _ = fmt.Fprintln(p.w, "::debug::problem matcher definition file: "+filename)
83+
84+
_, _ = fmt.Fprintln(p.w, "::add-matcher::"+filename)
85+
86+
for ind := range issues {
87+
_, err := fmt.Fprintln(p.w, formatIssueAsGitHub(&issues[ind]))
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
93+
_, _ = fmt.Fprintln(p.w, "::remove-matcher owner=golangci-lint-action::")
94+
95+
return nil
96+
}
97+
98+
func formatIssueAsGitHub(issue *result.Issue) string {
99+
severity := defaultGitHubSeverity
26100
if issue.Severity != "" {
27101
severity = issue.Severity
28102
}
@@ -32,21 +106,49 @@ func formatIssueAsGithub(issue *result.Issue) string {
32106
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
33107
file := filepath.ToSlash(issue.FilePath())
34108

35-
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
109+
ret := fmt.Sprintf("%s\t%s:%d:", severity, file, issue.Line())
36110
if issue.Pos.Column != 0 {
37-
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
111+
ret += fmt.Sprintf("%d:", issue.Pos.Column)
38112
}
39113

40-
ret += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
114+
ret += fmt.Sprintf("\t%s (%s)", issue.Text, issue.FromLinter)
41115
return ret
42116
}
43117

44-
func (p *GitHub) Print(issues []result.Issue) error {
45-
for ind := range issues {
46-
_, err := fmt.Fprintln(p.w, formatIssueAsGithub(&issues[ind]))
47-
if err != nil {
48-
return err
49-
}
118+
func storeProblemMatcher() (string, error) {
119+
//nolint:gosec // To be able to clean the file during tests, we need a deterministic filepath.
120+
file, err := os.Create(filepath.Join(os.TempDir(), filenameGitHubActionProblemMatchers))
121+
if err != nil {
122+
return "", err
123+
}
124+
125+
defer file.Close()
126+
127+
err = json.NewEncoder(file).Encode(generateProblemMatcher())
128+
if err != nil {
129+
return "", err
130+
}
131+
132+
return file.Name(), nil
133+
}
134+
135+
func generateProblemMatcher() GitHubProblemMatchers {
136+
return GitHubProblemMatchers{
137+
Matchers: []GitHubMatcher{
138+
{
139+
Owner: "golangci-lint-action",
140+
Severity: "error",
141+
Pattern: []GitHubPattern{
142+
{
143+
Regexp: `^([^\s]+)\s+([^:]+):(\d+):(?:(\d+):)?\s+(.+)$`,
144+
Severity: 1,
145+
File: 2,
146+
Line: 3,
147+
Column: 4,
148+
Message: 5,
149+
},
150+
},
151+
},
152+
},
50153
}
51-
return nil
52154
}

pkg/printers/github_test.go

+111-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package printers
22

33
import (
44
"bytes"
5+
"fmt"
56
"go/token"
7+
"os"
8+
"path/filepath"
9+
"regexp"
610
"runtime"
11+
"strings"
712
"testing"
813

914
"github.com/stretchr/testify/assert"
@@ -43,20 +48,27 @@ func TestGitHub_Print(t *testing.T) {
4348
},
4449
}
4550

51+
t.Cleanup(func() {
52+
_ = os.RemoveAll(filepath.Join(t.TempDir(), filenameGitHubActionProblemMatchers))
53+
})
54+
4655
buf := new(bytes.Buffer)
4756
printer := NewGitHub(buf)
4857

4958
err := printer.Print(issues)
5059
require.NoError(t, err)
5160

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)
61+
expected := `::debug::problem matcher definition file: /tmp/golangci-lint-action-problem-matchers.json
62+
::add-matcher::/tmp/golangci-lint-action-problem-matchers.json
63+
warning path/to/filea.go:10:4: some issue (linter-a)
64+
error path/to/fileb.go:300:9: another issue (linter-b)
65+
::remove-matcher owner=golangci-lint-action::
5466
`
5567

5668
assert.Equal(t, expected, buf.String())
5769
}
5870

59-
func Test_formatIssueAsGithub(t *testing.T) {
71+
func Test_formatIssueAsGitHub(t *testing.T) {
6072
sampleIssue := result.Issue{
6173
FromLinter: "sample-linter",
6274
Text: "some issue",
@@ -67,13 +79,13 @@ func Test_formatIssueAsGithub(t *testing.T) {
6779
Column: 4,
6880
},
6981
}
70-
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
82+
require.Equal(t, "error\tpath/to/file.go:10:4:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
7183

7284
sampleIssue.Pos.Column = 0
73-
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
85+
require.Equal(t, "error\tpath/to/file.go:10:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
7486
}
7587

76-
func Test_formatIssueAsGithub_Windows(t *testing.T) {
88+
func Test_formatIssueAsGitHub_Windows(t *testing.T) {
7789
if runtime.GOOS != "windows" {
7890
t.Skip("Skipping test on non Windows")
7991
}
@@ -88,8 +100,99 @@ func Test_formatIssueAsGithub_Windows(t *testing.T) {
88100
Column: 4,
89101
},
90102
}
91-
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
103+
require.Equal(t, "error\tpath/to/file.go:10:4:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
92104

93105
sampleIssue.Pos.Column = 0
94-
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
106+
require.Equal(t, "error\tpath/to/file.go:10:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
107+
}
108+
109+
func Test_generateProblemMatcher(t *testing.T) {
110+
pattern := generateProblemMatcher().Matchers[0].Pattern[0]
111+
112+
exp := regexp.MustCompile(pattern.Regexp)
113+
114+
testCases := []struct {
115+
desc string
116+
line string
117+
expected string
118+
}{
119+
{
120+
desc: "error",
121+
line: "error\tpath/to/filea.go:10:4:\tsome issue (sample-linter)",
122+
expected: `File: path/to/filea.go
123+
Line: 10
124+
Column: 4
125+
Severity: error
126+
Message: some issue (sample-linter)`,
127+
},
128+
{
129+
desc: "warning",
130+
line: "warning\tpath/to/fileb.go:1:4:\tsome issue (sample-linter)",
131+
expected: `File: path/to/fileb.go
132+
Line: 1
133+
Column: 4
134+
Severity: warning
135+
Message: some issue (sample-linter)`,
136+
},
137+
{
138+
desc: "no column",
139+
line: "error\t \tpath/to/fileb.go:40:\t Foo bar",
140+
expected: `File: path/to/fileb.go
141+
Line: 40
142+
Column:
143+
Severity: error
144+
Message: Foo bar`,
145+
},
146+
}
147+
148+
for _, test := range testCases {
149+
test := test
150+
t.Run(test.desc, func(t *testing.T) {
151+
t.Parallel()
152+
153+
assert.True(t, exp.MatchString(test.line), test.line)
154+
155+
actual := exp.ReplaceAllString(test.line, createReplacement(&pattern))
156+
157+
assert.Equal(t, test.expected, actual)
158+
})
159+
}
160+
}
161+
162+
func createReplacement(pattern *GitHubPattern) string {
163+
var repl []string
164+
165+
if pattern.File > 0 {
166+
repl = append(repl, fmt.Sprintf("File: $%d", pattern.File))
167+
}
168+
169+
if pattern.FromPath > 0 {
170+
repl = append(repl, fmt.Sprintf("FromPath: $%d", pattern.FromPath))
171+
}
172+
173+
if pattern.Line > 0 {
174+
repl = append(repl, fmt.Sprintf("Line: $%d", pattern.Line))
175+
}
176+
177+
if pattern.Column > 0 {
178+
repl = append(repl, fmt.Sprintf("Column: $%d", pattern.Column))
179+
}
180+
181+
if pattern.Severity > 0 {
182+
repl = append(repl, fmt.Sprintf("Severity: $%d", pattern.Severity))
183+
}
184+
185+
if pattern.Code > 0 {
186+
repl = append(repl, fmt.Sprintf("Code: $%d", pattern.Code))
187+
}
188+
189+
if pattern.Message > 0 {
190+
repl = append(repl, fmt.Sprintf("Message: $%d", pattern.Message))
191+
}
192+
193+
if pattern.Loop {
194+
repl = append(repl, fmt.Sprintf("Loop: $%v", pattern.Loop))
195+
}
196+
197+
return strings.Join(repl, "\n")
95198
}

pkg/printers/printer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ func TestPrinter_Print_file(t *testing.T) {
173173
func TestPrinter_Print_multiple(t *testing.T) {
174174
logger := logutils.NewStderrLog("skip")
175175

176+
t.Cleanup(func() {
177+
_ = os.RemoveAll(filepath.Join(t.TempDir(), filenameGitHubActionProblemMatchers))
178+
})
179+
176180
var issues []result.Issue
177181
unmarshalFile(t, "in-issues.json", &issues)
178182

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
::error file=pkg/experimental/myplugin/myplugin.go,line=13,col=1::don't use `init` function (gochecknoinits)
2-
::error file=pkg/lint/lintersdb/builder_plugin.go,line=59,col=69::hugeParam: settings is heavy (80 bytes); consider passing it by pointer (gocritic)
3-
::error file=pkg/printers/printer_test.go,line=6::File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
4-
::error file=pkg/config/issues.go,line=107,col=13::struct of size 144 bytes could be of size 128 bytes (maligned)
5-
::error file=pkg/config/linters_settings.go,line=200,col=22::struct of size 3144 bytes could be of size 3096 bytes (maligned)
6-
::error file=pkg/config/linters_settings.go,line=383,col=25::struct of size 72 bytes could be of size 64 bytes (maligned)
7-
::error file=pkg/config/linters_settings.go,line=470,col=22::struct of size 72 bytes could be of size 56 bytes (maligned)
8-
::error file=pkg/config/linters_settings.go,line=482,col=23::struct of size 136 bytes could be of size 128 bytes (maligned)
9-
::error file=pkg/config/linters_settings.go,line=584,col=27::struct of size 64 bytes could be of size 56 bytes (maligned)
10-
::error file=pkg/config/linters_settings.go,line=591,col=20::struct of size 88 bytes could be of size 80 bytes (maligned)
11-
::error file=pkg/config/linters_settings.go,line=710,col=25::struct of size 40 bytes could be of size 32 bytes (maligned)
12-
::error file=pkg/config/linters_settings.go,line=762,col=21::struct of size 112 bytes could be of size 104 bytes (maligned)
13-
::error file=pkg/config/linters_settings.go,line=787,col=23::struct of size 32 bytes could be of size 24 bytes (maligned)
14-
::error file=pkg/config/linters_settings.go,line=817,col=23::struct of size 40 bytes could be of size 32 bytes (maligned)
15-
::error file=pkg/config/linters_settings.go,line=902,col=25::struct of size 80 bytes could be of size 72 bytes (maligned)
16-
::error file=pkg/config/linters_settings.go,line=928,col=18::struct of size 112 bytes could be of size 96 bytes (maligned)
17-
::error file=pkg/config/run.go,line=6,col=10::struct of size 168 bytes could be of size 160 bytes (maligned)
18-
::error file=pkg/lint/linter/config.go,line=36,col=13::struct of size 128 bytes could be of size 120 bytes (maligned)
19-
::error file=pkg/golinters/govet_test.go,line=70,col=23::struct of size 96 bytes could be of size 88 bytes (maligned)
20-
::error file=pkg/result/processors/diff.go,line=17,col=11::struct of size 64 bytes could be of size 56 bytes (maligned)
21-
::warning file=pkg/experimental/myplugin/myplugin.go,line=49,col=14::unused-parameter: parameter 'pass' seems to be unused, consider removing or renaming it as _ (revive)
22-
::error file=pkg/commands/run.go,line=47,col=7::const `defaultFileMode` is unused (unused)
1+
::debug::problem matcher definition file: /tmp/golangci-lint-action-problem-matchers.json
2+
::add-matcher::/tmp/golangci-lint-action-problem-matchers.json
3+
error pkg/experimental/myplugin/myplugin.go:13:1: don't use `init` function (gochecknoinits)
4+
error pkg/lint/lintersdb/builder_plugin.go:59:69: hugeParam: settings is heavy (80 bytes); consider passing it by pointer (gocritic)
5+
error pkg/printers/printer_test.go:6: File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
6+
error pkg/config/issues.go:107:13: struct of size 144 bytes could be of size 128 bytes (maligned)
7+
error pkg/config/linters_settings.go:200:22: struct of size 3144 bytes could be of size 3096 bytes (maligned)
8+
error pkg/config/linters_settings.go:383:25: struct of size 72 bytes could be of size 64 bytes (maligned)
9+
error pkg/config/linters_settings.go:470:22: struct of size 72 bytes could be of size 56 bytes (maligned)
10+
error pkg/config/linters_settings.go:482:23: struct of size 136 bytes could be of size 128 bytes (maligned)
11+
error pkg/config/linters_settings.go:584:27: struct of size 64 bytes could be of size 56 bytes (maligned)
12+
error pkg/config/linters_settings.go:591:20: struct of size 88 bytes could be of size 80 bytes (maligned)
13+
error pkg/config/linters_settings.go:710:25: struct of size 40 bytes could be of size 32 bytes (maligned)
14+
error pkg/config/linters_settings.go:762:21: struct of size 112 bytes could be of size 104 bytes (maligned)
15+
error pkg/config/linters_settings.go:787:23: struct of size 32 bytes could be of size 24 bytes (maligned)
16+
error pkg/config/linters_settings.go:817:23: struct of size 40 bytes could be of size 32 bytes (maligned)
17+
error pkg/config/linters_settings.go:902:25: struct of size 80 bytes could be of size 72 bytes (maligned)
18+
error pkg/config/linters_settings.go:928:18: struct of size 112 bytes could be of size 96 bytes (maligned)
19+
error pkg/config/run.go:6:10: struct of size 168 bytes could be of size 160 bytes (maligned)
20+
error pkg/lint/linter/config.go:36:13: struct of size 128 bytes could be of size 120 bytes (maligned)
21+
error pkg/golinters/govet_test.go:70:23: struct of size 96 bytes could be of size 88 bytes (maligned)
22+
error pkg/result/processors/diff.go:17:11: struct of size 64 bytes could be of size 56 bytes (maligned)
23+
warning pkg/experimental/myplugin/myplugin.go:49:14: unused-parameter: parameter 'pass' seems to be unused, consider removing or renaming it as _ (revive)
24+
error pkg/commands/run.go:47:7: const `defaultFileMode` is unused (unused)
25+
::remove-matcher owner=golangci-lint-action::

0 commit comments

Comments
 (0)