-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathbufanalysis.go
More file actions
263 lines (241 loc) · 7.69 KB
/
bufanalysis.go
File metadata and controls
263 lines (241 loc) · 7.69 KB
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
// Copyright 2020-2026 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufanalysis
import (
"fmt"
"io"
"strconv"
"strings"
"github.com/bufbuild/protocompile/experimental/report"
)
const (
// FormatText is the text format for FileAnnotations.
FormatText Format = iota + 1
// FormatJSON is the JSON format for FileAnnotations.
FormatJSON
// FormatMSVS is the MSVS format for FileAnnotations.
FormatMSVS
// FormatJUnit is the JUnit format for FileAnnotations.
FormatJUnit
// FormatGithubActions is the Github Actions format for FileAnnotations.
//
// See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message.
FormatGithubActions
// FormatGitLabCodeQuality is the Code Qualiy report format for FileAnnotations.
//
// See https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format
// GitLab Code Quality report must be a single JSON array of GitLab Code Quality Issues
// objects with the specified properties.
FormatGitLabCodeQuality
)
var (
// AllFormatStrings is all format strings without aliases.
//
// Sorted in the order we want to display them.
AllFormatStrings = []string{
"text",
"json",
"msvs",
"junit",
"github-actions",
"gitlab-code-quality",
}
// AllFormatStringsWithAliases is all format strings with aliases.
//
// Sorted in the order we want to display them.
AllFormatStringsWithAliases = []string{
"text",
"gcc",
"json",
"msvs",
"junit",
"github-actions",
"gitlab-code-quality",
}
stringToFormat = map[string]Format{
"text": FormatText,
// alias for text
"gcc": FormatText,
"json": FormatJSON,
"msvs": FormatMSVS,
"junit": FormatJUnit,
"github-actions": FormatGithubActions,
"gitlab-code-quality": FormatGitLabCodeQuality,
}
formatToString = map[Format]string{
FormatText: "text",
FormatJSON: "json",
FormatMSVS: "msvs",
FormatJUnit: "junit",
FormatGithubActions: "github-actions",
FormatGitLabCodeQuality: "gitlab-code-quality",
}
)
// Format is a FileAnnotation format.
type Format int
// String implements fmt.Stringer.
func (f Format) String() string {
s, ok := formatToString[f]
if !ok {
return strconv.Itoa(int(f))
}
return s
}
// ParseFormat parses the Format.
//
// The empty strings defaults to FormatText.
func ParseFormat(s string) (Format, error) {
s = strings.ToLower(strings.TrimSpace(s))
if s == "" {
return FormatText, nil
}
f, ok := stringToFormat[s]
if ok {
return f, nil
}
return 0, fmt.Errorf("unknown format: %q", s)
}
// FileInfo is a minimal FileInfo interface.
type FileInfo interface {
Path() string
ExternalPath() string
}
// FileAnnotation is a file annotation.
type FileAnnotation interface {
// Stringer returns the string representation for this FileAnnotation.
fmt.Stringer
// FileInfo is the FileInfo for this annotation.
//
// This may be nil.
FileInfo() FileInfo
// StartLine is the starting line.
//
// If the starting line is not known, this will be 0.
StartLine() int
// StartColumn is the starting column.
//
// If the starting column is not known, this will be 0.
StartColumn() int
// EndLine is the ending line.
//
// If the ending line is not known, this will be 0.
// If the ending line is the same as the starting line, this will be explicitly
// set to the same value as start_line.
EndLine() int
// EndColumn is the ending column.
//
// If the ending column is not known, this will be 0.
// If the ending column is the same as the starting column, this will be explicitly
// set to the same value as start_column.
EndColumn() int
// Type is the type of annotation, typically an ID representing a failure type.
Type() string
// Message is the message of the annotation.
Message() string
// PluginName is the name of the plugin that the annotation originated from.
//
// May be empty if this annotation did not originate from a plugin.
// This may be added to the printed message field for certain printers.
PluginName() string
// PolicyName is the name of the policy that the annotation originated from.
//
// May be empty if this annotation did not originate from a policy.
// This may be added to the printed message field for certain printers.
PolicyName() string
isFileAnnotation()
}
// NewFileAnnotation returns a new FileAnnotation.
func NewFileAnnotation(
fileInfo FileInfo,
startLine int,
startColumn int,
endLine int,
endColumn int,
typeString string,
message string,
pluginName string,
policyName string,
) FileAnnotation {
return newFileAnnotation(
fileInfo,
startLine,
startColumn,
endLine,
endColumn,
typeString,
message,
pluginName,
policyName,
)
}
// FileAnnotationSet is a set of FileAnnotations.
type FileAnnotationSet interface {
// Stringer returns the string representation for this FileAnnotationSet.
fmt.Stringer
// error returns an error for this FileAnnotationSet. It will use the text format
// to create an error message.
error
// FileAnnotations returns the FileAnnotations in the set.
//
// This will always be non-empty.
// These will be deduplicated and sorted.
FileAnnotations() []FileAnnotation
// diagnosticReport returns the diagnostic [report.Report] for the [FileAnnotationSet],
// if set.
//
// This may be nil. If non-nil, it will be used as the output for
// [PrintFileAnnotationSet] when the format is set to "text", rendered
// with a [report.Renderer] configured by the given print options.
diagnosticReport() *report.Report
isFileAnnotationSet()
}
// NewFileAnnotationSet returns a new FileAnnotationSet.
//
// If len(fileAnnotations) is 0, this returns nil.
//
// The diagnosticReport is the [report.Report] from the compiler, if available.
// If non-nil, it will be rendered by [PrintFileAnnotationSet] when the format
// is "text". Otherwise, the individual file annotations will be used, same as
// all other print formats.
func NewFileAnnotationSet(diagnosticReport *report.Report, fileAnnotations ...FileAnnotation) FileAnnotationSet {
return newFileAnnotationSet(diagnosticReport, fileAnnotations)
}
// PrintFileAnnotationSet prints the file annotations separated by newlines.
func PrintFileAnnotationSet(writer io.Writer, fileAnnotationSet FileAnnotationSet, formatString string) error {
format, err := ParseFormat(formatString)
if err != nil {
return err
}
switch format {
case FormatText:
if diagnosticReport := fileAnnotationSet.diagnosticReport(); diagnosticReport != nil {
renderer := report.Renderer{}
_, _, err := renderer.Render(diagnosticReport, writer)
return err
}
return printAsText(writer, fileAnnotationSet.FileAnnotations())
case FormatJSON:
return printAsJSON(writer, fileAnnotationSet.FileAnnotations())
case FormatMSVS:
return printAsMSVS(writer, fileAnnotationSet.FileAnnotations())
case FormatJUnit:
return printAsJUnit(writer, fileAnnotationSet.FileAnnotations())
case FormatGithubActions:
return printAsGithubActions(writer, fileAnnotationSet.FileAnnotations())
case FormatGitLabCodeQuality:
return printAsGitLabCodeQuality(writer, fileAnnotationSet.FileAnnotations())
default:
return fmt.Errorf("unknown FileAnnotation Format: %v", format)
}
}