-
Notifications
You must be signed in to change notification settings - Fork 50
/
helpers.go
387 lines (351 loc) · 11.6 KB
/
helpers.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
package sourceconfigs
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/jenkins-x-plugins/jx-gitops/pkg/apis/gitops/v1alpha1"
jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
var info = termcolor.ColorInfo
// LoadSourceConfig loads the source config and optionally adds the default vlaues
func LoadSourceConfig(dir string, applyDefaults bool) (*v1alpha1.SourceConfig, error) {
config := &v1alpha1.SourceConfig{}
path := filepath.Join(dir, ".jx", "gitops", v1alpha1.SourceConfigFileName)
exists, err := files.FileExists(path)
if err != nil {
return config, errors.Wrapf(err, "failed to check if file exists %s", path)
}
if !exists {
log.Logger().Infof("the source config file %s does not exist", info(path))
return config, nil
}
err = yamls.LoadFile(path, config)
if err != nil {
return config, errors.Wrapf(err, "failed to load file %s", path)
}
if applyDefaults {
err = DefaultConfigValues(config)
if err != nil {
return nil, err
}
}
return config, nil
}
// SaveSourceConfig saves the source config to the given directory
func SaveSourceConfig(config *v1alpha1.SourceConfig, dir string) error {
if config.APIVersion == "" {
config.APIVersion = v1alpha1.APIVersion
}
if config.Kind == "" {
config.Kind = v1alpha1.KindSourceConfig
}
outDir := filepath.Join(dir, ".jx", "gitops")
err := os.MkdirAll(outDir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to make directory %s", outDir)
}
path := filepath.Join(outDir, v1alpha1.SourceConfigFileName)
err = yamls.SaveFile(config, path)
if err != nil {
return errors.Wrapf(err, "failed to save %s", path)
}
return nil
}
// DefaultConfigValues defaults values from the given config, group and repository if they are missing
func DefaultConfigValues(config *v1alpha1.SourceConfig) error {
err := DefaultGroupValues(config, config.Spec.Groups)
if err != nil {
return err
}
for i := range config.Spec.JenkinsServers {
jenkinsServer := &config.Spec.JenkinsServers[i]
err := DefaultGroupValues(config, jenkinsServer.Groups)
if err != nil {
return err
}
}
return nil
}
// DefaultGroupValues defaults values from the given config, group and repository if they are missing
func DefaultGroupValues(config *v1alpha1.SourceConfig, groups []v1alpha1.RepositoryGroup) error {
for i := range groups {
group := &groups[i]
for j := range group.Repositories {
repo := &group.Repositories[j]
err := DefaultValues(config, group, repo)
if err != nil {
return errors.Wrap(err, " Could not set default values from the config, group and repo")
}
}
}
return nil
}
// DefaultValues defaults values from the given config, group and repository if they are missing
func DefaultValues(config *v1alpha1.SourceConfig, group *v1alpha1.RepositoryGroup, repo *v1alpha1.Repository) error {
group.Slack = group.Slack.Inherit(config.Spec.Slack)
if group.Provider == "" {
group.Provider = "https://github.com"
}
if group.ProviderKind == "" {
group.ProviderKind = "github"
}
if group.ProviderName == "" {
group.ProviderName = "github"
}
if group.Scheduler == "" {
group.Scheduler = config.Spec.Scheduler
}
if group.Owner == "" {
return errors.Errorf("missing group.owner")
}
if repo.Name == "" {
return errors.Errorf("missing repo.name")
}
if repo.URL == "" {
repo.URL = stringhelpers.UrlJoin(group.Provider, group.Owner, repo.Name)
}
if repo.HTTPCloneURL == "" {
repo.HTTPCloneURL = stringhelpers.UrlJoin(group.Provider, group.Owner, repo.Name+".git")
}
if repo.Scheduler == "" {
repo.Scheduler = group.Scheduler
}
repo.Slack = repo.Slack.Inherit(group.Slack)
return nil
}
// GetFilteredGroups get the groups for the given git kind & server URL if specified and owner
func GetFilteredGroups(groups []v1alpha1.RepositoryGroup, gitKind, gitServerURL, owner string) []v1alpha1.RepositoryGroup {
var filteredGroups []v1alpha1.RepositoryGroup
for g := range groups {
group := &groups[g]
if (group.ProviderKind == gitKind || gitKind == "") && (group.Provider == gitServerURL || gitServerURL == "") && strings.EqualFold(group.Owner, owner) {
filteredGroups = append(filteredGroups, *group)
}
}
return filteredGroups
}
// GetOrCreateGroup get or create the group for the given name
func GetOrCreateGroup(config *v1alpha1.SourceConfig, gitKind, gitServerURL, owner string) *v1alpha1.RepositoryGroup {
var group *v1alpha1.RepositoryGroup
config.Spec.Groups, group = getOrCreateGroup(config.Spec.Groups, gitKind, gitServerURL, owner)
return group
}
// GetOrCreateJenkinsServerGroup get or create the group for the given name
func GetOrCreateJenkinsServerGroup(config *v1alpha1.JenkinsServer, gitKind, gitServerURL, owner string) *v1alpha1.RepositoryGroup {
var group *v1alpha1.RepositoryGroup
config.Groups, group = getOrCreateGroup(config.Groups, gitKind, gitServerURL, owner)
return group
}
// getOrCreateGroup get or create the group for the given name
func getOrCreateGroup(groups []v1alpha1.RepositoryGroup, gitKind, gitServerURL, owner string) ([]v1alpha1.RepositoryGroup, *v1alpha1.RepositoryGroup) {
for i := range groups {
group := &groups[i]
if (group.ProviderKind == gitKind || gitKind == "") && (group.Provider == gitServerURL || gitServerURL == "") && strings.EqualFold(group.Owner, owner) {
return groups, group
}
}
groups = append(groups, v1alpha1.RepositoryGroup{
ProviderKind: gitKind,
Provider: gitServerURL,
Owner: owner,
})
return groups, &groups[len(groups)-1]
}
// GetOrCreateRepositoryFor returns the repository for the given git server URL if specified, owner and repository
func GetOrCreateRepositoryFor(config *v1alpha1.SourceConfig, gitServerURL, owner, repo string) *v1alpha1.Repository {
group := GetOrCreateGroup(config, "", gitServerURL, owner)
return GetOrCreateRepository(group, repo)
}
// GetRepositoryFor returns the repository for the given git server URL if specified, owner and given repo name
func GetRepositoryFor(config *v1alpha1.SourceConfig, gitServerURL, owner, repo string) *v1alpha1.Repository {
groups := GetFilteredGroups(config.Spec.Groups, "", gitServerURL, owner)
return GetRepository(groups, repo)
}
// GetRepository get the repository for the given name
func GetRepository(groups []v1alpha1.RepositoryGroup, repoName string) *v1alpha1.Repository {
for g := range groups {
group := &groups[g]
for r := range group.Repositories {
repo := &group.Repositories[r]
// Nested repositories in gitlab can have / in their name which includes the subgroup
// this replaces the / with -
if strings.Replace(repo.Name, "/", "-", -1) == repoName {
return repo
}
}
}
return nil
}
// GetOrCreateRepository get or create the repository for the given name
func GetOrCreateRepository(group *v1alpha1.RepositoryGroup, repoName string) *v1alpha1.Repository {
for i := range group.Repositories {
repo := &group.Repositories[i]
if repo.Name == repoName {
return repo
}
}
group.Repositories = append(group.Repositories, v1alpha1.Repository{
Name: repoName,
})
return &group.Repositories[len(group.Repositories)-1]
}
// GetOrCreateJenkinsServer get or create the jenkins server with the the given name
func GetOrCreateJenkinsServer(config *v1alpha1.SourceConfig, name string) *v1alpha1.JenkinsServer {
for i := range config.Spec.JenkinsServers {
js := &config.Spec.JenkinsServers[i]
if js.Server == name {
return js
}
}
config.Spec.JenkinsServers = append(config.Spec.JenkinsServers, v1alpha1.JenkinsServer{
Server: name,
})
return &config.Spec.JenkinsServers[len(config.Spec.JenkinsServers)-1]
}
// SortConfig sorts the repositories in each group
func SortConfig(config *v1alpha1.SourceConfig) {
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
SortRepositories(group.Repositories)
}
}
// SortRepositories sorts the repositories
func SortRepositories(repositories []v1alpha1.Repository) {
sort.Slice(repositories, func(i, j int) bool {
r1 := repositories[i]
r2 := repositories[j]
return r1.Name < r2.Name
})
}
// EnrichConfig ensures everything is populated
func EnrichConfig(config *v1alpha1.SourceConfig) {
if config.APIVersion == "" {
config.APIVersion = v1alpha1.APIVersion
}
if config.Kind == "" {
config.Kind = v1alpha1.KindSourceConfig
}
// lets add a default slack configuration if it doesn't exist
if config.Spec.Slack == nil {
config.Spec.Slack = DefaultSlackNotify()
}
}
func DefaultSlackNotify() *v1alpha1.SlackNotify {
return &v1alpha1.SlackNotify{
Channel: v1alpha1.DefaultSlackChannel,
Kind: v1alpha1.NotifyKindFailureOrFirstSuccess,
Pipeline: v1alpha1.PipelineKindRelease,
}
}
func DryConfig(config *v1alpha1.SourceConfig) {
// if all of the repositories in a group have the same scheduler then clear them all and set it on the group
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
scheduler := ""
for j := range group.Repositories {
repo := &group.Repositories[j]
if repo.Scheduler == "" {
scheduler = ""
break
}
if scheduler == "" {
scheduler = repo.Scheduler
} else if scheduler != repo.Scheduler {
scheduler = ""
break
}
}
if scheduler != "" {
group.Scheduler = scheduler
for j := range group.Repositories {
group.Repositories[j].Scheduler = ""
}
}
}
// if the URLs can be guessed from the group, omit them
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
provider := group.Provider
if provider == "" {
break
}
owner := group.Owner
for j := range group.Repositories {
repo := &group.Repositories[j]
name := repo.Name
url := stringhelpers.UrlJoin(provider, owner, name)
cloneURL := url + ".git"
description := "Imported application for " + owner + "/" + name
if repo.URL == url || repo.URL == cloneURL {
repo.URL = ""
}
if repo.HTTPCloneURL == cloneURL {
repo.HTTPCloneURL = ""
}
if repo.Description == description {
repo.Description = ""
}
}
}
}
// FindSettings finds the settings for the given owner and repository name
func FindSettings(config *v1alpha1.SourceConfig, owner, repoName string) *jxcore.SettingsConfig {
if owner == "" {
owner = os.Getenv("REPO_OWNER")
}
if repoName == "" {
repoName = os.Getenv("REPO_NAME")
}
// lets try find the group for the repository name
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
if group.Owner != owner {
continue
}
for j := range group.Repositories {
repo := &group.Repositories[j]
if repo.Name == repoName {
return group.Settings
}
}
}
// if the repo name can't be found then lets just find the first group for this owner
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
if group.Owner == owner {
return group.Settings
}
}
return nil
}
// RemoveRepository removes the repositories with the given name optionally matching on the owner
func RemoveRepository(config *v1alpha1.SourceConfig, owner, repoName string) bool {
modified := false
// lets try find the group for the repository name
for i := range config.Spec.Groups {
group := &config.Spec.Groups[i]
if owner != "" && group.Owner != owner {
continue
}
for j := range group.Repositories {
repo := &group.Repositories[j]
if repo.Name == repoName {
r2 := group.Repositories[0:j]
if j+1 < len(group.Repositories) {
r2 = append(r2, group.Repositories[j+1:]...)
}
group.Repositories = r2
modified = true
break
}
}
}
return modified
}