-
Notifications
You must be signed in to change notification settings - Fork 50
/
source_config_types.go
316 lines (248 loc) · 9.41 KB
/
source_config_types.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
package v1alpha1
import (
"strings"
"github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// SourceConfigFileName default name of the source repository configuration
SourceConfigFileName = "source-config.yaml"
//DefaultSlackChannel
DefaultSlackChannel = "#jenkins-x-pipelines"
)
// BooleanFlag a type that is used for string boolean values that can be blank or yes/no
type BooleanFlag string
var (
// BooleanFlagNone indicates no value
BooleanFlagNone BooleanFlag = ""
// BooleanFlagYes BooleanFlagYes indicates yes
BooleanFlagYes BooleanFlag = "yes"
// BooleanFlagNo indicates no
BooleanFlagNo BooleanFlag = "no"
)
// PipelineKind what pipeline to notify on
type PipelineKind string
var (
// PipelineKindNone indicates all pipelines
PipelineKindNone PipelineKind = ""
// PipelineKindAll indicates all pipelines
PipelineKindAll PipelineKind = "all"
// PipelineKindRelease only notify on release pipelines
PipelineKindRelease PipelineKind = "release"
// PipelineKindPullRequest only notify on pullRequest pipelines
PipelineKindPullRequest PipelineKind = "pullRequest"
)
// NotifyKind what kind of notification
type NotifyKind string
var (
// NotifyKindNone indicates no notification
NotifyKindNone NotifyKind = ""
// NotifyKindNever never notify
NotifyKindNever NotifyKind = "never"
// NotifyKindAlways always notify
NotifyKindAlways NotifyKind = "always"
// NotifyKindFailure only failures
NotifyKindFailure NotifyKind = "failure"
// NotifyKindFailureOrFirstSuccess only failures or first success after failure
NotifyKindFailureOrFirstSuccess NotifyKind = "failureOrNextSuccess"
// NotifyKindSuccess only successful
NotifyKindSuccess NotifyKind = "success"
)
// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// SourceConfig represents a collection source repostory groups and repositories
//
// +k8s:openapi-gen=true
type SourceConfig struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata"`
// Spec holds the desired state of the SourceConfig from the client
// +optional
Spec SourceConfigSpec `json:"spec"`
}
// SourceConfigList contains a list of SourceConfig
//
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type SourceConfigList struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ListMeta `json:"metadata,omitempty"`
Items []SourceConfig `json:"items"`
}
// SourceConfigSpec defines the desired state of SourceConfig.
type SourceConfigSpec struct {
// Groups the groups of source repositories
Groups []RepositoryGroup `json:"groups,omitempty"`
// Scheduler the default scheduler for any group/repository which does not specify one
Scheduler string `json:"scheduler,omitempty"`
// Slack optional default slack notification configuration inherited by groups
Slack *SlackNotify `json:"slack,omitempty"`
// JenkinsServers the jenkins servers configured for this repository
JenkinsServers []JenkinsServer `json:"jenkinsServers,omitempty"`
// JenkinsFolderTemplate the default template file to use to generate the folder job DSL script
JenkinsFolderTemplate string `json:"jenkinsFolderTemplate,omitempty"`
// JenkinsJobTemplate the default template file to use to generate the projects job DSL script
JenkinsJobTemplate string `json:"jenkinsJobTemplate,omitempty"`
}
// SourceConfigSpec defines the desired state of SourceConfig.
type RepositoryGroup struct {
// Provider the git provider server URL
Provider string `json:"provider,omitempty"`
// ProviderKind the git provider kind
ProviderKind string `json:"providerKind,omitempty"`
// ProviderName the git provider name
ProviderName string `json:"providerName,omitempty"`
// Owner the name of the organisation/owner/project/user that owns the repository
Owner string `json:"owner,omitempty" validate:"nonzero"`
// Repositories the repositories for the
Repositories []Repository `json:"repositories,omitempty"`
// Scheduler the default scheduler for this group
Scheduler string `json:"scheduler,omitempty"`
// JenkinsFolderTemplate the default template file to use to generate the folder job DSL script
JenkinsFolderTemplate string `json:"jenkinsFolderTemplate,omitempty"`
// JenkinsJobTemplate the default job template file to use to generate the projects job DSL script
JenkinsJobTemplate string `json:"jenkinsJobTemplate,omitempty"`
// Slack optional slack notification configuration
Slack *SlackNotify `json:"slack,omitempty"`
// Settings optional settinsg for repositories in this group
Settings *v4beta1.SettingsConfig `json:"settings,omitempty"`
}
// Repository the name of the repository to import and the optional scheduler
type Repository struct {
// Name the name of the repository
Name string `json:"name,omitempty" validate:"nonzero"`
// Scheduler the optional name of the scheduler to use if different to the group
Scheduler string `json:"scheduler,omitempty"`
// JenkinsJobTemplate the template file to use to generate the projects job DSL script
JenkinsJobTemplate string `json:"jenkinsJobTemplate,omitempty"`
// Description the optional description of this repository
Description string `json:"description,omitempty"`
// URL the URL to access this repository
URL string `json:"url,omitempty"`
// HTTPCloneURL the HTTP/HTTPS based clone URL
HTTPCloneURL string `json:"httpCloneURL,omitempty"`
// SSHCloneURL the SSH based clone URL
SSHCloneURL string `json:"sshCloneURL,omitempty"`
// Slack optional slack notification configuration
Slack *SlackNotify `json:"slack,omitempty"`
}
// JenkinsServer the Jenkins server configuration
type JenkinsServer struct {
// Server the name of the Jenkins Server to use
Server string `json:"server,omitempty"`
// FolderTemplate the default template file to use to generate the folder job DSL script
FolderTemplate string `json:"folderTemplate,omitempty"`
// JobTemplate the default template file to use to generate the projects job DSL script
JobTemplate string `json:"jobTemplate,omitempty"`
// Groups the groups of source repositories
Groups []RepositoryGroup `json:"groups,omitempty"`
}
// SlackNotify the slack notification configuration
type SlackNotify struct {
// Channel the name of the channel to notify pipelines
Channel string `json:"channel,omitempty"`
// Kind kind of notification such as always, only failures, failures or first succeed, only succeeds etc
Kind NotifyKind `json:"kind,omitempty"`
// Pipeline kind of pipeline to notify on (all, releases, pull requests etc)
Pipeline PipelineKind `json:"pipeline,omitempty"`
// DirectMessage whether to use Direct Messages
DirectMessage BooleanFlag `json:"directMessage,omitempty"`
// NotifyReviewers whether to notify reviews of Pull Request pipelines
NotifyReviewers BooleanFlag `json:"notifyReviewers,omitempty"`
// Branch specify the branch name or filter to notify
Branch *Pattern `json:"branch,omitempty"`
// Context specify the context name or filter to notify
Context *Pattern `json:"context,omitempty"`
// PullRequestLabel specify the pull request labels to notify
PullRequestLabel *Pattern `json:"pullRequestLabel,omitempty"`
}
// Pattern for matching strings
type Pattern struct {
// Name
Name string `json:"name,omitempty"`
// Includes patterns to include in changing
Includes []string `json:"include,omitempty"`
// Excludes patterns to exclude from upgrading
Excludes []string `json:"exclude,omitempty"`
}
// Matches returns true if the text matches the given text
func (p *Pattern) Matches(text string) bool {
if p == nil {
return true
}
if p.Name != "" {
return text == p.Name
}
return stringhelpers.StringMatchesAny(text, p.Includes, p.Excludes)
}
// Matches returns true if the text matches the given text
func (p *Pattern) MatchesLabels(labels []string) bool {
if p == nil {
return true
}
if p.Name != "" {
if stringhelpers.StringArrayIndex(labels, p.Name) < 0 {
return false
}
}
for _, text := range p.Excludes {
if stringhelpers.StringArrayIndex(labels, text) >= 0 {
return false
}
}
if len(p.Includes) == 0 {
return true
}
for _, text := range p.Includes {
if stringhelpers.StringArrayIndex(labels, text) >= 0 {
return true
}
}
return false
}
func (p *Pattern) Inherit(group *Pattern) *Pattern {
if p == nil {
return group
}
return p
}
// ToBool converts the flag to a boolean such that it is only true if the
// value is "yes"
func (f BooleanFlag) ToBool() bool {
return strings.ToLower(string(f)) == "yes"
}
// Inherit if the current flag is blank lets use the group value
func (f BooleanFlag) Inherit(group BooleanFlag) BooleanFlag {
if string(f) != "" {
return f
}
return group
}
// Inherit inherits the settings on this repository from the group
func (repo *SlackNotify) Inherit(group *SlackNotify) *SlackNotify {
if repo == nil {
return group
}
if group == nil {
return repo
}
answer := *repo
if repo.Channel == "" {
answer.Channel = group.Channel
}
if string(repo.Pipeline) == "" {
answer.Pipeline = group.Pipeline
}
if string(repo.Kind) != "" {
answer.Kind = repo.Kind
}
answer.Branch = repo.Branch.Inherit(group.Branch)
answer.Context = repo.Context.Inherit(group.Context)
answer.PullRequestLabel = repo.PullRequestLabel.Inherit(group.PullRequestLabel)
answer.DirectMessage = repo.DirectMessage.Inherit(group.DirectMessage)
answer.NotifyReviewers = repo.NotifyReviewers.Inherit(group.NotifyReviewers)
return &answer
}