-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathglobal_cfg.go
719 lines (672 loc) · 26 KB
/
global_cfg.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package valid
import (
"fmt"
"regexp"
"strings"
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/utils"
)
const MergeableCommandReq = "mergeable"
const ApprovedCommandReq = "approved"
const UnDivergedCommandReq = "undiverged"
const PoliciesPassedCommandReq = "policies_passed"
const PlanRequirementsKey = "plan_requirements"
const ApplyRequirementsKey = "apply_requirements"
const ImportRequirementsKey = "import_requirements"
const WorkflowKey = "workflow"
const AllowedOverridesKey = "allowed_overrides"
const AllowCustomWorkflowsKey = "allow_custom_workflows"
const DefaultWorkflowName = "default"
const DeleteSourceBranchOnMergeKey = "delete_source_branch_on_merge"
const RepoLockingKey = "repo_locking"
const RepoLocksKey = "repo_locks"
const PolicyCheckKey = "policy_check"
const CustomPolicyCheckKey = "custom_policy_check"
const AutoDiscoverKey = "autodiscover"
const SilencePRCommentsKey = "silence_pr_comments"
var AllowedSilencePRComments = []string{"plan", "apply"}
// DefaultAtlantisFile is the default name of the config file for each repo.
const DefaultAtlantisFile = "atlantis.yaml"
// NonOverridableApplyReqs will get applied across all "repos" in the server side config.
// If repo config is allowed overrides, they can override this.
// TODO: Make this more customizable, not everyone wants this rigid workflow
// maybe something along the lines of defining overridable/non-overridable apply
// requirements in the config and removing the flag to enable policy checking.
var NonOverridableApplyReqs = []string{PoliciesPassedCommandReq}
// GlobalCfg is the final parsed version of server-side repo config.
type GlobalCfg struct {
Repos []Repo
Workflows map[string]Workflow
PolicySets PolicySets
Metrics Metrics
TeamAuthz TeamAuthz
}
type Metrics struct {
Statsd *Statsd
Prometheus *Prometheus
}
type Statsd struct {
Port string
Host string
}
type Prometheus struct {
Endpoint string
}
// Repo is the final parsed version of server-side repo config.
type Repo struct {
// ID is the exact match id of this config.
// If IDRegex is set then this will be empty.
ID string
// IDRegex is the regex match for this config.
// If ID is set then this will be nil.
IDRegex *regexp.Regexp
BranchRegex *regexp.Regexp
RepoConfigFile string
PlanRequirements []string
ApplyRequirements []string
ImportRequirements []string
PreWorkflowHooks []*WorkflowHook
Workflow *Workflow
PostWorkflowHooks []*WorkflowHook
AllowedWorkflows []string
AllowedOverrides []string
AllowCustomWorkflows *bool
DeleteSourceBranchOnMerge *bool
RepoLocking *bool
RepoLocks *RepoLocks
PolicyCheck *bool
CustomPolicyCheck *bool
AutoDiscover *AutoDiscover
SilencePRComments []string
}
type MergedProjectCfg struct {
PlanRequirements []string
ApplyRequirements []string
ImportRequirements []string
Workflow Workflow
AllowedWorkflows []string
DependsOn []string
RepoRelDir string
Workspace string
Name string
AutoplanEnabled bool
AutoMergeDisabled bool
AutoMergeMethod string
TerraformDistribution *string
TerraformVersion *version.Version
RepoCfgVersion int
PolicySets PolicySets
DeleteSourceBranchOnMerge bool
ExecutionOrderGroup int
RepoLocks RepoLocks
PolicyCheck bool
CustomPolicyCheck bool
SilencePRComments []string
}
// WorkflowHook is a map of custom run commands to run before or after workflows.
type WorkflowHook struct {
StepName string
RunCommand string
StepDescription string
Shell string
ShellArgs string
Commands string
}
// DefaultApplyStage is the Atlantis default apply stage.
var DefaultApplyStage = Stage{
Steps: []Step{
{
StepName: "apply",
},
},
}
// DefaultPolicyCheckStage is the Atlantis default policy check stage.
var DefaultPolicyCheckStage = Stage{
Steps: []Step{
{
StepName: "show",
},
{
StepName: "policy_check",
},
},
}
// DefaultPlanStage is the Atlantis default plan stage.
var DefaultPlanStage = Stage{
Steps: []Step{
{
StepName: "init",
},
{
StepName: "plan",
},
},
}
// DefaultImportStage is the Atlantis default import stage.
var DefaultImportStage = Stage{
Steps: []Step{
{
StepName: "init",
},
{
StepName: "import",
},
},
}
// DefaultStateRmStage is the Atlantis default state_rm stage.
var DefaultStateRmStage = Stage{
Steps: []Step{
{
StepName: "init",
},
{
StepName: "state_rm",
},
},
}
type GlobalCfgArgs struct {
RepoConfigFile string
// No longer a user option as of https://github.com/runatlantis/atlantis/pull/3911,
// but useful for tests to set to true to not require enumeration of allowed settings
// on the repo side
AllowAllRepoSettings bool
PolicyCheckEnabled bool
PreWorkflowHooks []*WorkflowHook
PostWorkflowHooks []*WorkflowHook
}
func NewGlobalCfgFromArgs(args GlobalCfgArgs) GlobalCfg {
defaultWorkflow := Workflow{
Name: DefaultWorkflowName,
Apply: DefaultApplyStage,
Plan: DefaultPlanStage,
PolicyCheck: DefaultPolicyCheckStage,
Import: DefaultImportStage,
StateRm: DefaultStateRmStage,
}
// Must construct slices here instead of using a `var` declaration because
// we treat nil slices differently.
commandReqs := []string{}
allowedOverrides := []string{}
allowedWorkflows := []string{}
policyCheck := false
if args.PolicyCheckEnabled {
commandReqs = append(commandReqs, PoliciesPassedCommandReq)
policyCheck = true
}
allowCustomWorkflows := false
deleteSourceBranchOnMerge := false
repoLocks := DefaultRepoLocks
customPolicyCheck := false
autoDiscover := AutoDiscover{Mode: AutoDiscoverAutoMode}
var silencePRComments []string
if args.AllowAllRepoSettings {
allowedOverrides = []string{PlanRequirementsKey, ApplyRequirementsKey, ImportRequirementsKey, WorkflowKey, DeleteSourceBranchOnMergeKey, RepoLockingKey, RepoLocksKey, PolicyCheckKey, SilencePRCommentsKey}
allowCustomWorkflows = true
}
return GlobalCfg{
Repos: []Repo{
{
IDRegex: regexp.MustCompile(".*"),
BranchRegex: regexp.MustCompile(".*"),
RepoConfigFile: args.RepoConfigFile,
PlanRequirements: commandReqs,
ApplyRequirements: commandReqs,
ImportRequirements: commandReqs,
PreWorkflowHooks: args.PreWorkflowHooks,
Workflow: &defaultWorkflow,
PostWorkflowHooks: args.PostWorkflowHooks,
AllowedWorkflows: allowedWorkflows,
AllowedOverrides: allowedOverrides,
AllowCustomWorkflows: &allowCustomWorkflows,
DeleteSourceBranchOnMerge: &deleteSourceBranchOnMerge,
RepoLocks: &repoLocks,
PolicyCheck: &policyCheck,
CustomPolicyCheck: &customPolicyCheck,
AutoDiscover: &autoDiscover,
SilencePRComments: silencePRComments,
},
},
Workflows: map[string]Workflow{
DefaultWorkflowName: defaultWorkflow,
},
TeamAuthz: TeamAuthz{
Args: make([]string, 0),
},
}
}
// IDMatches returns true if the repo ID otherID matches this config.
func (r Repo) IDMatches(otherID string) bool {
if r.ID != "" {
return r.ID == otherID
}
return r.IDRegex.MatchString(otherID)
}
// BranchMatches returns true if the branch other matches a branch regex (if preset).
func (r Repo) BranchMatches(other string) bool {
if r.BranchRegex == nil {
return true
}
return r.BranchRegex.MatchString(other)
}
// IDString returns a string representation of this config.
func (r Repo) IDString() string {
if r.ID != "" {
return r.ID
}
return "/" + r.IDRegex.String() + "/"
}
// MergeProjectCfg merges proj and rCfg with the global config to return a
// final config. It assumes that all configs have been validated.
func (g GlobalCfg) MergeProjectCfg(log logging.SimpleLogging, repoID string, proj Project, rCfg RepoCfg) MergedProjectCfg {
log.Debug("MergeProjectCfg started")
planReqs, applyReqs, importReqs, workflow, allowedOverrides, allowCustomWorkflows, deleteSourceBranchOnMerge, repoLocks, policyCheck, customPolicyCheck, _, silencePRComments := g.getMatchingCfg(log, repoID)
// If repos are allowed to override certain keys then override them.
for _, key := range allowedOverrides {
switch key {
case PlanRequirementsKey:
if proj.PlanRequirements != nil {
log.Debug("overriding server-defined %s with repo settings: [%s]", PlanRequirementsKey, strings.Join(proj.PlanRequirements, ","))
planReqs = proj.PlanRequirements
}
case ApplyRequirementsKey:
if proj.ApplyRequirements != nil {
log.Debug("overriding server-defined %s with repo settings: [%s]", ApplyRequirementsKey, strings.Join(proj.ApplyRequirements, ","))
applyReqs = proj.ApplyRequirements
// Preserve policies_passed req if policy check is enabled
if policyCheck {
applyReqs = append(applyReqs, PoliciesPassedCommandReq)
}
}
case ImportRequirementsKey:
if proj.ImportRequirements != nil {
log.Debug("overriding server-defined %s with repo settings: [%s]", ImportRequirementsKey, strings.Join(proj.ImportRequirements, ","))
importReqs = proj.ImportRequirements
}
case WorkflowKey:
if proj.WorkflowName != nil {
// We iterate over the global workflows first and the repo
// workflows second so that repo workflows override. This is
// safe because at this point we know if a repo is allowed to
// define its own workflow. We also know that a workflow will
// exist with this name due to earlier validation.
name := *proj.WorkflowName
for k, v := range g.Workflows {
if k == name {
workflow = v
}
}
if allowCustomWorkflows {
for k, v := range rCfg.Workflows {
if k == name {
workflow = v
}
}
}
log.Debug("overriding server-defined %s with repo-specified workflow: %q", WorkflowKey, workflow.Name)
}
case DeleteSourceBranchOnMergeKey:
//We check whether the server configured value and repo-root level
//config is different. If it is then we change to the more granular.
if rCfg.DeleteSourceBranchOnMerge != nil && deleteSourceBranchOnMerge != *rCfg.DeleteSourceBranchOnMerge {
log.Debug("overriding server-defined %s with repo settings: [%t]", DeleteSourceBranchOnMergeKey, rCfg.DeleteSourceBranchOnMerge)
deleteSourceBranchOnMerge = *rCfg.DeleteSourceBranchOnMerge
}
//Then we check whether the more granular project based config is
//different. If it is then we set it.
if proj.DeleteSourceBranchOnMerge != nil && deleteSourceBranchOnMerge != *proj.DeleteSourceBranchOnMerge {
log.Debug("overriding repo-root-defined %s with repo settings: [%t]", DeleteSourceBranchOnMergeKey, *proj.DeleteSourceBranchOnMerge)
deleteSourceBranchOnMerge = *proj.DeleteSourceBranchOnMerge
}
log.Debug("merged deleteSourceBranchOnMerge: [%t]", deleteSourceBranchOnMerge)
case RepoLockingKey:
if proj.RepoLocking != nil {
log.Debug("overriding server-defined %s with repo settings: [%t]", RepoLockingKey, *proj.RepoLocking)
if *proj.RepoLocking && repoLocks.Mode == RepoLocksDisabledMode {
repoLocks.Mode = DefaultRepoLocksMode
} else if !*proj.RepoLocking {
repoLocks.Mode = RepoLocksDisabledMode
}
}
case RepoLocksKey:
//We check whether the server configured value and repo-root level
//config is different. If it is then we change to the more granular.
if rCfg.RepoLocks != nil && repoLocks.Mode != rCfg.RepoLocks.Mode {
log.Debug("overriding server-defined %s with repo settings: [%#v]", RepoLocksKey, rCfg.RepoLocks)
repoLocks = *rCfg.RepoLocks
}
//Then we check whether the more granular project based config is
//different. If it is then we set it.
if proj.RepoLocks != nil && repoLocks.Mode != proj.RepoLocks.Mode {
log.Debug("overriding repo-root-defined %s with repo settings: [%#v]", RepoLocksKey, *proj.RepoLocks)
repoLocks = *proj.RepoLocks
}
log.Debug("merged repoLocks: [%#v]", repoLocks)
case PolicyCheckKey:
if proj.PolicyCheck != nil {
log.Debug("overriding server-defined %s with repo settings: [%t]", PolicyCheckKey, *proj.PolicyCheck)
policyCheck = *proj.PolicyCheck
}
case CustomPolicyCheckKey:
if proj.CustomPolicyCheck != nil {
log.Debug("overriding server-defined %s with repo settings: [%t]", CustomPolicyCheckKey, *proj.CustomPolicyCheck)
customPolicyCheck = *proj.CustomPolicyCheck
}
case SilencePRCommentsKey:
if proj.SilencePRComments != nil {
log.Debug("overriding repo-root-defined %s with repo settings: [%t]", SilencePRCommentsKey, strings.Join(proj.SilencePRComments, ","))
silencePRComments = proj.SilencePRComments
} else if rCfg.SilencePRComments != nil {
log.Debug("overriding server-defined %s with repo settings: [%s]", SilencePRCommentsKey, strings.Join(rCfg.SilencePRComments, ","))
silencePRComments = rCfg.SilencePRComments
}
}
log.Debug("MergeProjectCfg completed")
}
log.Debug("final settings: %s: [%s], %s: [%s], %s: [%s], %s: %s, %s: %t, %s: %s, %s: %t, %s: %t, %s: [%s]",
PlanRequirementsKey, strings.Join(planReqs, ","),
ApplyRequirementsKey, strings.Join(applyReqs, ","),
ImportRequirementsKey, strings.Join(importReqs, ","),
WorkflowKey, workflow.Name,
DeleteSourceBranchOnMergeKey, deleteSourceBranchOnMerge,
RepoLockingKey, repoLocks.Mode,
PolicyCheckKey, policyCheck,
CustomPolicyCheckKey, policyCheck,
SilencePRCommentsKey, strings.Join(silencePRComments, ","),
)
return MergedProjectCfg{
PlanRequirements: planReqs,
ApplyRequirements: applyReqs,
ImportRequirements: importReqs,
Workflow: workflow,
RepoRelDir: proj.Dir,
Workspace: proj.Workspace,
DependsOn: proj.DependsOn,
Name: proj.GetName(),
AutoplanEnabled: proj.Autoplan.Enabled,
TerraformDistribution: proj.TerraformDistribution,
TerraformVersion: proj.TerraformVersion,
RepoCfgVersion: rCfg.Version,
PolicySets: g.PolicySets,
DeleteSourceBranchOnMerge: deleteSourceBranchOnMerge,
ExecutionOrderGroup: proj.ExecutionOrderGroup,
RepoLocks: repoLocks,
PolicyCheck: policyCheck,
CustomPolicyCheck: customPolicyCheck,
SilencePRComments: silencePRComments,
}
}
// DefaultProjCfg returns the default project config for all projects under the
// repo with id repoID. It is used when there is no repo config.
func (g GlobalCfg) DefaultProjCfg(log logging.SimpleLogging, repoID string, repoRelDir string, workspace string) MergedProjectCfg {
log.Debug("building config based on server-side config")
planReqs, applyReqs, importReqs, workflow, _, _, deleteSourceBranchOnMerge, repoLocks, policyCheck, customPolicyCheck, _, silencePRComments := g.getMatchingCfg(log, repoID)
return MergedProjectCfg{
PlanRequirements: planReqs,
ApplyRequirements: applyReqs,
ImportRequirements: importReqs,
Workflow: workflow,
RepoRelDir: repoRelDir,
Workspace: workspace,
Name: "",
AutoplanEnabled: DefaultAutoPlanEnabled,
TerraformDistribution: nil,
TerraformVersion: nil,
PolicySets: g.PolicySets,
DeleteSourceBranchOnMerge: deleteSourceBranchOnMerge,
RepoLocks: repoLocks,
PolicyCheck: policyCheck,
CustomPolicyCheck: customPolicyCheck,
SilencePRComments: silencePRComments,
}
}
// RepoAutoDiscoverCfg returns the AutoDiscover config from the global config
// for the repo with id repoID. If no matching repo is found or there is no
// AutoDiscover config then this function returns nil.
func (g GlobalCfg) RepoAutoDiscoverCfg(repoID string) *AutoDiscover {
repo := g.MatchingRepo(repoID)
if repo != nil {
return repo.AutoDiscover
}
return nil
}
// ValidateRepoCfg validates that rCfg for repo with id repoID is valid based
// on our global config.
func (g GlobalCfg) ValidateRepoCfg(rCfg RepoCfg, repoID string) error {
mapContainsF := func(m map[string]Workflow, key string) bool {
for k := range m {
if k == key {
return true
}
}
return false
}
// Check allowed overrides.
var allowedOverrides []string
for _, repo := range g.Repos {
if repo.IDMatches(repoID) {
if repo.AllowedOverrides != nil {
allowedOverrides = repo.AllowedOverrides
}
}
}
for _, p := range rCfg.Projects {
if p.WorkflowName != nil && !utils.SlicesContains(allowedOverrides, WorkflowKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", WorkflowKey, AllowedOverridesKey, WorkflowKey)
}
if p.ApplyRequirements != nil && !utils.SlicesContains(allowedOverrides, ApplyRequirementsKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", ApplyRequirementsKey, AllowedOverridesKey, ApplyRequirementsKey)
}
if p.PlanRequirements != nil && !utils.SlicesContains(allowedOverrides, PlanRequirementsKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", PlanRequirementsKey, AllowedOverridesKey, PlanRequirementsKey)
}
if p.ImportRequirements != nil && !utils.SlicesContains(allowedOverrides, ImportRequirementsKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", ImportRequirementsKey, AllowedOverridesKey, ImportRequirementsKey)
}
if p.DeleteSourceBranchOnMerge != nil && !utils.SlicesContains(allowedOverrides, DeleteSourceBranchOnMergeKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", DeleteSourceBranchOnMergeKey, AllowedOverridesKey, DeleteSourceBranchOnMergeKey)
}
if p.RepoLocking != nil && !utils.SlicesContains(allowedOverrides, RepoLockingKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", RepoLockingKey, AllowedOverridesKey, RepoLockingKey)
}
if p.RepoLocks != nil && !utils.SlicesContains(allowedOverrides, RepoLocksKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", RepoLocksKey, AllowedOverridesKey, RepoLocksKey)
}
if p.CustomPolicyCheck != nil && !utils.SlicesContains(allowedOverrides, CustomPolicyCheckKey) {
return fmt.Errorf("repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'", CustomPolicyCheckKey, AllowedOverridesKey, CustomPolicyCheckKey)
}
if p.SilencePRComments != nil {
if !utils.SlicesContains(allowedOverrides, SilencePRCommentsKey) {
return fmt.Errorf(
"repo config not allowed to set '%s' key: server-side config needs '%s: [%s]'",
SilencePRCommentsKey,
AllowedOverridesKey,
SilencePRCommentsKey,
)
}
for _, silenceStage := range p.SilencePRComments {
if !utils.SlicesContains(AllowedSilencePRComments, silenceStage) {
return fmt.Errorf(
"repo config '%s' key value of '%s' is not supported, supported values are [%s]",
SilencePRCommentsKey,
silenceStage,
strings.Join(AllowedSilencePRComments, ", "),
)
}
}
}
}
// Check custom workflows.
var allowCustomWorkflows bool
for _, repo := range g.Repos {
if repo.IDMatches(repoID) {
if repo.AllowCustomWorkflows != nil {
allowCustomWorkflows = *repo.AllowCustomWorkflows
}
}
}
if len(rCfg.Workflows) > 0 && !allowCustomWorkflows {
return fmt.Errorf("repo config not allowed to define custom workflows: server-side config needs '%s: true'", AllowCustomWorkflowsKey)
}
// Check if the repo has set a workflow name that doesn't exist.
for _, p := range rCfg.Projects {
if p.WorkflowName != nil {
name := *p.WorkflowName
if !mapContainsF(rCfg.Workflows, name) && !mapContainsF(g.Workflows, name) {
return fmt.Errorf("workflow %q is not defined anywhere", name)
}
}
}
// Check workflow is allowed
var allowedWorkflows []string
for _, repo := range g.Repos {
if repo.IDMatches(repoID) {
if repo.AllowedWorkflows != nil {
allowedWorkflows = repo.AllowedWorkflows
}
}
}
for _, p := range rCfg.Projects {
// default is always allowed
if p.WorkflowName != nil && len(allowedWorkflows) != 0 {
name := *p.WorkflowName
if allowCustomWorkflows {
// If we allow CustomWorkflows we need to check that workflow name is defined inside repo and not global.
if mapContainsF(rCfg.Workflows, name) {
break
}
}
if !utils.SlicesContains(allowedWorkflows, name) {
return fmt.Errorf("workflow '%s' is not allowed for this repo", name)
}
}
}
return nil
}
// getMatchingCfg returns the key settings for repoID.
func (g GlobalCfg) getMatchingCfg(log logging.SimpleLogging, repoID string) (planReqs []string, applyReqs []string, importReqs []string, workflow Workflow, allowedOverrides []string, allowCustomWorkflows bool, deleteSourceBranchOnMerge bool, repoLocks RepoLocks, policyCheck bool, customPolicyCheck bool, autoDiscover AutoDiscover, silencePRComments []string) {
toLog := make(map[string]string)
traceF := func(repoIdx int, repoID string, key string, val interface{}) string {
from := "default server config"
if repoIdx > 0 {
from = fmt.Sprintf("repos[%d], id: %s", repoIdx, repoID)
}
var valStr string
switch v := val.(type) {
case string:
valStr = fmt.Sprintf("%q", v)
case []string:
valStr = fmt.Sprintf("[%s]", strings.Join(v, ","))
case bool:
valStr = fmt.Sprintf("%t", v)
default:
valStr = "this is a bug"
}
return fmt.Sprintf("setting %s: %s from %s", key, valStr, from)
}
// Can't use raw.DefaultAutoDiscoverMode() because of an import cycle. Should refactor to avoid that.
autoDiscover = AutoDiscover{Mode: AutoDiscoverAutoMode}
repoLocking := true
repoLocks = DefaultRepoLocks
for _, key := range []string{PlanRequirementsKey, ApplyRequirementsKey, ImportRequirementsKey, WorkflowKey, AllowedOverridesKey, AllowCustomWorkflowsKey, DeleteSourceBranchOnMergeKey, RepoLockingKey, RepoLocksKey, PolicyCheckKey, CustomPolicyCheckKey, SilencePRCommentsKey} {
for i, repo := range g.Repos {
if repo.IDMatches(repoID) {
switch key {
case PlanRequirementsKey:
if repo.PlanRequirements != nil {
toLog[PlanRequirementsKey] = traceF(i, repo.IDString(), PlanRequirementsKey, repo.PlanRequirements)
planReqs = repo.PlanRequirements
}
case ApplyRequirementsKey:
if repo.ApplyRequirements != nil {
toLog[ApplyRequirementsKey] = traceF(i, repo.IDString(), ApplyRequirementsKey, repo.ApplyRequirements)
applyReqs = repo.ApplyRequirements
}
case ImportRequirementsKey:
if repo.ImportRequirements != nil {
toLog[ImportRequirementsKey] = traceF(i, repo.IDString(), ImportRequirementsKey, repo.ImportRequirements)
importReqs = repo.ImportRequirements
}
case WorkflowKey:
if repo.Workflow != nil {
toLog[WorkflowKey] = traceF(i, repo.IDString(), WorkflowKey, repo.Workflow.Name)
workflow = *repo.Workflow
}
case AllowedOverridesKey:
if repo.AllowedOverrides != nil {
toLog[AllowedOverridesKey] = traceF(i, repo.IDString(), AllowedOverridesKey, repo.AllowedOverrides)
allowedOverrides = repo.AllowedOverrides
}
case AllowCustomWorkflowsKey:
if repo.AllowCustomWorkflows != nil {
toLog[AllowCustomWorkflowsKey] = traceF(i, repo.IDString(), AllowCustomWorkflowsKey, *repo.AllowCustomWorkflows)
allowCustomWorkflows = *repo.AllowCustomWorkflows
}
case DeleteSourceBranchOnMergeKey:
if repo.DeleteSourceBranchOnMerge != nil {
toLog[DeleteSourceBranchOnMergeKey] = traceF(i, repo.IDString(), DeleteSourceBranchOnMergeKey, *repo.DeleteSourceBranchOnMerge)
deleteSourceBranchOnMerge = *repo.DeleteSourceBranchOnMerge
}
case RepoLockingKey:
if repo.RepoLocking != nil {
toLog[RepoLockingKey] = traceF(i, repo.IDString(), RepoLockingKey, *repo.RepoLocking)
repoLocking = *repo.RepoLocking
}
case RepoLocksKey:
if repo.RepoLocks != nil {
toLog[RepoLocksKey] = traceF(i, repo.IDString(), RepoLocksKey, repo.RepoLocks.Mode)
repoLocks = *repo.RepoLocks
}
case PolicyCheckKey:
if repo.PolicyCheck != nil {
toLog[PolicyCheckKey] = traceF(i, repo.IDString(), PolicyCheckKey, *repo.PolicyCheck)
policyCheck = *repo.PolicyCheck
}
case CustomPolicyCheckKey:
if repo.CustomPolicyCheck != nil {
toLog[CustomPolicyCheckKey] = traceF(i, repo.IDString(), CustomPolicyCheckKey, *repo.CustomPolicyCheck)
customPolicyCheck = *repo.CustomPolicyCheck
}
case AutoDiscoverKey:
if repo.AutoDiscover != nil {
toLog[AutoDiscoverKey] = traceF(i, repo.IDString(), AutoDiscoverKey, repo.AutoDiscover.Mode)
autoDiscover = *repo.AutoDiscover
}
case SilencePRCommentsKey:
if repo.SilencePRComments != nil {
toLog[SilencePRCommentsKey] = traceF(i, repo.IDString(), SilencePRCommentsKey, repo.SilencePRComments)
silencePRComments = repo.SilencePRComments
}
}
}
}
}
for _, l := range toLog {
log.Debug(l)
}
// repoLocking is deprecated and enabled by default, disable repo locks if it is explicitly disabled
if !repoLocking {
repoLocks.Mode = RepoLocksDisabledMode
}
return
}
// MatchingRepo returns an instance of Repo which matches a given repoID.
// If multiple repos match, return the last one for consistency with getMatchingCfg.
func (g GlobalCfg) MatchingRepo(repoID string) *Repo {
for i := len(g.Repos) - 1; i >= 0; i-- {
repo := g.Repos[i]
if repo.IDMatches(repoID) {
return &repo
}
}
return nil
}
// RepoConfigFile returns a repository specific file path
// If not defined, return atlantis.yaml as default
func (g GlobalCfg) RepoConfigFile(repoID string) string {
repo := g.MatchingRepo(repoID)
if repo != nil && repo.RepoConfigFile != "" {
return repo.RepoConfigFile
}
return DefaultAtlantisFile
}