-
Notifications
You must be signed in to change notification settings - Fork 785
/
tide.go
132 lines (117 loc) · 3.54 KB
/
tide.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
package config
import (
"fmt"
"time"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/lighthouse-config/pkg/config"
)
// CreateTide creates a default Tide Config object
func CreateTide(tideURL string) config.Keeper {
t := config.Keeper{
TargetURL: tideURL,
}
var qs []config.KeeperQuery
qs = append(qs, createApplicationTideQuery())
qs = append(qs, createEnvironmentTideQuery())
t.Queries = qs
myTrue := true
myFalse := false
t.SyncPeriod = time.Duration(30)
t.StatusUpdatePeriod = time.Duration(30)
t.ContextOptions = config.KeeperContextPolicyOptions{
KeeperContextPolicy: config.KeeperContextPolicy{
FromBranchProtection: &myTrue,
SkipUnknownContexts: &myFalse,
},
}
return t
}
// AddRepoToTideConfig adds a code repository to the Tide section of the Prow Config
func AddRepoToTideConfig(t *config.Keeper, repo string, kind Kind) error {
switch kind {
case Application:
found := false
for index, q := range t.Queries {
if util.Contains(q.Labels, "approved") {
found = true
repos := t.Queries[index].Repos
if !util.Contains(repos, repo) {
repos = append(repos, repo)
t.Queries[index].Repos = repos
}
}
}
if !found {
log.Logger().Infof("Failed to find 'application' tide config, adding...")
t.Queries = append(t.Queries, createApplicationTideQuery())
}
case Environment, RemoteEnvironment:
found := false
for index, q := range t.Queries {
if !util.Contains(q.Labels, "approved") {
found = true
repos := t.Queries[index].Repos
if !util.Contains(repos, repo) {
repos = append(repos, repo)
t.Queries[index].Repos = repos
}
}
}
if !found {
log.Logger().Infof("Failed to find 'environment' tide config, adding...")
t.Queries = append(t.Queries, createEnvironmentTideQuery())
}
case Protection:
// No Tide config needed for Protection
default:
return fmt.Errorf("unknown Prow config kind %s", kind)
}
return nil
}
// RemoveRepoFromTideConfig adds a code repository to the Tide section of the Prow Config
func RemoveRepoFromTideConfig(t *config.Keeper, repo string, kind Kind) error {
switch kind {
case Application:
found := false
for index, q := range t.Queries {
if util.Contains(q.Labels, "approved") {
found = true
t.Queries[index].Repos = util.RemoveStringFromSlice(t.Queries[index].Repos, repo)
}
}
if !found {
log.Logger().Infof("Failed to find 'application' tide config, adding...")
}
case Environment, RemoteEnvironment:
found := false
for index, q := range t.Queries {
if !util.Contains(q.Labels, "approved") {
found = true
t.Queries[index].Repos = util.RemoveStringFromSlice(t.Queries[index].Repos, repo)
}
}
if !found {
log.Logger().Infof("Failed to find 'environment' tide config, adding...")
}
case Protection:
// No Tide config needed for Protection
default:
return fmt.Errorf("unknown Prow config kind %s", kind)
}
return nil
}
func createApplicationTideQuery() config.KeeperQuery {
return config.KeeperQuery{
Repos: []string{"jenkins-x/dummy"},
Labels: []string{"approved"},
MissingLabels: []string{"do-not-merge", "do-not-merge/hold", "do-not-merge/work-in-progress", "needs-ok-to-test", "needs-rebase"},
}
}
func createEnvironmentTideQuery() config.KeeperQuery {
return config.KeeperQuery{
Repos: []string{"jenkins-x/dummy-environment"},
Labels: []string{},
MissingLabels: []string{"do-not-merge", "do-not-merge/hold", "do-not-merge/work-in-progress", "needs-ok-to-test", "needs-rebase"},
}
}