forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
301 lines (280 loc) · 8.06 KB
/
activity.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
package kube
import (
"reflect"
"time"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
typev1 "github.com/jenkins-x/jx/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type PipelineActivityKey struct {
Name string
Pipeline string
Build string
BuildURL string
BuildLogsURL string
ReleaseNotesURL string
GitInfo *gits.GitRepositoryInfo
}
func (k *PipelineActivityKey) IsValid() bool {
return len(k.Name) > 0
}
type PromoteStepActivityKey struct {
PipelineActivityKey
Environment string
ApplicationURL string
}
type PromotePullRequestFn func(*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PromoteActivityStep, *v1.PromotePullRequestStep) error
type PromoteUpdateFn func(*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PromoteActivityStep, *v1.PromoteUpdateStep) error
// GetOrCreate gets or creates the pipeline activity
func (k *PipelineActivityKey) GetOrCreate(activities typev1.PipelineActivityInterface) (*v1.PipelineActivity, error) {
name := k.Name
create := false
defaultActivity := &v1.PipelineActivity{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1.PipelineActivitySpec{},
}
if activities == nil {
log.Warn("Warning: no PipelineActivities client available!")
return defaultActivity, nil
}
a, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
create = true
a = defaultActivity
}
spec := &a.Spec
if k.Pipeline != "" && spec.Pipeline == "" {
spec.Pipeline = k.Pipeline
}
if k.Build != "" && spec.Build == "" {
spec.Build = k.Build
}
if k.BuildURL != "" && spec.BuildURL == "" {
spec.BuildURL = k.BuildURL
}
if k.BuildLogsURL != "" && spec.BuildLogsURL == "" {
spec.BuildLogsURL = k.BuildLogsURL
}
if k.ReleaseNotesURL != "" && spec.ReleaseNotesURL == "" {
spec.ReleaseNotesURL = k.ReleaseNotesURL
}
gi := k.GitInfo
if gi != nil {
if gi.URL != "" && spec.GitURL == "" {
spec.GitURL = gi.URL
}
if gi.Organisation != "" && spec.GitOwner == "" {
spec.GitOwner = gi.Organisation
}
if gi.Name != "" && spec.GitRepository == "" {
spec.GitRepository = gi.Name
}
}
if create {
return activities.Create(a)
} else {
return a, nil
}
}
// GetOrCreatePreview gets or creates the Preview step for the key
func (k *PromoteStepActivityKey) GetOrCreatePreview(activities typev1.PipelineActivityInterface) (*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PreviewActivityStep, bool, error) {
a, err := k.GetOrCreate(activities)
if err != nil {
return nil, nil, nil, false, err
}
spec := &a.Spec
for _, step := range spec.Steps {
if k.matchesPreview(&step) {
return a, &step, step.Preview, false, nil
}
}
// if there is no initial release Stage lets add one
if len(spec.Steps) == 0 {
endTime := time.Now()
startTime := endTime.Add(-1 * time.Minute)
spec.Steps = append(spec.Steps, v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypeStage,
Stage: &v1.StageActivityStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: startTime,
},
CompletedTimestamp: &metav1.Time{
Time: endTime,
},
Status: v1.ActivityStatusTypeSucceeded,
Name: "Release",
},
},
})
}
// lets add a new step
preview := &v1.PreviewActivityStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: time.Now(),
},
},
Environment: k.Environment,
}
step := v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypePreview,
Preview: preview,
}
spec.Steps = append(spec.Steps, step)
return a, &spec.Steps[len(spec.Steps)-1], preview, true, nil
}
// GetOrCreatePromote gets or creates the Promote step for the key
func (k *PromoteStepActivityKey) GetOrCreatePromote(activities typev1.PipelineActivityInterface) (*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PromoteActivityStep, bool, error) {
a, err := k.GetOrCreate(activities)
if err != nil {
return nil, nil, nil, false, err
}
spec := &a.Spec
for _, step := range spec.Steps {
if k.matchesPromote(&step) {
return a, &step, step.Promote, false, nil
}
}
// if there is no initial release Stage lets add one
if len(spec.Steps) == 0 {
endTime := time.Now()
startTime := endTime.Add(-1 * time.Minute)
spec.Steps = append(spec.Steps, v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypeStage,
Stage: &v1.StageActivityStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: startTime,
},
CompletedTimestamp: &metav1.Time{
Time: endTime,
},
Status: v1.ActivityStatusTypeSucceeded,
Name: "Release",
},
},
})
}
// lets add a new step
promote := &v1.PromoteActivityStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: time.Now(),
},
},
Environment: k.Environment,
}
step := v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypePromote,
Promote: promote,
}
spec.Steps = append(spec.Steps, step)
return a, &spec.Steps[len(spec.Steps)-1], promote, true, nil
}
// GetOrCreatePromotePullRequest gets or creates the PromotePullRequest for the key
func (k *PromoteStepActivityKey) GetOrCreatePromotePullRequest(activities typev1.PipelineActivityInterface) (*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PromoteActivityStep, *v1.PromotePullRequestStep, bool, error) {
a, s, p, created, err := k.GetOrCreatePromote(activities)
if err != nil {
return nil, nil, nil, nil, created, err
}
if p.PullRequest == nil {
created = true
p.PullRequest = &v1.PromotePullRequestStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: time.Now(),
},
},
}
}
return a, s, p, p.PullRequest, created, err
}
// GetOrCreatePromoteUpdate gets or creates the Promote for the key
func (k *PromoteStepActivityKey) GetOrCreatePromoteUpdate(activities typev1.PipelineActivityInterface) (*v1.PipelineActivity, *v1.PipelineActivityStep, *v1.PromoteActivityStep, *v1.PromoteUpdateStep, bool, error) {
a, s, p, created, err := k.GetOrCreatePromote(activities)
if err != nil {
return nil, nil, nil, nil, created, err
}
// lets check the PR is completed
if p.PullRequest != nil {
if p.PullRequest.Status == v1.ActivityStatusTypeNone {
p.PullRequest.Status = v1.ActivityStatusTypeSucceeded
}
}
if p.Update == nil {
created = true
p.Update = &v1.PromoteUpdateStep{
CoreActivityStep: v1.CoreActivityStep{
StartedTimestamp: &metav1.Time{
Time: time.Now(),
},
},
}
}
return a, s, p, p.Update, created, err
}
func (k *PromoteStepActivityKey) OnPromotePullRequest(activities typev1.PipelineActivityInterface, fn PromotePullRequestFn) error {
if !k.IsValid() {
return nil
}
if activities == nil {
log.Warn("Warning: no PipelineActivities client available!")
return nil
}
a, s, ps, p, added, err := k.GetOrCreatePromotePullRequest(activities)
if err != nil {
return err
}
p1 := *p
err = fn(a, s, ps, p)
if err != nil {
return err
}
p2 := *p
if added || !reflect.DeepEqual(p1, p2) {
_, err = activities.Update(a)
}
return err
}
func (k *PromoteStepActivityKey) OnPromoteUpdate(activities typev1.PipelineActivityInterface, fn PromoteUpdateFn) error {
if !k.IsValid() {
return nil
}
if activities == nil {
log.Warn("Warning: no PipelineActivities client available!")
return nil
}
a, s, ps, p, added, err := k.GetOrCreatePromoteUpdate(activities)
if err != nil {
return err
}
p1 := *p
if k.ApplicationURL != "" {
ps.ApplicationURL = k.ApplicationURL
}
err = fn(a, s, ps, p)
if err != nil {
return err
}
if k.ApplicationURL != "" {
ps.ApplicationURL = k.ApplicationURL
}
p2 := *p
if added || !reflect.DeepEqual(p1, p2) {
_, err = activities.Update(a)
}
return err
}
func (k *PromoteStepActivityKey) matchesPreview(step *v1.PipelineActivityStep) bool {
s := step.Preview
return s != nil && s.Environment == k.Environment
}
func (k *PromoteStepActivityKey) matchesPromote(step *v1.PipelineActivityStep) bool {
s := step.Promote
return s != nil && s.Environment == k.Environment
}