|
| 1 | +package printers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/pkg/result" |
| 11 | +) |
| 12 | + |
| 13 | +// Field limits. |
| 14 | +const ( |
| 15 | + smallLimit = 255 |
| 16 | + largeLimit = 4000 |
| 17 | +) |
| 18 | + |
| 19 | +// TeamCity printer for TeamCity format. |
| 20 | +type TeamCity struct { |
| 21 | + w io.Writer |
| 22 | + escaper *strings.Replacer |
| 23 | +} |
| 24 | + |
| 25 | +// NewTeamCity output format outputs issues according to TeamCity service message format. |
| 26 | +func NewTeamCity(w io.Writer) *TeamCity { |
| 27 | + return &TeamCity{ |
| 28 | + w: w, |
| 29 | + // https://www.jetbrains.com/help/teamcity/service-messages.html#Escaped+Values |
| 30 | + escaper: strings.NewReplacer( |
| 31 | + "'", "|'", |
| 32 | + "\n", "|n", |
| 33 | + "\r", "|r", |
| 34 | + "|", "||", |
| 35 | + "[", "|[", |
| 36 | + "]", "|]", |
| 37 | + ), |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (p *TeamCity) Print(_ context.Context, issues []result.Issue) error { |
| 42 | + uniqLinters := map[string]struct{}{} |
| 43 | + |
| 44 | + for i := range issues { |
| 45 | + issue := issues[i] |
| 46 | + |
| 47 | + _, ok := uniqLinters[issue.FromLinter] |
| 48 | + if !ok { |
| 49 | + inspectionType := InspectionType{ |
| 50 | + id: issue.FromLinter, |
| 51 | + name: issue.FromLinter, |
| 52 | + description: issue.FromLinter, |
| 53 | + category: "Golangci-lint reports", |
| 54 | + } |
| 55 | + |
| 56 | + _, err := inspectionType.Print(p.w, p.escaper) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + uniqLinters[issue.FromLinter] = struct{}{} |
| 62 | + } |
| 63 | + |
| 64 | + instance := InspectionInstance{ |
| 65 | + typeID: issue.FromLinter, |
| 66 | + message: issue.Text, |
| 67 | + file: issue.FilePath(), |
| 68 | + line: issue.Line(), |
| 69 | + severity: issue.Severity, |
| 70 | + } |
| 71 | + |
| 72 | + _, err := instance.Print(p.w, p.escaper) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// InspectionType is the unique description of the conducted inspection. Each specific warning or |
| 82 | +// an error in code (inspection instance) has an inspection type. |
| 83 | +// https://www.jetbrains.com/help/teamcity/service-messages.html#Inspection+Type |
| 84 | +type InspectionType struct { |
| 85 | + id string // (mandatory) limited by 255 characters. |
| 86 | + name string // (mandatory) limited by 255 characters. |
| 87 | + description string // (mandatory) limited by 255 characters. |
| 88 | + category string // (mandatory) limited by 4000 characters. |
| 89 | +} |
| 90 | + |
| 91 | +func (i InspectionType) Print(w io.Writer, escaper *strings.Replacer) (int, error) { |
| 92 | + return fmt.Fprintf(w, "##teamcity[InspectionType id='%s' name='%s' description='%s' category='%s']\n", |
| 93 | + limit(i.id, smallLimit), limit(i.name, smallLimit), limit(escaper.Replace(i.description), largeLimit), limit(i.category, smallLimit)) |
| 94 | +} |
| 95 | + |
| 96 | +// InspectionInstance reports a specific defect, warning, error message. |
| 97 | +// Includes location, description, and various optional and custom attributes. |
| 98 | +// https://www.jetbrains.com/help/teamcity/service-messages.html#Inspection+Instance |
| 99 | +type InspectionInstance struct { |
| 100 | + typeID string // (mandatory) limited by 255 characters. |
| 101 | + message string // (optional) limited by 4000 characters. |
| 102 | + file string // (mandatory) file path limited by 4000 characters. |
| 103 | + line int // (optional) line of the file. |
| 104 | + severity string // (optional) any linter severity. |
| 105 | +} |
| 106 | + |
| 107 | +func (i InspectionInstance) Print(w io.Writer, replacer *strings.Replacer) (int, error) { |
| 108 | + return fmt.Fprintf(w, "##teamcity[inspection typeId='%s' message='%s' file='%s' line='%d' SEVERITY='%s']\n", |
| 109 | + limit(i.typeID, smallLimit), |
| 110 | + limit(replacer.Replace(i.message), largeLimit), |
| 111 | + limit(i.file, largeLimit), |
| 112 | + i.line, strings.ToUpper(i.severity)) |
| 113 | +} |
| 114 | + |
| 115 | +func limit(s string, max int) string { |
| 116 | + var size, count int |
| 117 | + for i := 0; i < max && count < len(s); i++ { |
| 118 | + _, size = utf8.DecodeRuneInString(s[count:]) |
| 119 | + count += size |
| 120 | + } |
| 121 | + |
| 122 | + return s[:count] |
| 123 | +} |
0 commit comments