-
Notifications
You must be signed in to change notification settings - Fork 465
/
EventBuilder.go
249 lines (235 loc) · 9.86 KB
/
EventBuilder.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
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package client
import (
"context"
"fmt"
bean2 "github.com/devtron-labs/devtron/api/bean"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/sql/repository/chartConfig"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/pkg/bean"
"github.com/devtron-labs/devtron/pkg/user/repository"
"github.com/devtron-labs/devtron/util/event"
"github.com/go-pg/pg"
"github.com/satori/go.uuid"
"go.uber.org/zap"
"strings"
"time"
)
type EventFactory interface {
Build(eventType util.EventType, sourceId *int, appId int, envId *int, pipelineType util.PipelineType) Event
BuildExtraCDData(event Event, wfr *pipelineConfig.CdWorkflowRunner, pipelineOverrideId int, stage bean2.WorkflowType) Event
BuildExtraCIData(event Event, material *MaterialTriggerInfo, dockerImage string) Event
//BuildFinalData(event Event) *Payload
}
type EventSimpleFactoryImpl struct {
logger *zap.SugaredLogger
cdWorkflowRepository pipelineConfig.CdWorkflowRepository
pipelineOverrideRepository chartConfig.PipelineOverrideRepository
ciWorkflowRepository pipelineConfig.CiWorkflowRepository
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository
ciPipelineRepository pipelineConfig.CiPipelineRepository
pipelineRepository pipelineConfig.PipelineRepository
userRepository repository.UserRepository
ciArtifactRepository repository2.CiArtifactRepository
}
func NewEventSimpleFactoryImpl(logger *zap.SugaredLogger, cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
pipelineOverrideRepository chartConfig.PipelineOverrideRepository, ciWorkflowRepository pipelineConfig.CiWorkflowRepository,
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository,
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
userRepository repository.UserRepository, ciArtifactRepository repository2.CiArtifactRepository) *EventSimpleFactoryImpl {
return &EventSimpleFactoryImpl{
logger: logger,
cdWorkflowRepository: cdWorkflowRepository,
pipelineOverrideRepository: pipelineOverrideRepository,
ciWorkflowRepository: ciWorkflowRepository,
ciPipelineMaterialRepository: ciPipelineMaterialRepository,
ciPipelineRepository: ciPipelineRepository,
pipelineRepository: pipelineRepository,
userRepository: userRepository,
ciArtifactRepository: ciArtifactRepository,
}
}
func (impl *EventSimpleFactoryImpl) Build(eventType util.EventType, sourceId *int, appId int, envId *int, pipelineType util.PipelineType) Event {
correlationId := uuid.NewV4()
event := Event{}
event.EventTypeId = int(eventType)
if sourceId != nil {
event.PipelineId = *sourceId
}
event.AppId = appId
if envId != nil {
event.EnvId = *envId
}
event.PipelineType = string(pipelineType)
event.CorrelationId = fmt.Sprintf("%s", correlationId)
event.EventTime = time.Now().Format(bean.LayoutRFC3339)
return event
}
func (impl *EventSimpleFactoryImpl) BuildExtraCDData(event Event, wfr *pipelineConfig.CdWorkflowRunner, pipelineOverrideId int, stage bean2.WorkflowType) Event {
//event.CdWorkflowRunnerId =
event.CdWorkflowType = stage
payload := event.Payload
if payload == nil {
payload = &Payload{}
payload.Stage = string(stage)
event.Payload = payload
}
if wfr != nil {
material, err := impl.getCiMaterialInfo(wfr.CdWorkflow.Pipeline.CiPipelineId, wfr.CdWorkflow.CiArtifactId)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "event", event, "stage", stage, "workflow runner", wfr, "pipelineOverrideId", pipelineOverrideId)
}
payload.MaterialTriggerInfo = material
payload.DockerImageUrl = wfr.CdWorkflow.CiArtifact.Image
event.UserId = int(wfr.TriggeredBy)
event.Payload = payload
event.CdWorkflowRunnerId = wfr.Id
event.CiArtifactId = wfr.CdWorkflow.CiArtifactId
} else if pipelineOverrideId > 0 {
pipelineOverride, err := impl.pipelineOverrideRepository.FindById(pipelineOverrideId)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "event", event, "stage", stage, "workflow runner", wfr, "pipelineOverrideId", pipelineOverrideId)
}
if pipelineOverride != nil && pipelineOverride.Id > 0 {
cdWorkflow, err := impl.cdWorkflowRepository.FindById(pipelineOverride.CdWorkflowId)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "cdWorkflow", cdWorkflow, "event", event, "stage", stage, "workflow runner", wfr, "pipelineOverrideId", pipelineOverrideId)
}
wfr, err := impl.cdWorkflowRepository.FindByWorkflowIdAndRunnerType(context.Background(), cdWorkflow.Id, stage)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "wfr", wfr, "event", event, "stage", stage, "workflow runner", wfr, "pipelineOverrideId", pipelineOverrideId)
}
if wfr.Id > 0 {
event.CdWorkflowRunnerId = wfr.Id
event.CiArtifactId = pipelineOverride.CiArtifactId
material, err := impl.getCiMaterialInfo(pipelineOverride.CiArtifact.PipelineId, pipelineOverride.CiArtifactId)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "material", material)
}
payload.MaterialTriggerInfo = material
payload.DockerImageUrl = wfr.CdWorkflow.CiArtifact.Image
event.UserId = int(wfr.TriggeredBy)
}
}
event.Payload = payload
} else if event.PipelineId > 0 {
pipeline, err := impl.pipelineRepository.FindById(event.PipelineId)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "pipeline", pipeline)
}
if pipeline != nil {
material, err := impl.getCiMaterialInfo(pipeline.CiPipelineId, 0)
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "material", material)
}
payload.MaterialTriggerInfo = material
}
event.Payload = payload
}
if event.UserId > 0 {
user, err := impl.userRepository.GetById(int32(event.UserId))
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "user", user)
}
payload = event.Payload
payload.TriggeredBy = user.EmailId
event.Payload = payload
}
return event
}
func (impl *EventSimpleFactoryImpl) BuildExtraCIData(event Event, material *MaterialTriggerInfo, dockerImage string) Event {
if material == nil {
materialInfo, err := impl.getCiMaterialInfo(event.PipelineId, event.CiArtifactId)
if err != nil {
impl.logger.Errorw("found error on payload build for ci, skipping this error ", "materialInfo", materialInfo)
}
material = materialInfo
} else if material.CiMaterials == nil {
materialInfo, err := impl.getCiMaterialInfo(event.PipelineId, 0)
if err != nil {
impl.logger.Errorw("found error on payload build for ci, skipping this error ", "materialInfo", materialInfo)
}
materialInfo.GitTriggers = material.GitTriggers
material = materialInfo
}
payload := event.Payload
if payload == nil {
payload = &Payload{}
event.Payload = payload
}
event.Payload.MaterialTriggerInfo = material
if event.UserId > 0 {
user, err := impl.userRepository.GetById(int32(event.UserId))
if err != nil {
impl.logger.Errorw("found error on payload build for cd stages, skipping this error ", "user", user)
}
payload = event.Payload
payload.TriggeredBy = user.EmailId
event.Payload = payload
}
return event
}
func (impl *EventSimpleFactoryImpl) getCiMaterialInfo(ciPipelineId int, ciArtifactId int) (*MaterialTriggerInfo, error) {
materialTriggerInfo := &MaterialTriggerInfo{}
if ciPipelineId > 0 {
ciMaterials, err := impl.ciPipelineMaterialRepository.GetByPipelineId(ciPipelineId)
if err != nil {
impl.logger.Errorw("error on fetching materials for", "ciPipelineId", ciPipelineId, "err", err)
return nil, err
}
var ciMaterialsArr []CiPipelineMaterialResponse
for _, m := range ciMaterials {
if m.GitMaterial == nil {
impl.logger.Warnw("git material are empty", "material", m)
continue
}
res := CiPipelineMaterialResponse{
Id: m.Id,
GitMaterialId: m.GitMaterialId,
GitMaterialName: m.GitMaterial.Name[strings.Index(m.GitMaterial.Name, "-")+1:],
Type: string(m.Type),
Value: m.Value,
Active: m.Active,
Url: m.GitMaterial.Url,
}
ciMaterialsArr = append(ciMaterialsArr, res)
}
materialTriggerInfo.CiMaterials = ciMaterialsArr
}
if ciArtifactId > 0 {
ciArtifact, err := impl.ciArtifactRepository.Get(ciArtifactId)
if err != nil {
impl.logger.Errorw("error fetching artifact data", "err", err)
return nil, err
}
// handling linked ci pipeline
if ciArtifact.ParentCiArtifact > 0 && ciArtifact.WorkflowId == nil {
ciArtifactId = ciArtifact.ParentCiArtifact
}
ciWf, err := impl.ciWorkflowRepository.FindLastTriggeredWorkflowByArtifactId(ciArtifactId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error fetching ci workflow data by artifact", "err", err)
return nil, err
}
if ciWf != nil {
materialTriggerInfo.GitTriggers = ciWf.GitTriggers
}
}
return materialTriggerInfo, nil
}