-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_iowriter.go
285 lines (259 loc) · 7.45 KB
/
comment_iowriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package reviewdog
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/haya14busa/go-sarif/sarif"
"github.com/reviewdog/reviewdog/proto/rdf"
"google.golang.org/protobuf/encoding/protojson"
)
var _ CommentService = &RawCommentWriter{}
// RawCommentWriter is comment writer which writes results to given writer
// without any formatting.
type RawCommentWriter struct {
w io.Writer
}
func NewRawCommentWriter(w io.Writer) *RawCommentWriter {
return &RawCommentWriter{w: w}
}
func (s *RawCommentWriter) Post(_ context.Context, c *Comment) error {
_, err := fmt.Fprintln(s.w, c.Result.Diagnostic.OriginalOutput)
return err
}
var _ CommentService = &UnifiedCommentWriter{}
// UnifiedCommentWriter is comment writer which writes results to given writer
// in one of following unified formats.
//
// Format:
// - <file>: [<tool name>] <message>
// - <file>:<lnum>: [<tool name>] <message>
// - <file>:<lnum>:<col>: [<tool name>] <message>
//
// where <message> can be multiple lines.
type UnifiedCommentWriter struct {
w io.Writer
}
func NewUnifiedCommentWriter(w io.Writer) *UnifiedCommentWriter {
return &UnifiedCommentWriter{w: w}
}
func (mc *UnifiedCommentWriter) Post(_ context.Context, c *Comment) error {
loc := c.Result.Diagnostic.GetLocation()
s := loc.GetPath()
start := loc.GetRange().GetStart()
if start.GetLine() > 0 {
s += fmt.Sprintf(":%d", start.GetLine())
if start.GetColumn() > 0 {
s += fmt.Sprintf(":%d", start.GetColumn())
}
}
s += fmt.Sprintf(": [%s] %s", c.ToolName, c.Result.Diagnostic.GetMessage())
_, err := fmt.Fprintln(mc.w, s)
return err
}
var _ CommentService = &RDJSONLCommentWriter{}
// RDJSONLCommentWriter
type RDJSONLCommentWriter struct {
w io.Writer
}
func NewRDJSONLCommentWriter(w io.Writer) *RDJSONLCommentWriter {
return &RDJSONLCommentWriter{w: w}
}
func (cw *RDJSONLCommentWriter) Post(_ context.Context, c *Comment) error {
if c.ToolName != "" && c.Result.Diagnostic.GetSource().GetName() == "" {
c.Result.Diagnostic.Source = &rdf.Source{
Name: c.ToolName,
}
}
// Remove OriginalOutput. It's used internally and we shouldn't expose it as output.
c.Result.Diagnostic.OriginalOutput = ""
b, err := protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: false,
Multiline: false,
EmitUnpopulated: false,
EmitDefaultValues: false,
}.Marshal(c.Result.Diagnostic)
if err != nil {
return err
}
if _, err = cw.w.Write(b); err != nil {
return err
}
if _, err := cw.w.Write([]byte("\n")); err != nil {
return err
}
return nil
}
var _ CommentService = &RDJSONCommentWriter{}
// RDJSONCommentWriter
type RDJSONCommentWriter struct {
w io.Writer
comments []*Comment
toolName string
}
func NewRDJSONCommentWriter(w io.Writer, toolName string) *RDJSONCommentWriter {
return &RDJSONCommentWriter{w: w, toolName: toolName}
}
func (cw *RDJSONCommentWriter) Post(_ context.Context, c *Comment) error {
cw.comments = append(cw.comments, c)
return nil
}
func (cw *RDJSONCommentWriter) Flush(_ context.Context) error {
result := &rdf.DiagnosticResult{
Source: &rdf.Source{
Name: cw.toolName,
},
Diagnostics: make([]*rdf.Diagnostic, 0, len(cw.comments)),
}
for _, c := range cw.comments {
// Remove OriginalOutput. It's used internally and we shouldn't expose it as output.
c.Result.Diagnostic.OriginalOutput = ""
result.Diagnostics = append(result.Diagnostics, c.Result.Diagnostic)
}
b, err := protojson.MarshalOptions{
UseProtoNames: true,
UseEnumNumbers: false,
Multiline: true,
EmitUnpopulated: false,
EmitDefaultValues: false,
}.Marshal(result)
if err != nil {
return err
}
if _, err = cw.w.Write(b); err != nil {
return err
}
if _, err := cw.w.Write([]byte("\n")); err != nil {
return err
}
return nil
}
var _ CommentService = &SARIFCommentWriter{}
// SARIFCommentWriter
type SARIFCommentWriter struct {
w io.Writer
comments []*Comment
toolName string
}
func NewSARIFCommentWriter(w io.Writer, toolName string) *SARIFCommentWriter {
return &SARIFCommentWriter{w: w, toolName: toolName}
}
func (cw *SARIFCommentWriter) Post(_ context.Context, c *Comment) error {
cw.comments = append(cw.comments, c)
return nil
}
func (cw *SARIFCommentWriter) Flush(_ context.Context) error {
run := sarif.Run{
Tool: sarif.Tool{
Driver: sarif.ToolComponent{
Name: cw.toolName,
Rules: make([]sarif.ReportingDescriptor, 0),
},
},
}
seenRules := make(map[string]bool)
for _, c := range cw.comments {
result := sarif.Result{
Message: sarif.Message{
Text: sarif.String(c.Result.Diagnostic.Message),
},
}
if code := c.Result.Diagnostic.GetCode(); code.GetValue() != "" {
result.RuleID = sarif.String(code.GetValue())
if seen := seenRules[code.GetValue()]; !seen {
seenRules[code.GetValue()] = true
rd := sarif.ReportingDescriptor{
ID: code.GetValue(),
}
if code.GetUrl() != "" {
rd.HelpURI = sarif.String(code.GetUrl())
}
run.Tool.Driver.Rules = append(run.Tool.Driver.Rules, rd)
}
}
level := severity2level(c.Result.Diagnostic.GetSeverity())
if level != sarif.None {
result.Level = &level
}
artifactLoc := sarif.ArtifactLocation{
URI: sarif.String(c.Result.Diagnostic.GetLocation().GetPath()),
}
result.Locations = []sarif.Location{{
PhysicalLocation: &sarif.PhysicalLocation{
ArtifactLocation: &artifactLoc,
Region: range2region(c.Result.Diagnostic.GetLocation().GetRange()),
},
}}
if len(c.Result.Diagnostic.GetSuggestions()) > 0 {
result.Fixes = make([]sarif.Fix, 0)
for _, suggestion := range c.Result.Diagnostic.GetSuggestions() {
result.Fixes = append(result.Fixes, sarif.Fix{
ArtifactChanges: []sarif.ArtifactChange{
{
ArtifactLocation: artifactLoc,
Replacements: []sarif.Replacement{{
DeletedRegion: *range2region(suggestion.GetRange()),
InsertedContent: &sarif.ArtifactContent{
Text: sarif.String(suggestion.GetText()),
},
}},
},
},
})
}
}
if len(c.Result.Diagnostic.GetRelatedLocations()) > 0 {
result.RelatedLocations = make([]sarif.Location, 0)
for _, relLoc := range c.Result.Diagnostic.GetRelatedLocations() {
result.RelatedLocations = append(result.RelatedLocations, sarif.Location{
PhysicalLocation: &sarif.PhysicalLocation{
ArtifactLocation: &sarif.ArtifactLocation{
URI: sarif.String(relLoc.GetLocation().GetPath()),
},
Region: range2region(relLoc.GetLocation().GetRange()),
},
Message: &sarif.Message{
Text: sarif.String(relLoc.Message),
},
})
}
}
run.Results = append(run.Results, result)
}
slf := sarif.NewSarif()
slf.Runs = []sarif.Run{run}
encoder := json.NewEncoder(cw.w)
encoder.SetIndent("", " ")
return encoder.Encode(slf)
}
func range2region(rng *rdf.Range) *sarif.Region {
region := &sarif.Region{}
start := rng.GetStart()
end := rng.GetEnd()
if start.GetLine() > 0 {
region.StartLine = sarif.Int64(int64(start.GetLine()))
}
if start.GetColumn() > 0 {
// Column is not usually unicodeCodePoints, but let's just keep it
// as is...
region.StartColumn = sarif.Int64(int64(start.GetColumn()))
}
if end.GetLine() > 0 {
region.EndLine = sarif.Int64(int64(end.GetLine()))
}
if end.GetColumn() > 0 {
region.EndColumn = sarif.Int64(int64(end.GetColumn()))
}
return region
}
func severity2level(s rdf.Severity) sarif.Level {
switch s {
case rdf.Severity_ERROR:
return sarif.Error
case rdf.Severity_WARNING:
return sarif.Warning
default:
return sarif.None
}
}