Skip to content

Commit 28b3813

Browse files
authored
feat: use problem matchers for GitHub Action format (#4685)
1 parent 24bcca2 commit 28b3813

File tree

5 files changed

+268
-55
lines changed

5 files changed

+268
-55
lines changed

pkg/printers/github.go

+127-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,143 @@
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

11-
type GitHub struct {
12-
w io.Writer
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"`
1322
}
1423

15-
const defaultGithubSeverity = "error"
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+
}
1660

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
61+
type GitHub struct {
62+
tempPath string
63+
w io.Writer
64+
}
65+
66+
// NewGitHub output format outputs issues according to GitHub actions the problem matcher regexp.
1967
func NewGitHub(w io.Writer) *GitHub {
20-
return &GitHub{w: w}
68+
return &GitHub{
69+
tempPath: filepath.Join(os.TempDir(), filenameGitHubActionProblemMatchers),
70+
w: w,
71+
}
72+
}
73+
74+
func (p *GitHub) Print(issues []result.Issue) error {
75+
// Note: the file with the problem matcher definition should not be removed.
76+
// A sleep can mitigate this problem but this will be flaky.
77+
//
78+
// Result if the file is removed prematurely:
79+
// Error: Unable to process command '::add-matcher::/tmp/golangci-lint-action-problem-matchers.json' successfully.
80+
// Error: Could not find file '/tmp/golangci-lint-action-problem-matchers.json'.
81+
filename, err := p.storeProblemMatcher()
82+
if err != nil {
83+
return err
84+
}
85+
86+
_, _ = fmt.Fprintln(p.w, "::debug::problem matcher definition file: "+filename)
87+
88+
_, _ = fmt.Fprintln(p.w, "::add-matcher::"+filename)
89+
90+
for ind := range issues {
91+
_, err := fmt.Fprintln(p.w, formatIssueAsGitHub(&issues[ind]))
92+
if err != nil {
93+
return err
94+
}
95+
}
96+
97+
_, _ = fmt.Fprintln(p.w, "::remove-matcher owner=golangci-lint-action::")
98+
99+
return nil
100+
}
101+
102+
func (p *GitHub) storeProblemMatcher() (string, error) {
103+
file, err := os.Create(p.tempPath)
104+
if err != nil {
105+
return "", err
106+
}
107+
108+
defer file.Close()
109+
110+
err = json.NewEncoder(file).Encode(generateProblemMatcher())
111+
if err != nil {
112+
return "", err
113+
}
114+
115+
return file.Name(), nil
116+
}
117+
118+
func generateProblemMatcher() GitHubProblemMatchers {
119+
return GitHubProblemMatchers{
120+
Matchers: []GitHubMatcher{
121+
{
122+
Owner: "golangci-lint-action",
123+
Severity: "error",
124+
Pattern: []GitHubPattern{
125+
{
126+
Regexp: `^([^\s]+)\s+([^:]+):(\d+):(?:(\d+):)?\s+(.+)$`,
127+
Severity: 1,
128+
File: 2,
129+
Line: 3,
130+
Column: 4,
131+
Message: 5,
132+
},
133+
},
134+
},
135+
},
136+
}
21137
}
22138

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
139+
func formatIssueAsGitHub(issue *result.Issue) string {
140+
severity := defaultGitHubSeverity
26141
if issue.Severity != "" {
27142
severity = issue.Severity
28143
}
@@ -32,21 +147,11 @@ func formatIssueAsGithub(issue *result.Issue) string {
32147
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
33148
file := filepath.ToSlash(issue.FilePath())
34149

35-
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
150+
ret := fmt.Sprintf("%s\t%s:%d:", severity, file, issue.Line())
36151
if issue.Pos.Column != 0 {
37-
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
152+
ret += fmt.Sprintf("%d:", issue.Pos.Column)
38153
}
39154

40-
ret += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
155+
ret += fmt.Sprintf("\t%s (%s)", issue.Text, issue.FromLinter)
41156
return ret
42157
}
43-
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-
}
50-
}
51-
return nil
52-
}

pkg/printers/github_test.go

+110-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package printers
22

33
import (
44
"bytes"
5+
"fmt"
56
"go/token"
7+
"path/filepath"
8+
"regexp"
69
"runtime"
10+
"strings"
711
"testing"
812

913
"github.com/stretchr/testify/assert"
@@ -44,19 +48,26 @@ func TestGitHub_Print(t *testing.T) {
4448
}
4549

4650
buf := new(bytes.Buffer)
51+
4752
printer := NewGitHub(buf)
53+
printer.tempPath = filepath.Join(t.TempDir(), filenameGitHubActionProblemMatchers)
4854

4955
err := printer.Print(issues)
5056
require.NoError(t, err)
5157

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)
58+
expected := `::debug::problem matcher definition file: /tmp/golangci-lint-action-problem-matchers.json
59+
::add-matcher::/tmp/golangci-lint-action-problem-matchers.json
60+
warning path/to/filea.go:10:4: some issue (linter-a)
61+
error path/to/fileb.go:300:9: another issue (linter-b)
62+
::remove-matcher owner=golangci-lint-action::
5463
`
64+
// To support all the OS.
65+
expected = strings.ReplaceAll(expected, "/tmp/golangci-lint-action-problem-matchers.json", printer.tempPath)
5566

5667
assert.Equal(t, expected, buf.String())
5768
}
5869

59-
func Test_formatIssueAsGithub(t *testing.T) {
70+
func Test_formatIssueAsGitHub(t *testing.T) {
6071
sampleIssue := result.Issue{
6172
FromLinter: "sample-linter",
6273
Text: "some issue",
@@ -67,13 +78,13 @@ func Test_formatIssueAsGithub(t *testing.T) {
6778
Column: 4,
6879
},
6980
}
70-
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
81+
require.Equal(t, "error\tpath/to/file.go:10:4:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
7182

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

76-
func Test_formatIssueAsGithub_Windows(t *testing.T) {
87+
func Test_formatIssueAsGitHub_Windows(t *testing.T) {
7788
if runtime.GOOS != "windows" {
7889
t.Skip("Skipping test on non Windows")
7990
}
@@ -88,8 +99,99 @@ func Test_formatIssueAsGithub_Windows(t *testing.T) {
8899
Column: 4,
89100
},
90101
}
91-
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
102+
require.Equal(t, "error\tpath/to/file.go:10:4:\tsome issue (sample-linter)", formatIssueAsGitHub(&sampleIssue))
92103

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

pkg/printers/printer_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ func TestPrinter_Print_multiple(t *testing.T) {
179179
data := &report.Data{}
180180
unmarshalFile(t, "in-report-data.json", data)
181181

182-
outputPath := filepath.Join(t.TempDir(), "github-actions.txt")
182+
outputPath := filepath.Join(t.TempDir(), "teamcity.txt")
183183

184184
cfg := &config.Output{
185185
Formats: []config.OutputFormat{
186186
{
187-
Format: "github-actions",
187+
Format: "teamcity",
188188
Path: outputPath,
189189
},
190190
{
@@ -210,7 +210,7 @@ func TestPrinter_Print_multiple(t *testing.T) {
210210
err = p.Print(issues)
211211
require.NoError(t, err)
212212

213-
goldenGitHub, err := os.ReadFile(filepath.Join("testdata", "golden-github-actions.txt"))
213+
goldenGitHub, err := os.ReadFile(filepath.Join("testdata", "golden-teamcity.txt"))
214214
require.NoError(t, err)
215215

216216
actual, err := os.ReadFile(outputPath)

pkg/printers/testdata/golden-github-actions.txt

-22
This file was deleted.

0 commit comments

Comments
 (0)