forked from kyma-project/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenericcomponent.go
100 lines (87 loc) · 3.6 KB
/
genericcomponent.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
package rendertemplates
import (
"strings"
)
// changeExtraRefsBase changes base_ref to base string for each extra_ref
func (j *Job) changeExtraRefsBase(base string) {
if j.JobConfig["extra_refs"] != nil {
for extraRefIndex := range j.JobConfig["extra_refs"].(ConfigSet) {
j.JobConfig["extra_refs"].(ConfigSet)[extraRefIndex].([]any)[0].(ConfigSet)["base_ref"] = base
}
}
}
// GenerateComponentJobs takes in a map of global values and generates jobs for all components
func (r *RenderConfig) GenerateComponentJobs(global map[string]interface{}) {
if present := len(r.JobConfigs); present > 0 {
for repoIndex, repo := range r.JobConfigs {
var jobs []Job
hasComponentJobs := false
for _, job := range repo.Jobs {
// check if the jobConfig is a component job
if job.JobConfig["name"] == nil && job.JobConfig["path"] != nil {
hasComponentJobs = true
// generate component jobs
componentJobs := generateSingleComponentJobs(global, repo, job)
jobs = append(jobs, componentJobs...)
} else {
// append the job to the list, making it possible to mix component job definitions and regular ones
jobs = append(jobs, job)
}
}
// replace jobs if there were generated ones, don't change anything otherwise
if hasComponentJobs {
r.JobConfigs[repoIndex].Jobs = jobs
}
}
}
}
// generateComponentJobs generates jobs for a single components
func generateSingleComponentJobs(global map[string]interface{}, repo Repo, job Job) []Job {
var jobs []Job
// generate component jobs
// get last element of repoName, this way "github.com/" part can be omitted
repoNameSlice := strings.Split(repo.RepoName, "/")
repository := repoNameSlice[len(repoNameSlice)-1]
nameSuffix := repository + "-" + strings.Replace(job.JobConfig["path"].(string), "/", "-", -1)
// generate pre- and post-submit jobs for the next release
if ReleaseMatches(global["nextRelease"], job.JobConfig["release_since"], job.JobConfig["release_until"]) {
if len(job.JobConfigPre) > 0 {
preSubmit := Job{}
preSubmit.JobConfig = deepCopyConfigSet(job.JobConfigPre)
preSubmit.JobConfig["name"] = "pre-" + nameSuffix
jobs = append(jobs, preSubmit)
}
if len(job.JobConfigPost) > 0 {
postSubmit := Job{}
postSubmit.JobConfig = deepCopyConfigSet(job.JobConfigPost)
postSubmit.JobConfig["name"] = "post-" + nameSuffix
jobs = append(jobs, postSubmit)
}
}
// check if we have to generate jobs for the previous supported releases
if job.JobConfig["skipReleaseJobs"] == nil || job.JobConfig["skipReleaseJobs"].(string) != "true" {
matchingReleases := MatchingReleases(global["releases"].([]interface{}), job.JobConfig["release_since"], job.JobConfig["release_until"])
for _, currentRelease := range matchingReleases {
rel := currentRelease.(string)
nameRelease := "rel" + strings.Replace(rel, ".", "", -1)
commonRelBranches := []string{"release-" + rel}
if len(job.JobConfigPre) > 0 {
preSubmitRel := Job{}
preSubmitRel.JobConfig = deepCopyConfigSet(job.JobConfigPre)
preSubmitRel.JobConfig["name"] = "pre-" + nameRelease + "-" + nameSuffix
preSubmitRel.JobConfig["branches"] = commonRelBranches
preSubmitRel.changeExtraRefsBase("release-" + rel)
jobs = append(jobs, preSubmitRel)
}
if len(job.JobConfigPost) > 0 {
postSubmitRel := Job{}
postSubmitRel.JobConfig = deepCopyConfigSet(job.JobConfigPost)
postSubmitRel.JobConfig["name"] = "post-" + nameRelease + "-" + nameSuffix
postSubmitRel.JobConfig["branches"] = commonRelBranches
postSubmitRel.changeExtraRefsBase("release-" + rel)
jobs = append(jobs, postSubmitRel)
}
}
}
return jobs
}