-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathCiWorkflowRepository.go
More file actions
440 lines (392 loc) · 19.1 KB
/
CiWorkflowRepository.go
File metadata and controls
440 lines (392 loc) · 19.1 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* 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 pipelineConfig
import (
"time"
"github.com/devtron-labs/devtron/internal/sql/constants"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type CiWorkflowRepository interface {
SaveWorkFlowWithTx(wf *CiWorkflow, tx *pg.Tx) error
FindLastTriggeredWorkflow(pipelineId int) (*CiWorkflow, error)
UpdateWorkFlowWithTx(wf *CiWorkflow, tx *pg.Tx) error
UpdateArtifactUploaded(id int, isUploaded workflow.ArtifactUploadedType) error
FindByStatusesIn(activeStatuses []string) ([]*CiWorkflow, error)
FindByPipelineId(pipelineId int, offset int, size int) ([]WorkflowWithArtifact, error)
FindById(id int) (*CiWorkflow, error)
FindRetriedWorkflowCountByReferenceId(id int) (int, error)
FindCiWorkflowGitTriggersById(id int) (workflow *CiWorkflow, err error)
FindCiWorkflowGitTriggersByIds(ids []int) ([]*CiWorkflow, error)
FindByName(name string) (*CiWorkflow, error)
FindRunningWorkflowsForPipeline(pipelineId int) ([]*CiWorkflow, error)
FindLastTriggeredWorkflowByCiIds(pipelineId []int) (ciWorkflow []*CiWorkflow, err error)
FindWorkflowsByCiWorkflowIds(ciWorkflowIds []int) (ciWorkflow []*CiWorkflow, err error)
FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error)
FindAllLastTriggeredWorkflowByArtifactId(ciArtifactId []int) (ciWorkflow []*CiWorkflow, err error)
FindAllTriggeredWorkflowCountInLast24Hour() (ciWorkflowCount int, err error)
FindLastTriggeredWorkflowGitTriggersByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error)
FindLastTriggeredWorkflowGitTriggersByArtifactIds(ciArtifactIds []int) ([]*WorkflowWithArtifact, error)
ExistsByStatus(status string) (bool, error)
FindBuildTypeAndStatusDataOfLast1Day() ([]*BuildTypeCount, error)
FIndCiWorkflowStatusesByAppId(appId int) ([]*CiWorkflowStatus, error)
FindCiPipelineIdsByAppId(appId int) ([]int, error)
MigrateIsArtifactUploaded(wfId int, isArtifactUploaded bool)
MigrateCiArtifactLocation(wfId int, artifactLocation string)
}
type CiWorkflowRepositoryImpl struct {
dbConnection *pg.DB
logger *zap.SugaredLogger
}
type CiWorkflow struct {
tableName struct{} `sql:"ci_workflow" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Name string `sql:"name"`
Status string `sql:"status"`
PodStatus string `sql:"pod_status"`
Message string `sql:"message"`
StartedOn time.Time `sql:"started_on"`
FinishedOn time.Time `sql:"finished_on"`
CiPipelineId int `sql:"ci_pipeline_id"`
Namespace string `sql:"namespace"`
BlobStorageEnabled bool `sql:"blob_storage_enabled,notnull"`
LogLocation string `sql:"log_file_path"`
GitTriggers map[int]GitCommit `sql:"git_triggers"`
TriggeredBy int32 `sql:"triggered_by"`
CiArtifactLocation string `sql:"ci_artifact_location"`
IsArtifactUploaded workflow.ArtifactUploadedType `sql:"is_artifact_uploaded"`
PodName string `sql:"pod_name"`
CiBuildType string `sql:"ci_build_type"`
EnvironmentId int `sql:"environment_id"`
ReferenceCiWorkflowId int `sql:"ref_ci_workflow_id"`
ParentCiWorkFlowId int `sql:"parent_ci_workflow_id"`
ExecutorType cdWorkflow.WorkflowExecutorType `sql:"executor_type"` //awf, system
ImagePathReservationId int `sql:"image_path_reservation_id"`
ImagePathReservationIds []int `sql:"image_path_reservation_ids" pg:",array"`
CiPipeline *CiPipeline
}
func (ciWorkflow *CiWorkflow) GetIsArtifactUploaded() (isArtifactUploaded bool, isMigrationRequired bool) {
return workflow.IsArtifactUploaded(ciWorkflow.IsArtifactUploaded)
}
func (ciWorkflow *CiWorkflow) WithIsArtifactUploaded(isArtifactUploaded bool) *CiWorkflow {
ciWorkflow.IsArtifactUploaded = workflow.GetArtifactUploadedType(isArtifactUploaded)
return ciWorkflow
}
func (ciWorkflow *CiWorkflow) InProgress() bool {
return ciWorkflow.Status == "Running" || ciWorkflow.Status == "Starting"
}
func (ciWorkflow *CiWorkflow) IsExternalRunInJobType() bool {
return ciWorkflow.EnvironmentId != 0
}
type WorkflowWithArtifact struct {
Id int `sql:"id"`
Name string `sql:"name"`
PodName string `sql:"pod_name"`
Status string `sql:"status"`
PodStatus string `sql:"pod_status"`
Message string `sql:"message"`
StartedOn time.Time `sql:"started_on"`
FinishedOn time.Time `sql:"finished_on"`
CiPipelineId int `sql:"ci_pipeline_id"`
Namespace string `sql:"namespace"`
LogFilePath string `sql:"log_file_path"`
GitTriggers map[int]GitCommit `sql:"git_triggers"`
TriggeredBy int32 `sql:"triggered_by"`
EmailId string `sql:"email_id"`
Image string `sql:"image"`
TargetPlatforms string `sql:"target_platforms"`
CiArtifactLocation string `sql:"ci_artifact_location"`
CiArtifactId int `sql:"ci_artifact_id"`
BlobStorageEnabled bool `sql:"blob_storage_enabled"`
CiBuildType string `sql:"ci_build_type"`
IsArtifactUploadedV2 workflow.ArtifactUploadedType `sql:"is_artifact_uploaded"` // IsArtifactUploadedV2 is the new column from ci_workflow table, IsArtifactUploaded is Deprecated and will be removed in future
IsArtifactUploaded bool `sql:"old_is_artifact_uploaded"` // Deprecated; Use IsArtifactUploadedV2 instead. IsArtifactUploaded is the column from ci_artifact table
EnvironmentId int `sql:"environment_id"`
EnvironmentName string `sql:"environment_name"`
RefCiWorkflowId int `sql:"ref_ci_workflow_id"`
ParentCiWorkflowId int `sql:"parent_ci_workflow_id"`
ExecutorType cdWorkflow.WorkflowExecutorType `sql:"executor_type"` //awf, system
ImagePathReservationId int `sql:"image_path_reservation_id"`
ImagePathReservationIds []int `sql:"image_path_reservation_ids" pg:",array"`
}
func (w *WorkflowWithArtifact) GetIsArtifactUploaded() (isArtifactUploaded bool, isMigrationRequired bool) {
return workflow.IsArtifactUploaded(w.IsArtifactUploadedV2)
}
type GitCommit struct {
Commit string //git hash
Author string
Date time.Time
Message string
Changes []string
WebhookData WebhookData
CiConfigureSourceValue string
GitRepoUrl string
GitRepoName string
CiConfigureSourceType constants.SourceType
}
type WebhookData struct {
Id int `json:"id"`
EventActionType string `json:"eventActionType"`
Data map[string]string `json:"data"`
}
func NewCiWorkflowRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *CiWorkflowRepositoryImpl {
return &CiWorkflowRepositoryImpl{
dbConnection: dbConnection,
logger: logger,
}
}
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflow(pipelineId int) (ciWorkflow *CiWorkflow, err error) {
workflow := &CiWorkflow{}
err = impl.dbConnection.Model(workflow).
Column("ci_workflow.*", "CiPipeline").
Where("ci_workflow.ci_pipeline_id = ? ", pipelineId).
Order("ci_workflow.started_on Desc").
Limit(1).
Select()
return workflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindByStatusesIn(activeStatuses []string) ([]*CiWorkflow, error) {
var ciWorkFlows []*CiWorkflow
err := impl.dbConnection.Model(&ciWorkFlows).
Column("ci_workflow.*").
Where("ci_workflow.status in (?)", pg.In(activeStatuses)).
Select()
return ciWorkFlows, err
}
func (impl *CiWorkflowRepositoryImpl) FindRunningWorkflowsForPipeline(pipelineId int) ([]*CiWorkflow, error) {
var ciWorkFlows []*CiWorkflow
// Status values for running/pending workflows that can be aborted
runningStatuses := []string{"Running", "Starting", "Pending"}
err := impl.dbConnection.Model(&ciWorkFlows).
Column("ci_workflow.*", "CiPipeline").
Where("ci_workflow.ci_pipeline_id = ?", pipelineId).
Where("ci_workflow.status in (?)", pg.In(runningStatuses)).
Order("ci_workflow.started_on DESC").
Select()
return ciWorkFlows, err
}
// FindByPipelineId gets only those workflowWithArtifact whose parent_ci_workflow_id is null, this is done to accommodate multiple ci_artifacts through a single workflow(parent), making child workflows for other ci_artifacts (this has been done due to design understanding and db constraint) single workflow single ci-artifact
func (impl *CiWorkflowRepositoryImpl) FindByPipelineId(pipelineId int, offset int, limit int) ([]WorkflowWithArtifact, error) {
var wfs []WorkflowWithArtifact
err := impl.dbConnection.Model().
TableExpr("ci_workflow AS wf").
ColumnExpr("cia.id AS ci_artifact_id").
ColumnExpr("env.environment_name").
ColumnExpr("cia.image").
ColumnExpr("cia.target_platforms").
ColumnExpr("cia.is_artifact_uploaded AS old_is_artifact_uploaded").
ColumnExpr("wf.*").
ColumnExpr("u.email_id").
Join("LEFT JOIN users u").
JoinOn("u.id = wf.triggered_by").
Join("LEFT JOIN ci_artifact cia").
JoinOn("wf.id = cia.ci_workflow_id").
Join("LEFT JOIN environment env").
JoinOn("env.id = wf.environment_id").
Where("wf.ci_pipeline_id = ?", pipelineId).
Where("parent_ci_workflow_id is NULL").
Order("wf.started_on DESC").
Offset(offset).
Limit(limit).
Select(&wfs)
if err != nil {
return nil, err
}
return wfs, err
}
func (impl *CiWorkflowRepositoryImpl) FindByName(name string) (*CiWorkflow, error) {
var ciWorkFlow *CiWorkflow
err := impl.dbConnection.Model(&ciWorkFlow).
Column("ci_workflow.*").
Where("ci_workflow.name = ?", name).
Select()
return ciWorkFlow, err
}
func (impl *CiWorkflowRepositoryImpl) FindById(id int) (*CiWorkflow, error) {
workflow := &CiWorkflow{}
err := impl.dbConnection.Model(workflow).
Column("ci_workflow.*", "CiPipeline", "CiPipeline.App", "CiPipeline.CiTemplate", "CiPipeline.CiTemplate.DockerRegistry").
Where("ci_workflow.id = ? ", id).
Select()
return workflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindRetriedWorkflowCountByReferenceId(id int) (int, error) {
retryCount := 0
query := "select count(*) from ci_workflow where ref_ci_workflow_id = ?"
_, err := impl.dbConnection.Query(&retryCount, query, id)
return retryCount, err
}
func (impl *CiWorkflowRepositoryImpl) FindCiWorkflowGitTriggersById(id int) (ciWorkflow *CiWorkflow, err error) {
workflow := &CiWorkflow{}
err = impl.dbConnection.Model(workflow).
Column("ci_workflow.git_triggers").
Where("ci_workflow.id = ? ", id).
Select()
return workflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindCiWorkflowGitTriggersByIds(ids []int) ([]*CiWorkflow, error) {
workflows := make([]*CiWorkflow, 0)
if len(ids) == 0 {
return workflows, nil
}
err := impl.dbConnection.Model(&workflows).
Column("ci_workflow.git_triggers").
Where("ci_workflow.id IN (?)", pg.In(ids)).
Select()
return workflows, err
}
func (impl *CiWorkflowRepositoryImpl) SaveWorkFlowWithTx(wf *CiWorkflow, tx *pg.Tx) error {
err := tx.Insert(wf)
return err
}
func (impl *CiWorkflowRepositoryImpl) UpdateWorkFlowWithTx(wf *CiWorkflow, tx *pg.Tx) error {
err := tx.Update(wf)
return err
}
func (impl *CiWorkflowRepositoryImpl) UpdateArtifactUploaded(id int, isUploaded workflow.ArtifactUploadedType) error {
_, err := impl.dbConnection.Model(&CiWorkflow{}).
Set("is_artifact_uploaded = ?", isUploaded).
Where("id = ?", id).
Update()
return err
}
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByCiIds(pipelineId []int) (ciWorkflow []*CiWorkflow, err error) {
err = impl.dbConnection.Model(&ciWorkflow).
Column("ci_workflow.*", "CiPipeline").
Where("ci_workflow.id IN (select MAX(id) from ci_workflow where ci_pipeline_id IN (?) GROUP BY ci_pipeline_id)", pg.In(pipelineId)).
Select()
return ciWorkflow, err
}
// FindWorkflowsByCiWorkflowIds fetches workflows by their workflow IDs (simple query)
func (impl *CiWorkflowRepositoryImpl) FindWorkflowsByCiWorkflowIds(ciWorkflowIds []int) (ciWorkflow []*CiWorkflow, err error) {
if len(ciWorkflowIds) == 0 {
return ciWorkflow, nil
}
err = impl.dbConnection.Model(&ciWorkflow).
Column("ci_workflow.*", "CiPipeline").
Where("ci_workflow.id IN (?)", pg.In(ciWorkflowIds)).
Select()
return ciWorkflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error) {
workflow := &CiWorkflow{}
err = impl.dbConnection.Model(workflow).
Column("ci_workflow.*", "CiPipeline").
Join("inner join ci_artifact cia on cia.ci_workflow_id = ci_workflow.id").
Where("cia.id = ? ", ciArtifactId).
Select()
return workflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindAllTriggeredWorkflowCountInLast24Hour() (ciWorkflowCount int, err error) {
cnt, err := impl.dbConnection.Model(&CiWorkflow{}).
ColumnExpr("DISTINCT ci_pipeline_id").
Where("started_on > ? ", time.Now().AddDate(0, 0, -1)).
Count()
if err != nil {
impl.logger.Errorw("error occurred while fetching ci workflow", "err", err)
}
return cnt, err
}
func (impl *CiWorkflowRepositoryImpl) FindAllLastTriggeredWorkflowByArtifactId(ciArtifactIds []int) (ciWorkflows []*CiWorkflow, err error) {
err = impl.dbConnection.Model(&ciWorkflows).
Column("ci_workflow.git_triggers", "ci_workflow.ci_pipeline_id", "CiPipeline", "cia.id").
Join("inner join ci_artifact cia on cia.ci_workflow_id = ci_workflow.id").
Where("cia.id in (?) ", pg.In(ciArtifactIds)).
Select()
return ciWorkflows, err
}
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowGitTriggersByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error) {
workflow := &CiWorkflow{}
err = impl.dbConnection.Model(workflow).
Column("ci_workflow.git_triggers").
Join("inner join ci_artifact cia on cia.ci_workflow_id = ci_workflow.id").
Where("cia.id = ? ", ciArtifactId).
Select()
return workflow, err
}
func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowGitTriggersByArtifactIds(ciArtifactIds []int) ([]*WorkflowWithArtifact, error) {
workflows := make([]*WorkflowWithArtifact, 0)
if len(ciArtifactIds) == 0 {
return workflows, nil
}
query := "SELECT cw.git_triggers,cw.id,cw.triggered_by,cw.ci_pipeline_id,cia.id as ci_artifact_id" +
" FROM ci_workflow cw INNER JOIN ci_artifact cia on cia.ci_workflow_id = cw.id " +
" WHERE cia.id IN (?)"
_, err := impl.dbConnection.Query(&workflows, query, pg.In(ciArtifactIds))
return workflows, err
}
func (impl *CiWorkflowRepositoryImpl) ExistsByStatus(status string) (bool, error) {
exists, err := impl.dbConnection.Model(&CiWorkflow{}).
Where("status =?", status).
Exists()
return exists, err
}
func (impl *CiWorkflowRepositoryImpl) FindBuildTypeAndStatusDataOfLast1Day() ([]*BuildTypeCount, error) {
var buildTypeCounts []*BuildTypeCount
query := "select status,ci_build_type as type, count(*) from ci_workflow where status in ('Succeeded','Failed') and started_on > ? group by (ci_build_type, status)"
_, err := impl.dbConnection.Query(&buildTypeCounts, query, time.Now().AddDate(0, 0, -1))
if err != nil {
impl.logger.Errorw("error occurred while fetching build type vs status vs count data", "err", err)
return nil, err
}
return buildTypeCounts, nil
}
func (impl *CiWorkflowRepositoryImpl) FIndCiWorkflowStatusesByAppId(appId int) ([]*CiWorkflowStatus, error) {
ciworkflowStatuses := make([]*CiWorkflowStatus, 0)
query := "SELECT cw1.ci_pipeline_id,cw1.status AS ci_status,cw1.blob_storage_enabled AS storage_configured " +
" FROM ci_workflow cw1 " +
" INNER JOIN " +
" (WITH cp AS (SELECT id, parent_ci_pipeline FROM ci_pipeline WHERE app_id = ? AND deleted=false ) " +
" SELECT cw.ci_pipeline_id, max(cw.id) " +
" FROM ci_workflow cw WHERE cw.ci_pipeline_id IN (SELECT cp.id FROM cp) OR cw.ci_pipeline_id IN (SELECT cp.parent_ci_pipeline FROM cp) " +
" GROUP BY ci_pipeline_id) cw2 " +
" ON cw1.id = cw2.max;"
_, err := impl.dbConnection.Query(&ciworkflowStatuses, query, appId) //, pg.In(ciPipelineIds))
if err != nil {
impl.logger.Errorw("error occurred while fetching build type vs status vs count data", "err", err)
return ciworkflowStatuses, err
}
return ciworkflowStatuses, err
}
// FindCiPipelineIdsByAppId gets all CI pipeline IDs for an app (simple query)
func (impl *CiWorkflowRepositoryImpl) FindCiPipelineIdsByAppId(appId int) ([]int, error) {
var ciPipelineIds []int
err := impl.dbConnection.Model((*CiPipeline)(nil)).
Column("id").
Where("app_id = ? AND deleted = false", appId).
Select(&ciPipelineIds)
return ciPipelineIds, err
}
func (impl *CiWorkflowRepositoryImpl) MigrateIsArtifactUploaded(wfId int, isArtifactUploaded bool) {
_, err := impl.dbConnection.Model((*CiWorkflow)(nil)).
Set("is_artifact_uploaded = ?", workflow.GetArtifactUploadedType(isArtifactUploaded)).
Where("id = ?", wfId).
Update()
if err != nil {
impl.logger.Errorw("error occurred while updating is_artifact_uploaded", "wfId", wfId, "err", err)
}
}
func (impl *CiWorkflowRepositoryImpl) MigrateCiArtifactLocation(wfId int, artifactLocation string) {
_, err := impl.dbConnection.Model((*CiWorkflow)(nil)).
Set("ci_artifact_location = ?", artifactLocation).
Where("id = ?", wfId).
Update()
if err != nil {
impl.logger.Errorw("error occurred while updating ci_artifact_location", "wfId", wfId, "err", err)
}
}