-
Notifications
You must be signed in to change notification settings - Fork 475
/
PipelineStageService.go
2154 lines (2076 loc) · 90.2 KB
/
PipelineStageService.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 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 pipeline
import (
"encoding/json"
"errors"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/pkg/pipeline/bean"
"github.com/devtron-labs/devtron/pkg/pipeline/repository"
repository2 "github.com/devtron-labs/devtron/pkg/plugin/repository"
"github.com/devtron-labs/devtron/pkg/resourceQualifiers"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/pkg/variables"
repository3 "github.com/devtron-labs/devtron/pkg/variables/repository"
"github.com/go-pg/pg"
"go.uber.org/zap"
"time"
)
type PipelineStageService interface {
GetCiPipelineStageData(ciPipelineId int) (preCiStage *bean.PipelineStageDto, postCiStage *bean.PipelineStageDto, err error)
CreatePipelineStage(stageReq *bean.PipelineStageDto, stageType repository.PipelineStageType, pipelineId int, userId int32) error
UpdatePipelineStage(stageReq *bean.PipelineStageDto, stageType repository.PipelineStageType, pipelineId int, userId int32) error
DeletePipelineStage(stageReq *bean.PipelineStageDto, userId int32, tx *pg.Tx) error
BuildPrePostAndRefPluginStepsDataForWfRequest(pipelineId int, stageType string, scope resourceQualifiers.Scope) (*bean.PrePostAndRefPluginStepsResponse, error)
GetCiPipelineStageDataDeepCopy(ciPipelineId int) (preCiStage *bean.PipelineStageDto, postCiStage *bean.PipelineStageDto, err error)
GetCdPipelineStageDataDeepCopy(cdPipelineId int) (*bean.PipelineStageDto, *bean.PipelineStageDto, error)
GetCdPipelineStageDataDeepCopyForPipelineIds(cdPipelineIds []int) (map[int][]*bean.PipelineStageDto, error)
GetCdStageByCdPipelineIdAndStageType(cdPipelineId int, stageType repository.PipelineStageType) (*repository.PipelineStage, error)
// DeletePipelineStageIfReq function is used to delete corrupted pipelineStage data
// , there was a bug(https://github.com/devtron-labs/devtron/issues/3826) where we were not deleting pipeline stage entry even after deleting all the pipelineStageSteps
// , this will delete those pipelineStage entry
DeletePipelineStageIfReq(stageReq *bean.PipelineStageDto, userId int32) (error, bool)
}
func NewPipelineStageService(logger *zap.SugaredLogger,
pipelineStageRepository repository.PipelineStageRepository,
globalPluginRepository repository2.GlobalPluginRepository,
pipelineRepository pipelineConfig.PipelineRepository,
scopedVariableManager variables.ScopedVariableManager,
) *PipelineStageServiceImpl {
return &PipelineStageServiceImpl{
logger: logger,
pipelineStageRepository: pipelineStageRepository,
globalPluginRepository: globalPluginRepository,
pipelineRepository: pipelineRepository,
scopedVariableManager: scopedVariableManager,
}
}
type PipelineStageServiceImpl struct {
logger *zap.SugaredLogger
pipelineStageRepository repository.PipelineStageRepository
globalPluginRepository repository2.GlobalPluginRepository
pipelineRepository pipelineConfig.PipelineRepository
scopedVariableManager variables.ScopedVariableManager
}
func (impl *PipelineStageServiceImpl) GetCiPipelineStageDataDeepCopy(ciPipelineId int) (*bean.PipelineStageDto, *bean.PipelineStageDto, error) {
//getting all stages by ci pipeline id
ciStages, err := impl.pipelineStageRepository.GetAllCiStagesByCiPipelineId(ciPipelineId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting all ciStages by ciPipelineId", "err", err, "ciPipelineId", ciStages)
return nil, nil, err
}
var preCiStage *bean.PipelineStageDto
var postCiStage *bean.PipelineStageDto
for _, ciStage := range ciStages {
if ciStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CI {
preCiStage, err = impl.BuildPipelineStageDataDeepCopy(ciStage)
if err != nil {
impl.logger.Errorw("error in getting ci stage data", "err", err, "ciStage", ciStage)
return nil, nil, err
}
} else if ciStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CI {
postCiStage, err = impl.BuildPipelineStageDataDeepCopy(ciStage)
if err != nil {
impl.logger.Errorw("error in getting ci stage data", "err", err, "ciStage", ciStage)
return nil, nil, err
}
} else {
impl.logger.Errorw("found improper stage mapped with ciPipelineRequest", "ciPipelineId", ciPipelineId, "stage", ciStage)
}
}
return preCiStage, postCiStage, nil
}
func (impl *PipelineStageServiceImpl) GetCdStageByCdPipelineIdAndStageType(cdPipelineId int, stageType repository.PipelineStageType) (*repository.PipelineStage, error) {
return impl.pipelineStageRepository.GetCdStageByCdPipelineIdAndStageType(cdPipelineId, stageType)
}
func (impl *PipelineStageServiceImpl) GetCdPipelineStageDataDeepCopy(cdPipelineId int) (*bean.PipelineStageDto, *bean.PipelineStageDto, error) {
//getting all stages by cd pipeline id
cdStages, err := impl.pipelineStageRepository.GetAllCdStagesByCdPipelineId(cdPipelineId)
if err != nil {
impl.logger.Errorw("error in getting all cdStages by cdPipelineId", "err", err, "cdPipelineStages", cdStages)
return nil, nil, err
}
if len(cdStages) == 0 {
//no entry for cdStages in db
return nil, nil, nil
}
var preDeployStage *bean.PipelineStageDto
var postDeployStage *bean.PipelineStageDto
for _, cdStage := range cdStages {
if cdStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CD {
preDeployStage, err = impl.BuildPipelineStageDataDeepCopy(cdStage)
if err != nil {
impl.logger.Errorw("error in getting cd stage data", "err", err, "cdStage", cdStage)
return nil, nil, err
}
} else if cdStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CD {
postDeployStage, err = impl.BuildPipelineStageDataDeepCopy(cdStage)
if err != nil {
impl.logger.Errorw("error in getting cd stage data", "err", err, "cdStage", cdStage)
return nil, nil, err
}
} else {
impl.logger.Errorw("found improper stage mapped with cdPipeline", "cdPipelineId", cdPipelineId, "stage", cdStage)
}
}
if preDeployStage != nil {
preDeployStage.Name = "Pre-Deployment"
}
if postDeployStage != nil {
postDeployStage.Name = "Post-Deployment"
}
return preDeployStage, postDeployStage, nil
}
func (impl *PipelineStageServiceImpl) GetCdPipelineStageDataDeepCopyForPipelineIds(cdPipelineIds []int) (map[int][]*bean.PipelineStageDto, error) {
pipelinePrePostStageMappingResp := make(map[int][]*bean.PipelineStageDto)
pipelineStageMapping := make(map[int][]*repository.PipelineStage)
pipelineStages, err := impl.pipelineStageRepository.GetAllCdStagesByCdPipelineIds(cdPipelineIds)
if err != nil {
impl.logger.Errorw("error in getting pipelineStages from cdPipelineIds", "err", err, "cdPipelineIds", cdPipelineIds)
return pipelinePrePostStageMappingResp, err
}
if len(pipelineStages) == 0 {
return pipelinePrePostStageMappingResp, nil
}
for _, pipelineStage := range pipelineStages {
pipelineStageMapping[pipelineStage.CdPipelineId] = append(pipelineStageMapping[pipelineStage.CdPipelineId], pipelineStage)
}
for pipelineId, pipelineStages := range pipelineStageMapping {
var preDeployStage *bean.PipelineStageDto
var postDeployStage *bean.PipelineStageDto
for _, pipelineStage := range pipelineStages {
if pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CD {
preDeployStage, err = impl.BuildPipelineStageDataDeepCopy(pipelineStage)
if err != nil {
impl.logger.Errorw("error in getting cd stage data", "err", err, "cdStage", bean.CdStage)
return pipelinePrePostStageMappingResp, err
}
preDeployStage.Name = "Pre-Deployment"
} else if pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CD {
postDeployStage, err = impl.BuildPipelineStageDataDeepCopy(pipelineStage)
if err != nil {
impl.logger.Errorw("error in getting cd stage data", "err", err, "cdStage", bean.CdStage)
return pipelinePrePostStageMappingResp, err
}
postDeployStage.Name = "Post-Deployment"
} else {
impl.logger.Errorw("found improper stage mapped with cdPipeline", "cdPipelineId", pipelineId, "stage", bean.CdStage)
}
}
pipelinePrePostStageMappingResp[pipelineId] = append(pipelinePrePostStageMappingResp[pipelineId], preDeployStage)
pipelinePrePostStageMappingResp[pipelineId] = append(pipelinePrePostStageMappingResp[pipelineId], postDeployStage)
}
return pipelinePrePostStageMappingResp, nil
}
func (impl *PipelineStageServiceImpl) BuildPipelineStageDataDeepCopy(pipelineStage *repository.PipelineStage) (*bean.PipelineStageDto, error) {
stageData := &bean.PipelineStageDto{
Id: pipelineStage.Id,
Name: pipelineStage.Name,
Description: pipelineStage.Description,
Type: pipelineStage.Type,
}
if pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CD || pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CD {
pipeline, err := impl.pipelineRepository.FindById(pipelineStage.CdPipelineId)
if err != nil {
impl.logger.Errorw("error in getting pipeline from cdPipelineId", "err", err, "cdPipelineId", pipelineStage.CdPipelineId)
return nil, err
}
if pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CD {
stageData.TriggerType = pipeline.PreTriggerType
}
if pipelineStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CD {
stageData.TriggerType = pipeline.PostTriggerType
}
}
//getting all steps in this stage
steps, err := impl.pipelineStageRepository.GetAllStepsByStageId(pipelineStage.Id)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting pipeline steps by stage id", "err", err, "pipelineStage", pipelineStage)
return nil, err
}
var stepsDto []*bean.PipelineStageStepDto
for _, step := range steps {
stepDto := &bean.PipelineStageStepDto{
Id: step.Id,
Name: step.Name,
Index: step.Index,
Description: step.Description,
OutputDirectoryPath: step.OutputDirectoryPath,
StepType: step.StepType,
TriggerIfParentStageFail: step.TriggerIfParentStageFail,
}
if step.StepType == repository.PIPELINE_STEP_TYPE_INLINE {
inlineStepDetail, err := impl.BuildInlineStepDataDeepCopy(step)
if err != nil {
impl.logger.Errorw("error in getting inline step data", "err", err, "step", step)
return nil, err
}
stepDto.InlineStepDetail = inlineStepDetail
} else if step.StepType == repository.PIPELINE_STEP_TYPE_REF_PLUGIN {
refPluginStepDetail, err := impl.BuildRefPluginStepDataDeepCopy(step)
if err != nil {
impl.logger.Errorw("error in getting ref plugin step data", "err", err, "step", step)
return nil, err
}
stepDto.RefPluginStepDetail = refPluginStepDetail
}
stepsDto = append(stepsDto, stepDto)
}
stageData.Steps = stepsDto
return stageData, nil
}
func (impl *PipelineStageServiceImpl) BuildInlineStepDataDeepCopy(step *repository.PipelineStageStep) (*bean.InlineStepDetailDto, error) {
//getting script details for step
scriptDetail, err := impl.pipelineStageRepository.GetScriptDetailById(step.ScriptId)
if err != nil {
impl.logger.Errorw("error in getting script details by id", "err", err, "scriptId", step.ScriptId)
return nil, err
}
inlineStepDetail := &bean.InlineStepDetailDto{
ScriptType: scriptDetail.Type,
Script: scriptDetail.Script,
StoreScriptAt: scriptDetail.StoreScriptAt,
DockerfileExists: scriptDetail.DockerfileExists,
MountPath: scriptDetail.MountPath,
MountCodeToContainer: scriptDetail.MountCodeToContainer,
MountCodeToContainerPath: scriptDetail.MountCodeToContainerPath,
MountDirectoryFromHost: scriptDetail.MountDirectoryFromHost,
ContainerImagePath: scriptDetail.ContainerImagePath,
ImagePullSecretType: scriptDetail.ImagePullSecretType,
ImagePullSecret: scriptDetail.ImagePullSecret,
}
//getting script mapping details
scriptMappings, err := impl.pipelineStageRepository.GetScriptMappingDetailByScriptId(step.ScriptId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting script mapping by scriptId", "err", err, "scriptId", step.ScriptId)
return nil, err
}
var mountPathMap []*bean.MountPathMap
var commandArgsMap []*bean.CommandArgsMap
var portMap []*bean.PortMap
for _, scriptMapping := range scriptMappings {
if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_FILE_PATH {
mapEntry := &bean.MountPathMap{
FilePathOnDisk: scriptMapping.FilePathOnDisk,
FilePathOnContainer: scriptMapping.FilePathOnContainer,
}
mountPathMap = append(mountPathMap, mapEntry)
} else if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_DOCKER_ARG {
mapEntry := &bean.CommandArgsMap{
Command: scriptMapping.Command,
Args: scriptMapping.Args,
}
commandArgsMap = append(commandArgsMap, mapEntry)
} else if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_PORT {
mapEntry := &bean.PortMap{
PortOnLocal: scriptMapping.PortOnLocal,
PortOnContainer: scriptMapping.PortOnContainer,
}
portMap = append(portMap, mapEntry)
}
}
inlineStepDetail.MountPathMap = mountPathMap
inlineStepDetail.CommandArgsMap = commandArgsMap
inlineStepDetail.PortMap = portMap
inputVariablesDto, outputVariablesDto, conditionsDto, err := impl.BuildVariableAndConditionDataByStepIdDeepCopy(step.Id)
if err != nil {
impl.logger.Errorw("error in getting variables and conditions data by stepId", "err", err, "stepId", step.Id)
return nil, err
}
inlineStepDetail.InputVariables = inputVariablesDto
inlineStepDetail.OutputVariables = outputVariablesDto
inlineStepDetail.ConditionDetails = conditionsDto
return inlineStepDetail, nil
}
func (impl *PipelineStageServiceImpl) BuildRefPluginStepDataDeepCopy(step *repository.PipelineStageStep) (*bean.RefPluginStepDetailDto, error) {
refPluginStepDetail := &bean.RefPluginStepDetailDto{
PluginId: step.RefPluginId,
}
inputVariablesDto, outputVariablesDto, conditionsDto, err := impl.BuildVariableAndConditionDataByStepIdDeepCopy(step.Id)
if err != nil {
impl.logger.Errorw("error in getting variables and conditions data by stepId", "err", err, "stepId", step.Id)
return nil, err
}
refPluginStepDetail.InputVariables = inputVariablesDto
refPluginStepDetail.OutputVariables = outputVariablesDto
refPluginStepDetail.ConditionDetails = conditionsDto
return refPluginStepDetail, nil
}
func (impl *PipelineStageServiceImpl) BuildVariableAndConditionDataByStepIdDeepCopy(stepId int) ([]*bean.StepVariableDto, []*bean.StepVariableDto, []*bean.ConditionDetailDto, error) {
var inputVariablesDto []*bean.StepVariableDto
var outputVariablesDto []*bean.StepVariableDto
var conditionsDto []*bean.ConditionDetailDto
//getting all variables in the step
variables, err := impl.pipelineStageRepository.GetVariablesByStepId(stepId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting variables by stepId", "err", err, "stepId", stepId)
return nil, nil, nil, err
}
variableNameIdMap := make(map[int]string)
for _, variable := range variables {
variableNameIdMap[variable.Id] = variable.Name
variableDto := &bean.StepVariableDto{
Name: variable.Name,
Format: variable.Format,
Description: variable.Description,
IsExposed: variable.IsExposed,
AllowEmptyValue: variable.AllowEmptyValue,
DefaultValue: variable.DefaultValue,
Value: variable.Value,
ValueType: variable.ValueType,
PreviousStepIndex: variable.PreviousStepIndex,
ReferenceVariableName: variable.ReferenceVariableName,
ReferenceVariableStage: variable.ReferenceVariableStage,
VariableStepIndexInPlugin: variable.VariableStepIndexInPlugin,
}
if variable.VariableType == repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_INPUT {
inputVariablesDto = append(inputVariablesDto, variableDto)
} else if variable.VariableType == repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_OUTPUT {
outputVariablesDto = append(outputVariablesDto, variableDto)
}
}
conditions, err := impl.pipelineStageRepository.GetConditionsByStepId(stepId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting conditions by stepId", "err", err, "stepId", stepId)
return nil, nil, nil, err
}
for _, condition := range conditions {
conditionDto := &bean.ConditionDetailDto{
Id: condition.Id,
ConditionalOperator: condition.ConditionalOperator,
ConditionalValue: condition.ConditionalValue,
ConditionType: condition.ConditionType,
}
varName, ok := variableNameIdMap[condition.ConditionVariableId]
if ok {
conditionDto.ConditionOnVariable = varName
}
conditionsDto = append(conditionsDto, conditionDto)
}
return inputVariablesDto, outputVariablesDto, conditionsDto, nil
}
// GetCiPipelineStageData and related methods starts
func (impl *PipelineStageServiceImpl) GetCiPipelineStageData(ciPipelineId int) (*bean.PipelineStageDto, *bean.PipelineStageDto, error) {
//getting all stages by ci pipeline id
ciStages, err := impl.pipelineStageRepository.GetAllCiStagesByCiPipelineId(ciPipelineId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting all ciStages by ciPipelineId", "err", err, "ciPipelineId", ciStages)
return nil, nil, err
}
var preCiStage *bean.PipelineStageDto
var postCiStage *bean.PipelineStageDto
for _, ciStage := range ciStages {
if ciStage.Type == repository.PIPELINE_STAGE_TYPE_PRE_CI {
preCiStage, err = impl.BuildCiStageData(ciStage)
if err != nil {
impl.logger.Errorw("error in getting ci stage data", "err", err, "ciStage", ciStage)
return nil, nil, err
}
} else if ciStage.Type == repository.PIPELINE_STAGE_TYPE_POST_CI {
postCiStage, err = impl.BuildCiStageData(ciStage)
if err != nil {
impl.logger.Errorw("error in getting ci stage data", "err", err, "ciStage", ciStage)
return nil, nil, err
}
} else {
impl.logger.Errorw("found improper stage mapped with ciPipelineRequest", "ciPipelineId", ciPipelineId, "stage", ciStage)
}
}
return preCiStage, postCiStage, nil
}
func (impl *PipelineStageServiceImpl) BuildCiStageData(ciStage *repository.PipelineStage) (*bean.PipelineStageDto, error) {
stageData := &bean.PipelineStageDto{
Id: ciStage.Id,
Name: ciStage.Name,
Description: ciStage.Description,
Type: ciStage.Type,
}
//getting all steps in this stage
steps, err := impl.pipelineStageRepository.GetAllStepsByStageId(ciStage.Id)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting ci steps by stage id", "err", err, "ciStageId", ciStage.Id)
return nil, err
}
var stepsDto []*bean.PipelineStageStepDto
for _, step := range steps {
stepDto := &bean.PipelineStageStepDto{
Id: step.Id,
Name: step.Name,
Index: step.Index,
Description: step.Description,
OutputDirectoryPath: step.OutputDirectoryPath,
StepType: step.StepType,
TriggerIfParentStageFail: step.TriggerIfParentStageFail,
}
if step.StepType == repository.PIPELINE_STEP_TYPE_INLINE {
inlineStepDetail, err := impl.BuildInlineStepData(step)
if err != nil {
impl.logger.Errorw("error in getting inline step data", "err", err, "step", step)
return nil, err
}
stepDto.InlineStepDetail = inlineStepDetail
} else if step.StepType == repository.PIPELINE_STEP_TYPE_REF_PLUGIN {
refPluginStepDetail, err := impl.BuildRefPluginStepData(step)
if err != nil {
impl.logger.Errorw("error in getting ref plugin step data", "err", err, "step", step)
return nil, err
}
stepDto.RefPluginStepDetail = refPluginStepDetail
}
stepsDto = append(stepsDto, stepDto)
}
stageData.Steps = stepsDto
return stageData, nil
}
func (impl *PipelineStageServiceImpl) BuildInlineStepData(step *repository.PipelineStageStep) (*bean.InlineStepDetailDto, error) {
//getting script details for step
scriptDetail, err := impl.pipelineStageRepository.GetScriptDetailById(step.ScriptId)
if err != nil {
impl.logger.Errorw("error in getting script details by id", "err", err, "scriptId", step.ScriptId)
return nil, err
}
inlineStepDetail := &bean.InlineStepDetailDto{
ScriptType: scriptDetail.Type,
Script: scriptDetail.Script,
StoreScriptAt: scriptDetail.StoreScriptAt,
DockerfileExists: scriptDetail.DockerfileExists,
MountPath: scriptDetail.MountPath,
MountCodeToContainer: scriptDetail.MountCodeToContainer,
MountCodeToContainerPath: scriptDetail.MountCodeToContainerPath,
MountDirectoryFromHost: scriptDetail.MountDirectoryFromHost,
ContainerImagePath: scriptDetail.ContainerImagePath,
ImagePullSecretType: scriptDetail.ImagePullSecretType,
ImagePullSecret: scriptDetail.ImagePullSecret,
}
//getting script mapping details
scriptMappings, err := impl.pipelineStageRepository.GetScriptMappingDetailByScriptId(step.ScriptId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting script mapping by scriptId", "err", err, "scriptId", step.ScriptId)
return nil, err
}
var mountPathMap []*bean.MountPathMap
var commandArgsMap []*bean.CommandArgsMap
var portMap []*bean.PortMap
for _, scriptMapping := range scriptMappings {
if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_FILE_PATH {
mapEntry := &bean.MountPathMap{
FilePathOnDisk: scriptMapping.FilePathOnDisk,
FilePathOnContainer: scriptMapping.FilePathOnContainer,
}
mountPathMap = append(mountPathMap, mapEntry)
} else if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_DOCKER_ARG {
mapEntry := &bean.CommandArgsMap{
Command: scriptMapping.Command,
Args: scriptMapping.Args,
}
commandArgsMap = append(commandArgsMap, mapEntry)
} else if scriptMapping.TypeOfMapping == repository2.SCRIPT_MAPPING_TYPE_PORT {
mapEntry := &bean.PortMap{
PortOnLocal: scriptMapping.PortOnLocal,
PortOnContainer: scriptMapping.PortOnContainer,
}
portMap = append(portMap, mapEntry)
}
}
inlineStepDetail.MountPathMap = mountPathMap
inlineStepDetail.CommandArgsMap = commandArgsMap
inlineStepDetail.PortMap = portMap
inputVariablesDto, outputVariablesDto, conditionsDto, err := impl.BuildVariableAndConditionDataByStepId(step.Id)
if err != nil {
impl.logger.Errorw("error in getting variables and conditions data by stepId", "err", err, "stepId", step.Id)
return nil, err
}
inlineStepDetail.InputVariables = inputVariablesDto
inlineStepDetail.OutputVariables = outputVariablesDto
inlineStepDetail.ConditionDetails = conditionsDto
return inlineStepDetail, nil
}
func (impl *PipelineStageServiceImpl) BuildRefPluginStepData(step *repository.PipelineStageStep) (*bean.RefPluginStepDetailDto, error) {
refPluginStepDetail := &bean.RefPluginStepDetailDto{
PluginId: step.RefPluginId,
}
inputVariablesDto, outputVariablesDto, conditionsDto, err := impl.BuildVariableAndConditionDataByStepId(step.Id)
if err != nil {
impl.logger.Errorw("error in getting variables and conditions data by stepId", "err", err, "stepId", step.Id)
return nil, err
}
refPluginStepDetail.InputVariables = inputVariablesDto
refPluginStepDetail.OutputVariables = outputVariablesDto
refPluginStepDetail.ConditionDetails = conditionsDto
return refPluginStepDetail, nil
}
func (impl *PipelineStageServiceImpl) BuildVariableAndConditionDataByStepId(stepId int) ([]*bean.StepVariableDto, []*bean.StepVariableDto, []*bean.ConditionDetailDto, error) {
var inputVariablesDto []*bean.StepVariableDto
var outputVariablesDto []*bean.StepVariableDto
var conditionsDto []*bean.ConditionDetailDto
//getting all variables in the step
variables, err := impl.pipelineStageRepository.GetVariablesByStepId(stepId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting variables by stepId", "err", err, "stepId", stepId)
return nil, nil, nil, err
}
variableNameIdMap := make(map[int]string)
for _, variable := range variables {
variableNameIdMap[variable.Id] = variable.Name
variableDto := &bean.StepVariableDto{
Id: variable.Id,
Name: variable.Name,
Format: variable.Format,
Description: variable.Description,
IsExposed: variable.IsExposed,
AllowEmptyValue: variable.AllowEmptyValue,
DefaultValue: variable.DefaultValue,
Value: variable.Value,
ValueType: variable.ValueType,
PreviousStepIndex: variable.PreviousStepIndex,
ReferenceVariableName: variable.ReferenceVariableName,
ReferenceVariableStage: variable.ReferenceVariableStage,
VariableStepIndexInPlugin: variable.VariableStepIndexInPlugin,
}
if variable.VariableType == repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_INPUT {
inputVariablesDto = append(inputVariablesDto, variableDto)
} else if variable.VariableType == repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_OUTPUT {
outputVariablesDto = append(outputVariablesDto, variableDto)
}
}
conditions, err := impl.pipelineStageRepository.GetConditionsByStepId(stepId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting conditions by stepId", "err", err, "stepId", stepId)
return nil, nil, nil, err
}
for _, condition := range conditions {
conditionDto := &bean.ConditionDetailDto{
Id: condition.Id,
ConditionalOperator: condition.ConditionalOperator,
ConditionalValue: condition.ConditionalValue,
ConditionType: condition.ConditionType,
}
varName, ok := variableNameIdMap[condition.ConditionVariableId]
if ok {
conditionDto.ConditionOnVariable = varName
}
conditionsDto = append(conditionsDto, conditionDto)
}
return inputVariablesDto, outputVariablesDto, conditionsDto, nil
}
//GetCiPipelineStageData and related methods ends
// CreatePipelineStage and related methods starts
func (impl *PipelineStageServiceImpl) CreatePipelineStage(stageReq *bean.PipelineStageDto, stageType repository.PipelineStageType, pipelineId int, userId int32) error {
dbConnection := impl.pipelineRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return err
}
// Rollback tx on error.
defer tx.Rollback()
stage := &repository.PipelineStage{
Name: stageReq.Name,
Description: stageReq.Description,
Type: stageType,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
if stageType == repository.PIPELINE_STAGE_TYPE_PRE_CI || stageType == repository.PIPELINE_STAGE_TYPE_POST_CI {
stage.CiPipelineId = pipelineId
} else if stageType == repository.PIPELINE_STAGE_TYPE_PRE_CD || stageType == repository.PIPELINE_STAGE_TYPE_POST_CD {
stage.CdPipelineId = pipelineId
} else {
return errors.New("unknown stage type")
}
stage, err = impl.pipelineStageRepository.CreatePipelineStage(stage, tx)
if err != nil {
impl.logger.Errorw("error in creating entry for pipeline Stage", "err", err, "pipelineStage", stage)
return err
}
stageReq.Id = stage.Id
indexNameString := make(map[int]string)
for _, step := range stageReq.Steps {
indexNameString[step.Index] = step.Name
}
//creating stage steps and all related data
err = impl.CreateStageSteps(stageReq.Steps, stage.Id, userId, indexNameString, tx)
if err != nil {
impl.logger.Errorw("error in creating stage steps for ci stage", "err", err, "stageId", stage.Id)
return err
}
err = impl.extractAndMapScopedVariables(stageReq, userId, tx)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
impl.logger.Errorw("error in tx commit", "err", err)
return err
}
return nil
}
func (impl *PipelineStageServiceImpl) CreateStageSteps(steps []*bean.PipelineStageStepDto, stageId int, userId int32, indexNameString map[int]string, tx *pg.Tx) error {
for _, step := range steps {
//setting dependentStep detail
var dependentOnStep string
if step.Index > 1 {
//since starting index is independent of any step we will be setting dependent detail for further indexes
name, ok := indexNameString[step.Index-1]
if ok {
dependentOnStep = name
}
}
var stepId int
var inputVariables []*bean.StepVariableDto
var outputVariables []*bean.StepVariableDto
var conditionDetails []*bean.ConditionDetailDto
if step.StepType == repository.PIPELINE_STEP_TYPE_INLINE {
inlineStepDetail := step.InlineStepDetail
//creating script entry first, because step entry needs scriptId
scriptEntryId, err := impl.CreateScriptAndMappingForInlineStep(inlineStepDetail, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating script and mapping for inline step", "err", err, "inlineStepDetail", inlineStepDetail)
return err
}
inlineStep := &repository.PipelineStageStep{
PipelineStageId: stageId,
Name: step.Name,
Description: step.Description,
Index: step.Index,
StepType: step.StepType,
ScriptId: scriptEntryId,
OutputDirectoryPath: step.OutputDirectoryPath,
DependentOnStep: dependentOnStep,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
TriggerIfParentStageFail: step.TriggerIfParentStageFail,
}
inlineStep, err = impl.pipelineStageRepository.CreatePipelineStageStep(inlineStep, tx)
if err != nil {
impl.logger.Errorw("error in creating inline step", "err", err, "step", inlineStep)
return err
}
stepId = inlineStep.Id
inputVariables = inlineStepDetail.InputVariables
outputVariables = inlineStepDetail.OutputVariables
conditionDetails = inlineStepDetail.ConditionDetails
} else if step.StepType == repository.PIPELINE_STEP_TYPE_REF_PLUGIN {
refPluginStepDetail := step.RefPluginStepDetail
refPluginStep := &repository.PipelineStageStep{
PipelineStageId: stageId,
Name: step.Name,
Description: step.Description,
Index: step.Index,
StepType: step.StepType,
RefPluginId: refPluginStepDetail.PluginId,
OutputDirectoryPath: step.OutputDirectoryPath,
DependentOnStep: dependentOnStep,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
TriggerIfParentStageFail: step.TriggerIfParentStageFail,
}
refPluginStep, err := impl.pipelineStageRepository.CreatePipelineStageStep(refPluginStep, tx)
if err != nil {
impl.logger.Errorw("error in creating ref plugin step", "err", err, "step", refPluginStep)
return err
}
stepId = refPluginStep.Id
inputVariables = refPluginStepDetail.InputVariables
outputVariables = refPluginStepDetail.OutputVariables
conditionDetails = refPluginStepDetail.ConditionDetails
}
inputVariablesRepo, outputVariablesRepo, err := impl.CreateInputAndOutputVariables(stepId, inputVariables, outputVariables, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating variables for step", "err", err, "stepId", stepId, "inputVariables", inputVariables, "outputVariables", outputVariables)
return err
}
if len(conditionDetails) > 0 {
err = impl.CreateConditions(stepId, conditionDetails, inputVariablesRepo, outputVariablesRepo, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating conditions", "err", err, "conditionDetails", conditionDetails)
return err
}
}
}
return nil
}
func (impl *PipelineStageServiceImpl) CreateScriptAndMappingForInlineStep(inlineStepDetail *bean.InlineStepDetailDto, userId int32, tx *pg.Tx) (scriptId int, err error) {
scriptEntry := &repository.PluginPipelineScript{
Script: inlineStepDetail.Script,
Type: inlineStepDetail.ScriptType,
StoreScriptAt: inlineStepDetail.StoreScriptAt,
DockerfileExists: inlineStepDetail.DockerfileExists,
MountPath: inlineStepDetail.MountPath,
MountCodeToContainer: inlineStepDetail.MountCodeToContainer,
MountCodeToContainerPath: inlineStepDetail.MountCodeToContainerPath,
MountDirectoryFromHost: inlineStepDetail.MountDirectoryFromHost,
ContainerImagePath: inlineStepDetail.ContainerImagePath,
ImagePullSecretType: inlineStepDetail.ImagePullSecretType,
ImagePullSecret: inlineStepDetail.ImagePullSecret,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
scriptEntry, err = impl.pipelineStageRepository.CreatePipelineScript(scriptEntry, tx)
if err != nil {
impl.logger.Errorw("error in creating script entry for inline step", "err", err, "scriptEntry", scriptEntry)
return 0, err
}
var scriptMap []repository.ScriptPathArgPortMapping
for _, mountPath := range inlineStepDetail.MountPathMap {
repositoryEntry := repository.ScriptPathArgPortMapping{
TypeOfMapping: repository2.SCRIPT_MAPPING_TYPE_FILE_PATH,
FilePathOnDisk: mountPath.FilePathOnDisk,
FilePathOnContainer: mountPath.FilePathOnContainer,
ScriptId: scriptEntry.Id,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
scriptMap = append(scriptMap, repositoryEntry)
}
for _, commandArgMap := range inlineStepDetail.CommandArgsMap {
repositoryEntry := repository.ScriptPathArgPortMapping{
TypeOfMapping: repository2.SCRIPT_MAPPING_TYPE_DOCKER_ARG,
Command: commandArgMap.Command,
Args: commandArgMap.Args,
ScriptId: scriptEntry.Id,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
scriptMap = append(scriptMap, repositoryEntry)
}
for _, portMap := range inlineStepDetail.PortMap {
repositoryEntry := repository.ScriptPathArgPortMapping{
TypeOfMapping: repository2.SCRIPT_MAPPING_TYPE_PORT,
PortOnLocal: portMap.PortOnLocal,
PortOnContainer: portMap.PortOnContainer,
ScriptId: scriptEntry.Id,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
scriptMap = append(scriptMap, repositoryEntry)
}
if len(scriptMap) > 0 {
err = impl.pipelineStageRepository.CreateScriptMapping(scriptMap, tx)
if err != nil {
impl.logger.Errorw("error in creating script mappings", "err", err, "scriptMappings", scriptMap)
return 0, err
}
}
return scriptEntry.Id, nil
}
func (impl *PipelineStageServiceImpl) CreateInputAndOutputVariables(stepId int, inputVariables []*bean.StepVariableDto, outputVariables []*bean.StepVariableDto, userId int32, tx *pg.Tx) (inputVariablesRepo []repository.PipelineStageStepVariable, outputVariablesRepo []repository.PipelineStageStepVariable, err error) {
if len(inputVariables) > 0 {
//creating input variables
inputVariablesRepo, err = impl.CreateVariablesEntryInDb(stepId, inputVariables, repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_INPUT, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating input variables for step", "err", err, "stepId", stepId)
return nil, nil, err
}
}
if len(outputVariables) > 0 {
//creating output variables
outputVariablesRepo, err = impl.CreateVariablesEntryInDb(stepId, outputVariables, repository.PIPELINE_STAGE_STEP_VARIABLE_TYPE_OUTPUT, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating output variables for step", "err", err, "stepId", stepId)
return nil, nil, err
}
}
return inputVariablesRepo, outputVariablesRepo, nil
}
func (impl *PipelineStageServiceImpl) CreateConditions(stepId int, conditions []*bean.ConditionDetailDto, inputVariablesRepo []repository.PipelineStageStepVariable, outputVariablesRepo []repository.PipelineStageStepVariable, userId int32, tx *pg.Tx) error {
variableNameIdMap := make(map[string]int)
for _, inVar := range inputVariablesRepo {
variableNameIdMap[inVar.Name] = inVar.Id
}
for _, outVar := range outputVariablesRepo {
variableNameIdMap[outVar.Name] = outVar.Id
}
//creating conditions
_, err := impl.CreateConditionsEntryInDb(stepId, conditions, variableNameIdMap, userId, tx)
if err != nil {
impl.logger.Errorw("error in creating conditions for step", "err", err, "stepId", stepId)
return err
}
return nil
}
func (impl *PipelineStageServiceImpl) CreateVariablesEntryInDb(stepId int, variables []*bean.StepVariableDto, variableType repository.PipelineStageStepVariableType, userId int32, tx *pg.Tx) ([]repository.PipelineStageStepVariable, error) {
var variablesRepo []repository.PipelineStageStepVariable
var err error
for _, v := range variables {
inVarRepo := repository.PipelineStageStepVariable{
PipelineStageStepId: stepId,
Name: v.Name,
Format: v.Format,
Description: v.Description,
IsExposed: v.IsExposed,
AllowEmptyValue: v.AllowEmptyValue,
DefaultValue: v.DefaultValue,
Value: v.Value,
ValueType: v.ValueType,
VariableType: variableType,
PreviousStepIndex: v.PreviousStepIndex,
ReferenceVariableName: v.ReferenceVariableName,
ReferenceVariableStage: v.ReferenceVariableStage,
VariableStepIndexInPlugin: v.VariableStepIndexInPlugin,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
variablesRepo = append(variablesRepo, inVarRepo)
}
if len(variablesRepo) > 0 {
//saving variables
variablesRepo, err = impl.pipelineStageRepository.CreatePipelineStageStepVariables(variablesRepo, tx)
if err != nil {
impl.logger.Errorw("error in creating variables for pipeline stage steps", "err", err, "variables", variablesRepo)
return nil, err
}
}
return variablesRepo, nil
}
func (impl *PipelineStageServiceImpl) CreateConditionsEntryInDb(stepId int, conditions []*bean.ConditionDetailDto, variableNameIdMap map[string]int, userId int32, tx *pg.Tx) ([]repository.PipelineStageStepCondition, error) {
var conditionsRepo []repository.PipelineStageStepCondition
var err error
for _, condition := range conditions {
varId := variableNameIdMap[condition.ConditionOnVariable]
conditionRepo := repository.PipelineStageStepCondition{
PipelineStageStepId: stepId,
ConditionVariableId: varId,
ConditionType: condition.ConditionType,
ConditionalOperator: condition.ConditionalOperator,
ConditionalValue: condition.ConditionalValue,
Deleted: false,
AuditLog: sql.AuditLog{
CreatedOn: time.Now(),
CreatedBy: userId,
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
conditionsRepo = append(conditionsRepo, conditionRepo)
}
if len(conditionsRepo) > 0 {
//saving conditions
conditionsRepo, err = impl.pipelineStageRepository.CreatePipelineStageStepConditions(conditionsRepo, tx)
if err != nil {
impl.logger.Errorw("error in creating pipeline stage step conditions", "err", err, "conditionsRepo", conditionsRepo)
return nil, err
}
}
return conditionsRepo, nil
}
//CreateCiStage and related methods ends
// UpdatePipelineStage and related methods starts
func (impl *PipelineStageServiceImpl) UpdatePipelineStage(stageReq *bean.PipelineStageDto, stageType repository.PipelineStageType, pipelineId int, userId int32) error {
var stageOld *repository.PipelineStage
var err error
if stageType == repository.PIPELINE_STAGE_TYPE_PRE_CI || stageType == repository.PIPELINE_STAGE_TYPE_POST_CI {
//getting stage by stageType and ciPipelineId
stageOld, err = impl.pipelineStageRepository.GetCiStageByCiPipelineIdAndStageType(pipelineId, stageType)
} else if stageType == repository.PIPELINE_STAGE_TYPE_PRE_CD || stageType == repository.PIPELINE_STAGE_TYPE_POST_CD {
stageOld, err = impl.pipelineStageRepository.GetCdStageByCdPipelineIdAndStageType(pipelineId, stageType)
}
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting stageId by pipelineId and stageType", "err", err, "pipelineId", pipelineId, "stageType", stageType)
return err
}
//if stage is present with 0 stage steps, delete the stage and create new stage
//handle corrupt data (https://github.com/devtron-labs/devtron/issues/3826)
createNewPipStage := false
if err == nil && stageOld != nil {
stageReq.Id = stageOld.Id
err, createNewPipStage = impl.DeletePipelineStageIfReq(stageReq, userId)
if err != nil {
impl.logger.Errorw("error in deleting the corrupted pipeline stage", "err", err, "pipelineStageReq", stageReq)
return err
}
}
if err == pg.ErrNoRows || createNewPipStage {
//no stage found, creating new stage
stageReq.Id = 0
if len(stageReq.Steps) > 0 {
err = impl.CreatePipelineStage(stageReq, stageType, pipelineId, userId)
if err != nil {
impl.logger.Errorw("error in creating new pipeline stage", "err", err, "pipelineStageReq", stageReq)
return err
}
}
} else {
//stageId found, to handle as an update request
stageReq.Id = stageOld.Id
stageUpdateReq := stageOld
stageUpdateReq.Name = stageReq.Name
stageUpdateReq.Description = stageReq.Description
stageUpdateReq.UpdatedBy = userId
stageUpdateReq.UpdatedOn = time.Now()
_, err = impl.pipelineStageRepository.UpdatePipelineStage(stageUpdateReq)
if err != nil {
impl.logger.Errorw("error in updating entry for pipelineStage", "err", err, "pipelineStage", stageUpdateReq)
return err
}
// filtering(if steps/variables/conditions are updated or newly added) and performing relevant actions on update request
err = impl.FilterAndActOnStepsInPipelineStageUpdateRequest(stageReq, stageType, pipelineId, userId)
if err != nil {
impl.logger.Errorw("error in filtering and performing actions on steps in pipelineStage update request", "err", err, "stageReq", stageReq)
return err
}