output: convert backslashes to forward slashes for GitHub Action annotations printer (#4149)
Some checks failed
Release a tag / release (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile]) (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/alpine.Dockerfile]) (push) Has been cancelled

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
This commit is contained in:
Yury Gargay 2023-10-24 14:38:15 +02:00 committed by GitHub
parent 3d582093e6
commit 9b20d49dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package printers
import (
"fmt"
"io"
"path/filepath"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -26,7 +27,12 @@ func formatIssueAsGithub(issue *result.Issue) string {
severity = issue.Severity
}
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, issue.FilePath(), issue.Line())
// Convert backslashes to forward slashes.
// This is needed when running on windows.
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
file := filepath.ToSlash(issue.FilePath())
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
if issue.Pos.Column != 0 {
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
}

View File

@ -4,6 +4,7 @@ package printers
import (
"bytes"
"go/token"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
@ -72,3 +73,24 @@ func TestFormatGithubIssue(t *testing.T) {
sampleIssue.Pos.Column = 0
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
}
func TestFormatGithubIssueWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non Windows")
}
sampleIssue := result.Issue{
FromLinter: "sample-linter",
Text: "some issue",
Pos: token.Position{
Filename: "path\\to\\file.go",
Offset: 2,
Line: 10,
Column: 4,
},
}
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
sampleIssue.Pos.Column = 0
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
}