Skip to content

Commit f87d7fd

Browse files
authored
feat:validation for pipeline Type (#4670)
* Bug(linked ci) : validation added * Bug(linked ci) : sql script added * Bug(linked ci) : condition check * Bug(linked ci) : error handling added * Bug(linked ci) : sql file name updated * Bug(linked ci) : validation added * Bug(linked ci) : message changed * story (version up) : sql file updated * story (version up) : comment added in sql file * Bug(linked ci) : validation changed * Bug(linked ci) : validation changed * Bug(same image scan detail) : Normal job flag added * Bug(linked ci) : normal job check removed * Bug(linked ci) : assets updated * Bug(linked ci) : normal job added * Bug(linked ci) : comment removed * Bug(linked ci) : sql file no. updated * code commented * bug(linked ci): sql file updated * bug(linked ci): validation added * all comments removed related to bug * comment added * logger updated * code review comment resolved * error message updated * main merge * sql file name changed * sql file no. updated
1 parent 211a1e4 commit f87d7fd

File tree

8 files changed

+64
-15
lines changed

8 files changed

+64
-15
lines changed

internal/util/ErrorUtil.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ type ApiError struct {
3737
UserDetailMessage string `json:"userDetailMessage,omitempty"`
3838
}
3939

40+
func NewApiError() *ApiError {
41+
return &ApiError{}
42+
}
43+
44+
func (e *ApiError) WithHttpStatusCode(httpStatusCode int) *ApiError {
45+
e.HttpStatusCode = httpStatusCode
46+
return e
47+
}
48+
49+
func (e *ApiError) WithCode(code string) *ApiError {
50+
e.Code = code
51+
return e
52+
}
53+
func (e *ApiError) WithInternalMessage(InternalMessage string) *ApiError {
54+
e.InternalMessage = InternalMessage
55+
return e
56+
}
57+
func (e *ApiError) WithUserMessage(userMessage interface{}) *ApiError {
58+
e.UserMessage = userMessage
59+
return e
60+
}
61+
62+
func (e *ApiError) WithUserDetailMessage(UserDetailMessage string) *ApiError {
63+
e.UserDetailMessage = UserDetailMessage
64+
return e
65+
}
66+
4067
func (e *ApiError) Error() string {
4168
return e.InternalMessage
4269
}

pkg/bean/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ type ExternalCiConfigRole struct {
191191

192192
// -------------------
193193
type PatchAction int
194-
type PipelineType string
195194

196195
const (
197196
CREATE PatchAction = iota

pkg/pipeline/BuildPipelineConfigService.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ func (impl *CiPipelineConfigServiceImpl) patchCiPipelineUpdateSource(baseCiConfi
280280
impl.logger.Errorw("error in fetching pipeline", "id", modifiedCiPipeline.Id, "err", err)
281281
return nil, err
282282
}
283-
283+
if !modifiedCiPipeline.PipelineType.IsValidPipelineType() {
284+
impl.logger.Debugw(" Invalid PipelineType", "PipelineType", modifiedCiPipeline.PipelineType)
285+
errorMessage := fmt.Sprintf(CiPipeline.PIPELINE_TYPE_IS_NOT_VALID, modifiedCiPipeline.Name)
286+
return nil, util.NewApiError().WithHttpStatusCode(http.StatusBadRequest).WithInternalMessage(errorMessage).WithUserMessage(errorMessage)
287+
}
284288
cannotUpdate := false
285289
for _, material := range pipeline.CiPipelineMaterials {
286290
if material.ScmId != "" {
@@ -1455,7 +1459,7 @@ func (impl *CiPipelineConfigServiceImpl) GetCiPipelineMin(appId int, envIds []in
14551459
var ciPipelineResp []*bean.CiPipelineMin
14561460
for _, pipeline := range pipelines {
14571461
parentCiPipeline := pipelineConfig.CiPipeline{}
1458-
pipelineType := CiPipeline.NORMAL
1462+
pipelineType := CiPipeline.CI_BUILD
14591463

14601464
if pipelineParentCiMap[pipeline.Id] != nil {
14611465
parentCiPipeline = *pipelineParentCiMap[pipeline.Id]

pkg/pipeline/CiCdPipelineOrchestrator.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
attributesBean "github.com/devtron-labs/devtron/pkg/attributes/bean"
3030
"golang.org/x/exp/slices"
31+
"net/http"
3132
"path"
3233
"regexp"
3334
"strconv"
@@ -816,7 +817,6 @@ func (impl CiCdPipelineOrchestratorImpl) CreateCiConf(createRequest *bean.CiConf
816817
}
817818
// Rollback tx on error.
818819
defer tx.Rollback()
819-
820820
ciPipelineObject := &pipelineConfig.CiPipeline{
821821
AppId: createRequest.AppId,
822822
IsManual: ciPipeline.IsManual,
@@ -2087,7 +2087,13 @@ func (impl CiCdPipelineOrchestratorImpl) CreateEcrRepo(dockerRepository, AWSRegi
20872087
}
20882088

20892089
func (impl CiCdPipelineOrchestratorImpl) AddPipelineToTemplate(createRequest *bean.CiConfigRequest, isSwitchCiPipelineRequest bool) (resp *bean.CiConfigRequest, err error) {
2090-
2090+
for _, ciPipeline := range createRequest.CiPipelines {
2091+
if !ciPipeline.PipelineType.IsValidPipelineType() {
2092+
impl.logger.Debugw(" Invalid PipelineType", "ciPipeline.PipelineType", ciPipeline.PipelineType)
2093+
errorMessage := fmt.Sprintf(CiPipeline.PIPELINE_TYPE_IS_NOT_VALID, ciPipeline.PipelineType)
2094+
return nil, util.NewApiError().WithHttpStatusCode(http.StatusBadRequest).WithInternalMessage(errorMessage).WithUserMessage(errorMessage)
2095+
}
2096+
}
20912097
if createRequest.AppWorkflowId == 0 {
20922098
// create workflow
20932099
wf := &appWorkflow.AppWorkflow{

pkg/pipeline/adapter/adapter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,9 @@ func IsLinkedCD(ci pipelineConfig.CiPipeline) bool {
174174
}
175175

176176
// IsLinkedCI will return if the pipelineConfig.CiPipeline is a Linked CI
177-
// Currently there are inconsistent values present in PipelineType ("CI_EXTERNAL", "LINKED") 207_ci_external.up
178-
// TODO migrate the deprecated values and maintain a consistent PipelineType
179177
func IsLinkedCI(ci pipelineConfig.CiPipeline) bool {
180178
return ci.ParentCiPipeline != 0 &&
181-
(ci.PipelineType == string(CiPipeline.CI_EXTERNAL) || ci.PipelineType == string(CiPipeline.LINKED))
179+
ci.PipelineType == string(CiPipeline.LINKED)
182180
}
183181

184182
// IsCIJob will return if the pipelineConfig.CiPipeline is a CI JOB

pkg/pipeline/bean/CiPipeline/CiBuildConfig.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ const Main = "main"
1212
const UniquePlaceHolderForAppName = "$etron"
1313

1414
const PIPELINE_NAME_ALREADY_EXISTS_ERROR = "pipeline name already exist"
15+
const PIPELINE_TYPE_IS_NOT_VALID = "PipelineType is not valid for pipeline %s"
1516

1617
type PipelineType string
1718

1819
const (
19-
NORMAL PipelineType = "NORMAL"
20-
LINKED PipelineType = "LINKED"
21-
// CI_EXTERNAL field is been sent from the dashboard in CreateLinkedCI request and directly gets saved to Database without any validations
22-
CI_EXTERNAL PipelineType = "CI_EXTERNAL" // Deprecated Enum: TODO fix the PipelineTypes in code and database
23-
EXTERNAL PipelineType = "EXTERNAL"
24-
CI_JOB PipelineType = "CI_JOB"
25-
LINKED_CD PipelineType = "LINKED_CD"
20+
CI_BUILD PipelineType = "CI_BUILD"
21+
LINKED PipelineType = "LINKED"
22+
EXTERNAL PipelineType = "EXTERNAL"
23+
CI_JOB PipelineType = "CI_JOB"
24+
LINKED_CD PipelineType = "LINKED_CD"
2625
)
2726

2827
type CiBuildConfigBean struct {
@@ -58,3 +57,12 @@ type BuildPackConfig struct {
5857
Args map[string]string `json:"args"`
5958
ProjectPath string `json:"projectPath,omitempty"`
6059
}
60+
61+
func (pType PipelineType) IsValidPipelineType() bool {
62+
switch pType {
63+
case CI_BUILD, LINKED, EXTERNAL, CI_JOB, LINKED_CD:
64+
return true
65+
default:
66+
return false
67+
}
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---- Not Required

scripts/sql/246_pipeline_type.up.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
UPDATE "public"."ci_pipeline" SET ci_pipeline_type='LINKED' WHERE ci_pipeline_type='CI_EXTERNAL';
2+
3+
UPDATE "public"."ci_pipeline"
4+
SET ci_pipeline_type = 'CI_BUILD'
5+
FROM app
6+
WHERE ci_pipeline.app_id = app.id AND ci_pipeline.ci_pipeline_type IS NULL AND app.app_type in( 0,2);

0 commit comments

Comments
 (0)