-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
231 lines (190 loc) · 5.18 KB
/
jobs.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
package internal
import (
"fmt"
"sort"
"github.com/CircleCI-Public/circleci-config/config"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
type Type uint32
// This type is to help us build a workflow graph
const (
TestJob Type = iota // generic test job
ArtifactJob
DeployJob
)
type Job struct {
config.Job
Type
// map of orb name (e.g. "slack") to registry key (e.g. "circleci/[email protected]")
// for orbs required by this job
Orbs map[string]string
}
func BuildConfig(ls labels.LabelSet, jobs []*Job) config.Config {
if len(jobs) == 0 {
return buildFallbackConfig(ls)
}
jobs = addStubJobs(ls, jobs)
// Can jobs not just be "cast" to []*config.Jobs somehow?
configJobs := make([]*config.Job, len(jobs))
for i := range jobs {
configJobs[i] = &jobs[i].Job
}
workflows := buildWorkflows(jobs)
return config.Config{
Comment: fmt.Sprintf("This config was automatically generated from your source code\n"+
"Stacks detected: %s", ls.String()),
Workflows: workflows,
Jobs: configJobs,
Orbs: buildOrbs(jobs),
}
}
func buildDeployJob(ls labels.LabelSet) *Job {
deployJob := stubDeployJob
deployJob.Steps = stubDeployJob.Steps
deployJob.Steps = append(deployJob.Steps, getCICDSteps(ls)...)
if emptyRepoLabel, ok := ls[labels.EmptyRepo]; ok && emptyRepoLabel.Valid {
deployJob.Steps = append(deployJob.Steps, getEmptyJobSteps(ls)...)
}
return &deployJob
}
func buildFallbackConfig(ls labels.LabelSet) config.Config {
deployJob := buildDeployJob(ls)
deployJob.Comment = ""
comment := `Couldn't automatically generate a config from your source code.
This is a generic template to serve as a base for your custom config
See: https://circleci.com/docs/configuration-reference`
if len(ls) > 0 {
comment = fmt.Sprintf("%s\n"+
"Stacks detected: %s", comment, ls.String())
}
return config.Config{
Comment: comment,
Jobs: []*config.Job{
&stubTestJob.Job,
&stubArtifactJob.Job,
&deployJob.Job,
},
Workflows: []*config.Workflow{
{
Name: "example",
Jobs: []config.WorkflowJob{
{
Job: &stubTestJob.Job,
}, {
Job: &stubArtifactJob.Job,
Requires: []*config.Job{
&stubTestJob.Job,
},
}, {
Job: &deployJob.Job,
Requires: []*config.Job{
&stubTestJob.Job,
},
},
},
},
},
}
}
func getCICDSteps(ls labels.LabelSet) []config.Step {
steps := make([]config.Step, 0)
cicdStepMap := map[string]string{
labels.CICDGithubActions: "github actions",
labels.CICDGitlabWorkflow: "gitlab workflows",
labels.CICDJenkins: "jenkins",
}
for label, cicdName := range cicdStepMap {
if ciLabel, ok := ls[label]; ok && ciLabel.Valid {
steps = append(steps, config.Step{
Type: config.Run,
Name: fmt.Sprintf("found %s config", cicdName),
Command: ":", // dont do anything just return status 0 and continue
})
}
}
return steps
}
func addStubJobs(ls labels.LabelSet, jobs []*Job) []*Job {
jobTypesPresent := map[Type]bool{}
for _, j := range jobs {
jobTypesPresent[j.Type] = true
}
if !jobTypesPresent[TestJob] && !jobTypesPresent[ArtifactJob] {
jobs = append(jobs, &stubTestJob)
}
if !jobTypesPresent[DeployJob] {
deployJob := buildDeployJob(ls)
jobs = append(jobs, deployJob)
}
return jobs
}
func buildOrbs(jobs []*Job) []config.Orb {
// merge all jobs orb maps
orbsByName := make(map[string]string)
for _, j := range jobs {
for name, registryKey := range j.Orbs {
orbsByName[name] = registryKey
}
}
orbs := make([]config.Orb, len(orbsByName))
i := 0
for name, registryKey := range orbsByName {
orbs[i] = config.Orb{Name: name, RegistryKey: registryKey}
i++
}
// sort to make the slice order dependable for tests
sort.Slice(orbs, func(i, j int) bool {
return orbs[i].Name < orbs[j].Name
})
return orbs
}
func buildWorkflows(jobs []*Job) []*config.Workflow {
// DeployJobs are added, but commented out
workflowJobs := make([]config.WorkflowJob, len(jobs))
for i, j := range jobs {
workflowJobs[i].Job = &j.Job
workflowJobs[i].CommentedOut = j.Type == DeployJob
workflowJobs[i].Requires = workflowJobRequires(j, jobs)
}
name := "build"
for _, j := range jobs {
if j.Type == TestJob {
name = "build-and-test"
}
}
return []*config.Workflow{{
Name: name,
Jobs: workflowJobs,
}}
}
func workflowJobRequires(job *Job, allJobs []*Job) []*config.Job {
jobsByType := getJobsByType(allJobs)
if job.Type == ArtifactJob {
return jobsByType[TestJob]
}
if job.Type == DeployJob {
// if there are artifact jobs, it's sufficient to depend on them,
// as they already depend on any test jobs themselves
if len(jobsByType[ArtifactJob]) > 0 {
return jobsByType[ArtifactJob]
}
return jobsByType[TestJob]
}
return []*config.Job{}
}
func getJobsByType(jobs []*Job) map[Type][]*config.Job {
jobsByType := map[Type][]*config.Job{}
for _, j := range jobs {
jobsByType[j.Type] = append(jobsByType[j.Type], &j.Job)
}
return jobsByType
}
func getEmptyJobSteps(ls labels.LabelSet) []config.Step {
steps := make([]config.Step, 0)
steps = append(steps, config.Step{
Type: config.Run,
Name: "found empty repo",
Command: ":", // dont do anything just return status 0 and continue
})
return steps
}