-
Notifications
You must be signed in to change notification settings - Fork 277
/
checker.go
172 lines (157 loc) · 4.46 KB
/
checker.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
// Copyright 2020-2022 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 bandeps
import (
"context"
"sync"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/thread"
"go.opencensus.io/trace"
"go.uber.org/zap"
)
type checker struct {
logger *zap.Logger
runner command.Runner
}
func newChecker(logger *zap.Logger, runner command.Runner) *checker {
return &checker{
logger: logger,
runner: runner,
}
}
func (c *checker) Check(
ctx context.Context,
envStdioContainer app.EnvStdioContainer,
externalConfig ExternalConfig,
) ([]Violation, error) {
state := newState(c.logger, envStdioContainer, c.runner)
if err := c.populateState(ctx, state, externalConfig); err != nil {
return nil, err
}
for _, externalBanConfig := range externalConfig.Bans {
if err := c.checkBan(ctx, state, externalBanConfig); err != nil {
return nil, err
}
}
return state.Violations(), nil
}
func (c *checker) checkBan(
ctx context.Context,
state *state,
externalBanConfig ExternalBanConfig,
) error {
ctx, span := trace.StartSpan(ctx, "checkBan")
defer span.End()
packages, err := c.getPackages(ctx, state, externalBanConfig.Packages)
if err != nil {
return err
}
banPackages, err := c.getPackages(ctx, state, externalBanConfig.Deps)
if err != nil {
return err
}
for pkg := range packages {
deps, err := state.DepsForPackages(ctx, pkg)
if err != nil {
return err
}
for dep := range deps {
if _, ok := banPackages[dep]; ok {
state.AddViolation(
newViolation(
pkg,
dep,
externalBanConfig.Note,
),
)
}
}
}
return nil
}
func (c *checker) getPackages(
ctx context.Context,
state *state,
externalPackageConfig ExternalPackageConfig,
) (map[string]struct{}, error) {
usePackages, err := state.PackagesForPackageExpressions(ctx, externalPackageConfig.Use...)
if err != nil {
return nil, err
}
exceptPackages, err := state.PackagesForPackageExpressions(ctx, externalPackageConfig.Except...)
if err != nil {
return nil, err
}
subtractMaps(usePackages, exceptPackages)
return usePackages, nil
}
func (c *checker) populateState(ctx context.Context, state *state, externalConfig ExternalConfig) error {
ctx, span := trace.StartSpan(ctx, "populateState")
defer span.End()
var depPackageExpressions []string
var packageExpressions []string
for _, externalBanConfig := range externalConfig.Bans {
depPackageExpressions = append(depPackageExpressions, externalBanConfig.Packages.Use...)
depPackageExpressions = append(depPackageExpressions, externalBanConfig.Packages.Except...)
packageExpressions = append(packageExpressions, externalBanConfig.Deps.Use...)
packageExpressions = append(packageExpressions, externalBanConfig.Deps.Except...)
}
depPackages := make(map[string]struct{})
var lock sync.Mutex
var jobs []func(context.Context) error
for _, packageExpression := range depPackageExpressions {
packageExpression := packageExpression
jobs = append(
jobs,
func(ctx context.Context) error {
pkgs, err := state.PackagesForPackageExpressions(ctx, packageExpression)
if err != nil {
return err
}
lock.Lock()
addMaps(depPackages, pkgs)
lock.Unlock()
return nil
},
)
}
for _, packageExpression := range packageExpressions {
packageExpression := packageExpression
jobs = append(
jobs,
func(ctx context.Context) error {
_, err := state.PackagesForPackageExpressions(ctx, packageExpression)
return err
},
)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancel(cancel)); err != nil {
return err
}
jobs = make([]func(context.Context) error, 0)
for pkg := range depPackages {
pkg := pkg
jobs = append(
jobs,
func(ctx context.Context) error {
_, err := state.DepsForPackages(ctx, pkg)
return err
},
)
}
return thread.Parallelize(ctx, jobs, thread.ParallelizeWithCancel(cancel))
}