2021-11-14 20:01:54 +00:00
|
|
|
package printers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-02-24 16:33:24 +00:00
|
|
|
"io"
|
2021-11-14 20:01:54 +00:00
|
|
|
|
|
|
|
"github.com/golangci/golangci-lint/pkg/result"
|
|
|
|
)
|
|
|
|
|
|
|
|
type github struct {
|
2022-02-24 16:33:24 +00:00
|
|
|
w io.Writer
|
2021-11-14 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultGithubSeverity = "error"
|
|
|
|
|
2021-11-16 20:07:53 +00:00
|
|
|
// NewGithub output format outputs issues according to GitHub actions format:
|
2021-11-14 20:01:54 +00:00
|
|
|
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
|
2022-02-24 16:33:24 +00:00
|
|
|
func NewGithub(w io.Writer) Printer {
|
|
|
|
return &github{w: w}
|
2021-11-14 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
|
|
|
|
func formatIssueAsGithub(issue *result.Issue) string {
|
|
|
|
severity := defaultGithubSeverity
|
|
|
|
if issue.Severity != "" {
|
|
|
|
severity = issue.Severity
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, issue.FilePath(), issue.Line())
|
|
|
|
if issue.Pos.Column != 0 {
|
|
|
|
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += fmt.Sprintf("::%s (%s)", issue.Text, issue.FromLinter)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
func (p *github) Print(_ context.Context, issues []result.Issue) error {
|
2021-11-14 20:01:54 +00:00
|
|
|
for ind := range issues {
|
2022-02-24 16:33:24 +00:00
|
|
|
_, err := fmt.Fprintln(p.w, formatIssueAsGithub(&issues[ind]))
|
2021-11-14 20:01:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|