-
Notifications
You must be signed in to change notification settings - Fork 256
/
runner.go
204 lines (187 loc) · 5.68 KB
/
runner.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
// Copyright 2020-2023 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 internal
import (
"context"
"strings"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/protosource"
"github.com/bufbuild/buf/private/pkg/protoversion"
"github.com/bufbuild/buf/private/pkg/stringutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"go.uber.org/zap"
)
const (
tracerName = "bufbuild/buf"
)
// Runner is a runner.
type Runner struct {
logger *zap.Logger
ignorePrefix string
tracer trace.Tracer
}
// NewRunner returns a new Runner.
func NewRunner(logger *zap.Logger, options ...RunnerOption) *Runner {
runner := &Runner{
logger: logger,
tracer: otel.GetTracerProvider().Tracer(tracerName),
}
for _, option := range options {
option(runner)
}
return runner
}
// RunnerOption is an option for a new Runner.
type RunnerOption func(*Runner)
// RunnerWithIgnorePrefix returns a new RunnerOption that sets the comment ignore prefix.
//
// This will result in failures where the location has "ignore_prefix id" in the leading
// comment being ignored.
//
// The default is to not enable comment ignores.
func RunnerWithIgnorePrefix(ignorePrefix string) RunnerOption {
return func(runner *Runner) {
runner.ignorePrefix = ignorePrefix
}
}
// Check runs the Rules.
func (r *Runner) Check(ctx context.Context, config *Config, previousFiles []protosource.File, files []protosource.File) ([]bufanalysis.FileAnnotation, error) {
rules := config.Rules
if len(rules) == 0 {
return nil, nil
}
ctx, span := r.tracer.Start(ctx, "check", trace.WithAttributes(
attribute.Key("num_files").Int(len(files)),
attribute.Key("num_rules").Int(len(rules)),
))
defer span.End()
ignoreFunc := r.newIgnoreFunc(config)
var fileAnnotations []bufanalysis.FileAnnotation
resultC := make(chan *result, len(rules))
for _, rule := range rules {
rule := rule
go func() {
iFileAnnotations, iErr := rule.check(ignoreFunc, previousFiles, files)
resultC <- newResult(iFileAnnotations, iErr)
}()
}
var err error
for i := 0; i < len(rules); i++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-resultC:
fileAnnotations = append(fileAnnotations, result.FileAnnotations...)
err = multierr.Append(err, result.Err)
}
}
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return nil, err
}
bufanalysis.SortFileAnnotations(fileAnnotations)
return fileAnnotations, nil
}
func (r *Runner) newIgnoreFunc(config *Config) IgnoreFunc {
return func(id string, descriptors []protosource.Descriptor, locations []protosource.Location) bool {
if idIsIgnored(id, descriptors, config) {
return true
}
// if ignorePrefix is empty, comment ignores are not enabled for the runner
// this is the case with breaking changes
if r.ignorePrefix != "" && config.AllowCommentIgnores &&
locationsAreIgnored(id, r.ignorePrefix, locations, config) {
return true
}
if config.IgnoreUnstablePackages {
for _, descriptor := range descriptors {
if descriptorPackageIsUnstable(descriptor) {
return true
}
}
}
return false
}
}
func idIsIgnored(id string, descriptors []protosource.Descriptor, config *Config) bool {
for _, descriptor := range descriptors {
// OR of descriptors
if idIsIgnoredForDescriptor(id, descriptor, config) {
return true
}
}
return false
}
func idIsIgnoredForDescriptor(id string, descriptor protosource.Descriptor, config *Config) bool {
if descriptor == nil {
return false
}
path := descriptor.File().Path()
if normalpath.MapHasEqualOrContainingPath(config.IgnoreRootPaths, path, normalpath.Relative) {
return true
}
if id == "" {
return false
}
ignoreRootPaths, ok := config.IgnoreIDToRootPaths[id]
if !ok {
return false
}
return normalpath.MapHasEqualOrContainingPath(ignoreRootPaths, path, normalpath.Relative)
}
func locationsAreIgnored(id string, ignorePrefix string, locations []protosource.Location, config *Config) bool {
// we already check that ignorePrefix is non-empty, but just doing here for safety
if id == "" || ignorePrefix == "" {
return false
}
fullIgnorePrefix := ignorePrefix + " " + id
for _, location := range locations {
if location != nil {
if leadingComments := location.LeadingComments(); leadingComments != "" {
for _, line := range stringutil.SplitTrimLinesNoEmpty(leadingComments) {
if strings.HasPrefix(line, fullIgnorePrefix) {
return true
}
}
}
}
}
return false
}
func descriptorPackageIsUnstable(descriptor protosource.Descriptor) bool {
if descriptor == nil {
return false
}
packageVersion, ok := protoversion.NewPackageVersionForPackage(descriptor.File().Package())
if !ok {
return false
}
return packageVersion.StabilityLevel() != protoversion.StabilityLevelStable
}
type result struct {
FileAnnotations []bufanalysis.FileAnnotation
Err error
}
func newResult(fileAnnotations []bufanalysis.FileAnnotation, err error) *result {
return &result{
FileAnnotations: fileAnnotations,
Err: err,
}
}