-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathbatch_spec.go
More file actions
269 lines (229 loc) · 9.2 KB
/
batch_spec.go
File metadata and controls
269 lines (229 loc) · 9.2 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
264
265
266
267
268
269
package batches
import (
"fmt"
"strings"
"github.com/sourcegraph/sourcegraph/lib/batches/env"
"github.com/sourcegraph/sourcegraph/lib/batches/overridable"
"github.com/sourcegraph/sourcegraph/lib/batches/schema"
"github.com/sourcegraph/sourcegraph/lib/batches/template"
"github.com/sourcegraph/sourcegraph/lib/batches/yaml"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// Some general notes about the struct definitions below.
//
// 1. They map _very_ closely to the batch spec JSON schema. We don't
// auto-generate the types because we need YAML support (more on that in a
// moment) and because no generator can currently handle oneOf fields
// gracefully in Go, but that's a potential future enhancement.
//
// 2. Fields are tagged with _both_ JSON and YAML tags. Internally, the JSON
// schema library needs to be able to marshal the struct to JSON for
// validation, so we need to ensure that we're generating the right JSON to
// represent the YAML that we unmarshalled.
//
// 3. All JSON tags include omitempty so that the schema validation can pick up
// omitted fields. The other option here was to have everything unmarshal to
// pointers, which is ugly and inefficient.
type BatchSpec struct {
Version int `json:"version,omitempty" yaml:"version"`
Name string `json:"name,omitempty" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description"`
On []OnQueryOrRepository `json:"on,omitempty" yaml:"on"`
Workspaces []WorkspaceConfiguration `json:"workspaces,omitempty" yaml:"workspaces"`
Steps []Step `json:"steps,omitempty" yaml:"steps"`
TransformChanges *TransformChanges `json:"transformChanges,omitempty" yaml:"transformChanges,omitempty"`
ImportChangesets []ImportChangeset `json:"importChangesets,omitempty" yaml:"importChangesets"`
ChangesetTemplate *ChangesetTemplate `json:"changesetTemplate,omitempty" yaml:"changesetTemplate"`
}
type ChangesetTemplate struct {
Title string `json:"title,omitempty" yaml:"title"`
Body string `json:"body,omitempty" yaml:"body"`
Branch string `json:"branch,omitempty" yaml:"branch"`
Fork *bool `json:"fork,omitempty" yaml:"fork"`
Commit ExpandedGitCommitDescription `json:"commit" yaml:"commit"`
Published *overridable.BoolOrString `json:"published" yaml:"published"`
}
type GitCommitAuthor struct {
Name string `json:"name" yaml:"name"`
Email string `json:"email" yaml:"email"`
}
type ExpandedGitCommitDescription struct {
Message string `json:"message,omitempty" yaml:"message"`
Author *GitCommitAuthor `json:"author,omitempty" yaml:"author"`
}
type ImportChangeset struct {
Repository string `json:"repository" yaml:"repository"`
ExternalIDs []any `json:"externalIDs" yaml:"externalIDs"`
}
type WorkspaceConfiguration struct {
RootAtLocationOf string `json:"rootAtLocationOf,omitempty" yaml:"rootAtLocationOf"`
In string `json:"in,omitempty" yaml:"in"`
OnlyFetchWorkspace bool `json:"onlyFetchWorkspace,omitempty" yaml:"onlyFetchWorkspace"`
}
type OnQueryOrRepository struct {
RepositoriesMatchingQuery string `json:"repositoriesMatchingQuery,omitempty" yaml:"repositoriesMatchingQuery"`
Repository string `json:"repository,omitempty" yaml:"repository"`
Branch string `json:"branch,omitempty" yaml:"branch"`
Branches []string `json:"branches,omitempty" yaml:"branches"`
}
var ErrConflictingBranches = NewValidationError(errors.New("both branch and branches specified"))
func (oqor *OnQueryOrRepository) GetBranches() ([]string, error) {
if oqor.Branch != "" {
if len(oqor.Branches) > 0 {
return nil, ErrConflictingBranches
}
return []string{oqor.Branch}, nil
}
return oqor.Branches, nil
}
type Step struct {
Run string `json:"run,omitempty" yaml:"run"`
Container string `json:"container,omitempty" yaml:"container"`
Env env.Environment `json:"env" yaml:"env"`
Files map[string]string `json:"files,omitempty" yaml:"files,omitempty"`
Outputs Outputs `json:"outputs,omitempty" yaml:"outputs,omitempty"`
Mount []Mount `json:"mount,omitempty" yaml:"mount,omitempty"`
If any `json:"if,omitempty" yaml:"if,omitempty"`
}
func (s *Step) IfCondition() string {
switch v := s.If.(type) {
case bool:
if v {
return "true"
}
return "false"
case string:
return v
default:
return ""
}
}
type Outputs map[string]Output
type Output struct {
Value string `json:"value,omitempty" yaml:"value,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
}
type TransformChanges struct {
Group []Group `json:"group,omitempty" yaml:"group"`
}
type Group struct {
Directory string `json:"directory,omitempty" yaml:"directory"`
Branch string `json:"branch,omitempty" yaml:"branch"`
Repository string `json:"repository,omitempty" yaml:"repository"`
}
type Mount struct {
Mountpoint string `json:"mountpoint" yaml:"mountpoint"`
Path string `json:"path" yaml:"path"`
}
func ParseBatchSpec(data []byte) (*BatchSpec, error) {
return parseBatchSpec(schema.BatchSpecJSON, data)
}
func parseBatchSpec(schema string, data []byte) (*BatchSpec, error) {
var spec BatchSpec
if err := yaml.UnmarshalValidate(schema, data, &spec); err != nil {
var multiErr errors.MultiError
if errors.As(err, &multiErr) {
var newMultiError error
for _, e := range multiErr.Errors() {
// In case of `name` we try to make the error message more user-friendly.
if strings.Contains(e.Error(), "name: Does not match pattern") {
newMultiError = errors.Append(newMultiError, NewValidationError(errors.Newf("The batch change name can only contain word characters, dots and dashes. No whitespace or newlines allowed.")))
} else {
newMultiError = errors.Append(newMultiError, NewValidationError(e))
}
}
return nil, newMultiError
}
return nil, err
}
var errs error
if len(spec.Steps) != 0 && spec.ChangesetTemplate == nil {
errs = errors.Append(errs, NewValidationError(errors.New("batch spec includes steps but no changesetTemplate")))
}
for i, step := range spec.Steps {
for _, mount := range step.Mount {
if strings.ContainsAny(mount.Path, invalidMountCharacters) {
errs = errors.Append(errs, NewValidationError(errors.Newf("step %d mount path contains invalid characters", i+1)))
}
if strings.ContainsAny(mount.Mountpoint, invalidMountCharacters) {
errs = errors.Append(errs, NewValidationError(errors.Newf("step %d mount mountpoint contains invalid characters", i+1)))
}
}
for name := range step.Files {
if strings.ContainsAny(name, invalidMountCharacters) {
errs = errors.Append(errs, NewValidationError(errors.Newf("step %d files target path contains invalid characters", i+1)))
}
}
}
return &spec, errs
}
// docker uses Golang's `encoding/csv` library to parse arguments passed to `--mount`
const invalidMountCharacters = ",\"\n\r"
func (on *OnQueryOrRepository) String() string {
if on.RepositoriesMatchingQuery != "" {
return on.RepositoriesMatchingQuery
} else if on.Repository != "" {
return "repository:" + on.Repository
}
return fmt.Sprintf("%v", *on)
}
// BatchSpecValidationError is returned when parsing/using values from the batch spec failed.
type BatchSpecValidationError struct {
err error
}
func NewValidationError(err error) BatchSpecValidationError {
return BatchSpecValidationError{err}
}
func (e BatchSpecValidationError) Error() string {
return e.err.Error()
}
func IsValidationError(err error) bool {
return errors.HasType[*BatchSpecValidationError](err)
}
// SkippedStepsForRepo calculates the steps required to run on the given repo.
func SkippedStepsForRepo(spec *BatchSpec, repoName string, fileMatches []string) (skipped map[int]struct{}, err error) {
skipped = map[int]struct{}{}
for idx, step := range spec.Steps {
// If no if condition is set the step is always run.
if step.IfCondition() == "" {
continue
}
batchChange := template.BatchChangeAttributes{
Name: spec.Name,
Description: spec.Description,
}
// TODO: This step ctx is incomplete, is this allowed?
// We can at least optimize further here and do more static evaluation
// when we have a cached result for the previous step.
stepCtx := &template.StepContext{
Repository: template.Repository{
Name: repoName,
FileMatches: fileMatches,
},
BatchChange: batchChange,
}
static, boolVal, err := template.IsStaticBool(step.IfCondition(), stepCtx)
if err != nil {
return nil, err
}
if static && !boolVal {
skipped[idx] = struct{}{}
}
}
return skipped, nil
}
// RequiredEnvVars inspects all steps for outer environment variables used and
// compiles a deduplicated list from those.
func (s *BatchSpec) RequiredEnvVars() []string {
requiredMap := map[string]struct{}{}
required := []string{}
for _, step := range s.Steps {
for _, v := range step.Env.OuterVars() {
if _, ok := requiredMap[v]; !ok {
requiredMap[v] = struct{}{}
required = append(required, v)
}
}
}
return required
}