-
Notifications
You must be signed in to change notification settings - Fork 51
/
builder.go
401 lines (379 loc) · 12.5 KB
/
builder.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package pipelinescheduler
import (
"github.com/davecgh/go-spew/spew"
schedulerapi "github.com/jenkins-x-plugins/jx-gitops/pkg/apis/scheduler/v1alpha1"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/lighthouse-client/pkg/config/job"
"github.com/pkg/errors"
)
// Build combines the slice of schedulers into one, with the most specific schedule config defined last
func Build(schedulers []*schedulerapi.SchedulerSpec) (*schedulerapi.SchedulerSpec, error) {
var answer *schedulerapi.SchedulerSpec
for i := len(schedulers) - 1; i >= 0; i-- {
parent := schedulers[i]
if answer == nil {
answer = parent
} else {
if answer.SchedulerAgent == nil {
answer.SchedulerAgent = parent.SchedulerAgent
} else if parent.SchedulerAgent != nil {
applyToSchedulerAgent(parent.SchedulerAgent, answer.SchedulerAgent)
}
if answer.Policy == nil {
answer.Policy = parent.Policy
} else if parent.Policy != nil {
applyToGlobalProtectionPolicy(parent.Policy, answer.Policy)
}
if answer.Presubmits == nil {
answer.Presubmits = parent.Presubmits
} else if !answer.Presubmits.Replace && parent.Presubmits != nil {
err := applyToPreSubmits(parent.Presubmits, answer.Presubmits)
if err != nil {
return nil, errors.WithStack(err)
}
}
if answer.Postsubmits == nil {
answer.Postsubmits = parent.Postsubmits
} else if !answer.Postsubmits.Replace && parent.Postsubmits != nil {
err := applyToPostSubmits(parent.Postsubmits, answer.Postsubmits)
if err != nil {
return nil, errors.WithStack(err)
}
}
// combine the queries
if answer.Queries == nil {
answer.Queries = parent.Queries
} else {
applyToQueries(parent.Queries, answer.Queries)
}
if answer.MergeMethod == nil {
answer.MergeMethod = parent.MergeMethod
}
if answer.ProtectionPolicy == nil {
answer.ProtectionPolicy = parent.ProtectionPolicy
} else if parent.ProtectionPolicy != nil {
applyToProtectionPolicies(parent.ProtectionPolicy, answer.ProtectionPolicy)
}
if answer.ContextOptions == nil {
answer.ContextOptions = parent.ContextOptions
} else if parent.ContextOptions != nil {
applyToRepoContextPolicy(parent.ContextOptions, answer.ContextOptions)
}
// TODO: This should probably be an array of triggers, because the plugins yaml is expecting an array
if answer.Trigger == nil {
answer.Trigger = parent.Trigger
} else if parent.Trigger != nil {
applyToTrigger(parent.Trigger, answer.Trigger)
}
if answer.Approve == nil {
answer.Approve = parent.Approve
} else if parent.Approve != nil {
applyToApprove(parent.Approve, answer.Approve)
}
if answer.LGTM == nil {
answer.LGTM = parent.LGTM
} else if parent.LGTM != nil {
applyToLgtm(parent.LGTM, answer.LGTM)
}
if answer.ExternalPlugins == nil {
answer.ExternalPlugins = parent.ExternalPlugins
} else if parent.ExternalPlugins != nil {
applyToExternalPlugins(parent.ExternalPlugins, answer.ExternalPlugins)
}
if answer.Plugins == nil {
answer.Plugins = parent.Plugins
} else if parent.Plugins != nil {
applyToReplaceableSliceOfStrings(parent.Plugins, answer.Plugins)
}
if answer.Merger == nil {
answer.Merger = parent.Merger
} else if parent.Merger != nil {
applyToMerger(parent.Merger, answer.Merger)
}
if answer.Periodics == nil {
answer.Periodics = parent.Periodics
}
if answer.Attachments == nil {
answer.Attachments = parent.Attachments
}
}
}
return answer, nil
}
func applyToTrigger(parent, child *schedulerapi.Trigger) {
if child.IgnoreOkToTest != nil {
child.IgnoreOkToTest = parent.IgnoreOkToTest
}
if child.JoinOrgURL == nil {
child.JoinOrgURL = parent.JoinOrgURL
}
if child.OnlyOrgMembers == nil {
child.OnlyOrgMembers = parent.OnlyOrgMembers
}
if child.TrustedOrg == nil {
child.TrustedOrg = parent.TrustedOrg
}
}
func applyToSchedulerAgent(parent, child *schedulerapi.SchedulerAgent) {
if child.Agent == nil {
child.Agent = parent.Agent
}
}
func applyToMerger(parent, child *schedulerapi.Merger) {
if child.ContextPolicy == nil {
child.ContextPolicy = parent.ContextPolicy
} else if parent.ContextPolicy != nil {
applyToContextPolicy(parent.ContextPolicy, child.ContextPolicy)
}
if child.MergeType == nil {
child.MergeType = parent.MergeType
}
if child.MaxGoroutines == nil {
child.MaxGoroutines = parent.MaxGoroutines
}
if child.SquashLabel == nil {
child.SquashLabel = parent.SquashLabel
}
if child.BlockerLabel == nil {
child.BlockerLabel = parent.BlockerLabel
}
if child.PRStatusBaseURL == nil {
child.PRStatusBaseURL = parent.PRStatusBaseURL
}
if child.TargetURL == nil {
child.TargetURL = parent.TargetURL
}
syncPeriod, err := child.GetSyncPeriod()
if err != nil || syncPeriod == nil {
child.SyncPeriod = parent.SyncPeriod
}
if child.StatusUpdatePeriod == nil {
child.StatusUpdatePeriod = parent.StatusUpdatePeriod
}
}
func applyToRepoContextPolicy(parent, child *schedulerapi.RepoContextPolicy) {
if child.ContextPolicy == nil {
child.ContextPolicy = parent.ContextPolicy
} else if parent.ContextPolicy != nil {
applyToContextPolicy(parent.ContextPolicy, child.ContextPolicy)
}
if child.Branches == nil {
child.Branches = parent.Branches
} else if !child.Branches.Replace && parent.Branches != nil {
if child.Branches.Items == nil {
child.Branches.Items = make(map[string]*schedulerapi.ContextPolicy)
}
for pk, pv := range parent.Branches.Items {
if cv, ok := child.Branches.Items[pk]; !ok {
child.Branches.Items[pk] = pv
} else if pv != nil {
applyToContextPolicy(pv, cv)
}
}
}
}
func applyToContextPolicy(parent, child *schedulerapi.ContextPolicy) {
if child.FromBranchProtection == nil {
child.FromBranchProtection = parent.FromBranchProtection
}
if child.SkipUnknownContexts == nil {
child.SkipUnknownContexts = parent.SkipUnknownContexts
}
if child.OptionalContexts == nil {
child.OptionalContexts = parent.OptionalContexts
} else if parent.OptionalContexts != nil {
applyToReplaceableSliceOfStrings(parent.OptionalContexts, child.OptionalContexts)
}
if child.RequiredContexts == nil {
child.RequiredContexts = parent.RequiredContexts
} else if parent.RequiredContexts != nil {
applyToReplaceableSliceOfStrings(parent.RequiredContexts, child.RequiredContexts)
}
if child.RequiredIfPresentContexts == nil {
child.RequiredIfPresentContexts = parent.RequiredIfPresentContexts
} else if parent.RequiredIfPresentContexts != nil {
applyToReplaceableSliceOfStrings(parent.RequiredIfPresentContexts, child.RequiredIfPresentContexts)
}
}
func applyToReplaceableSliceOfStrings(parent, child *schedulerapi.ReplaceableSliceOfStrings) {
if !child.Replace && parent != nil {
if child.Items == nil {
child.Items = make([]string, 0)
}
for i := range parent.Items {
if stringhelpers.StringArrayIndex(child.Items, parent.Items[i]) < 0 {
child.Items = append(child.Items, parent.Items[i])
}
}
}
}
func applyToLgtm(parent, child *schedulerapi.Lgtm) {
if child.StickyLgtmTeam == nil {
child.StickyLgtmTeam = parent.StickyLgtmTeam
}
if child.ReviewActsAsLgtm == nil {
child.ReviewActsAsLgtm = parent.ReviewActsAsLgtm
}
if child.StoreTreeHash == nil {
child.StoreTreeHash = parent.StoreTreeHash
}
}
func applyToExternalPlugins(parent, child *schedulerapi.ReplaceableSliceOfExternalPlugins) {
if child.Items == nil {
child.Items = parent.Items
} else if !child.Replace {
child.Items = append(child.Items, parent.Items...)
}
}
// TODO use this
// func applyToExternalPlugin(parent *schedulerapi.ExternalPlugin, child *schedulerapi.ExternalPlugin) {
// if child.Name == nil {
// child.Name = parent.Name
// }
// if child.Endpoint == nil {
// child.Endpoint = parent.Endpoint
// }
// if child.Events == nil {
// child.Events = parent.Events
// } else if parent.Events != nil {
// applyToReplaceableSliceOfStrings(parent.Events, child.Events)
// }
//}
func applyToApprove(parent, child *schedulerapi.Approve) {
if child.IgnoreReviewState == nil {
child.IgnoreReviewState = parent.IgnoreReviewState
}
if child.IssueRequired == nil {
child.IssueRequired = parent.IssueRequired
}
if child.LgtmActsAsApprove == nil {
child.LgtmActsAsApprove = parent.LgtmActsAsApprove
}
if child.RequireSelfApproval == nil {
child.RequireSelfApproval = parent.RequireSelfApproval
}
}
func applyToGlobalProtectionPolicy(parent, child *schedulerapi.GlobalProtectionPolicy) {
if child.ProtectionPolicy == nil {
child.ProtectionPolicy = parent.ProtectionPolicy
} else if parent.ProtectionPolicy != nil {
applyToProtectionPolicy(parent.ProtectionPolicy, child.ProtectionPolicy)
}
if child.ProtectTested == nil {
child.ProtectTested = parent.ProtectTested
}
}
func applyToProtectionPolicy(parent, child *schedulerapi.ProtectionPolicy) {
if child.Protect == nil {
child.Protect = parent.Protect
}
if child.Admins == nil {
child.Admins = parent.Admins
}
if child.Restrictions == nil {
child.Restrictions = parent.Restrictions
} else if parent.Restrictions != nil {
applyToRestrictions(parent.Restrictions, child.Restrictions)
}
if child.RequiredPullRequestReviews == nil {
child.RequiredPullRequestReviews = parent.RequiredPullRequestReviews
} else if parent.RequiredPullRequestReviews != nil {
applyToRequiredPullRequestReviews(parent.RequiredPullRequestReviews, child.RequiredPullRequestReviews)
}
}
func applyToRequiredPullRequestReviews(parent, child *schedulerapi.ReviewPolicy) {
if child.Approvals == nil {
child.Approvals = parent.Approvals
}
if child.DismissStale == nil {
child.DismissStale = parent.DismissStale
}
if child.RequireOwners == nil {
child.RequireOwners = parent.RequireOwners
}
if child.DismissalRestrictions == nil {
child.DismissalRestrictions = parent.DismissalRestrictions
} else if parent.DismissalRestrictions != nil {
applyToRestrictions(parent.DismissalRestrictions, child.DismissalRestrictions)
}
}
func applyToRestrictions(parent, child *schedulerapi.Restrictions) {
if child.Teams == nil {
child.Teams = parent.Teams
} else if parent.Teams != nil {
applyToReplaceableSliceOfStrings(parent.Teams, child.Teams)
}
if child.Users == nil {
child.Users = parent.Users
} else if parent.Users != nil {
applyToReplaceableSliceOfStrings(parent.Users, child.Users)
}
}
func applyToPostSubmits(parentPostsubmits, childPostsubmits *schedulerapi.Postsubmits) error {
if childPostsubmits.Items == nil {
childPostsubmits.Items = make([]*job.Postsubmit, 0)
}
// Work through each of the post submits in the parent. If we can find a name based match in child,
// we apply it to the child, otherwise we append it
for _, parent := range parentPostsubmits.Items {
var found []*job.Postsubmit
for _, postsubmit := range childPostsubmits.Items {
if postsubmit.Name != "" && parent.Name != "" && postsubmit.Name == parent.Name {
found = append(found, postsubmit)
}
}
if len(found) > 1 {
return errors.Errorf("more than one postsubmit with name %v in %s", parent.Name, spew.Sdump(childPostsubmits))
} else if len(found) == 1 {
child := found[0]
*child = *parent
} else {
childPostsubmits.Items = append(childPostsubmits.Items, parent)
}
}
return nil
}
func applyToPreSubmits(parentPresubmits, childPresubmits *schedulerapi.Presubmits) error {
if childPresubmits.Items == nil {
childPresubmits.Items = make([]*job.Presubmit, 0)
}
// Work through each of the presubmits in the parent. If we can find a name based match in child,
// we apply it to the child, otherwise we append it
for _, parent := range parentPresubmits.Items {
var found []*job.Presubmit
for _, child := range childPresubmits.Items {
if child.Name == parent.Name {
found = append(found, child)
}
}
if len(found) > 1 {
return errors.Errorf("more than one presubmit with name %v in %s", parent.Name, spew.Sdump(parentPresubmits))
} else if len(found) == 1 {
child := found[0]
*child = *parent
} else {
childPresubmits.Items = append(childPresubmits.Items, parent)
}
}
return nil
}
func applyToProtectionPolicies(parent,
child *schedulerapi.ProtectionPolicies) {
if child.ProtectionPolicy == nil {
child.ProtectionPolicy = parent.ProtectionPolicy
} else if parent.ProtectionPolicy != nil {
applyToProtectionPolicy(parent.ProtectionPolicy, child.ProtectionPolicy)
}
if child.Items == nil {
child.Items = parent.Items
} else if !child.Replace {
for k, v := range parent.Items {
if _, ok := child.Items[k]; !ok {
child.Items[k] = v
}
}
}
}
func applyToQueries(parents, children []*schedulerapi.Query) {
children = append(children, parents...) //nolint:ineffassign,staticcheck
}