validate pipeline config

Signed-off-by: runzexia <runzexia@yunify.com>
This commit is contained in:
runzexia
2020-02-05 10:55:30 +08:00
parent 88da4f7809
commit 751eff2da7
3 changed files with 143 additions and 0 deletions

View File

@@ -39,6 +39,12 @@ func CreateDevOpsProjectPipelineHandler(request *restful.Request, resp *restful.
errors.ParseSvcErr(restful.NewError(http.StatusForbidden, err.Error()), resp)
return
}
err = devops.ValidatePipelineConfig(pipeline)
if err != nil {
klog.Errorf("%+v", err)
errors.ParseSvcErr(restful.NewError(http.StatusBadRequest, err.Error()), resp)
return
}
pipelineName, err := devops.CreateProjectPipeline(projectId, pipeline)
if err != nil {
@@ -96,6 +102,12 @@ func UpdateDevOpsProjectPipelineHandler(request *restful.Request, resp *restful.
errors.ParseSvcErr(restful.NewError(http.StatusForbidden, err.Error()), resp)
return
}
err = devops.ValidatePipelineConfig(pipeline)
if err != nil {
klog.Errorf("%+v", err)
errors.ParseSvcErr(restful.NewError(http.StatusBadRequest, err.Error()), resp)
return
}
pipelineName, err := devops.UpdateProjectPipeline(projectId, pipelineId, pipeline)
if err != nil {

View File

@@ -309,3 +309,39 @@ func GetMultiBranchPipelineSonar(projectId, pipelineId, branchId string) ([]*Son
}
return sonarStatus, nil
}
func ValidatePipelineConfig(pipeline *ProjectPipeline) error {
switch pipeline.Type {
case NoScmPipelineType:
if pipeline.Pipeline == nil {
err := fmt.Errorf("request should contains Pipeline struct")
return err
}
if pipeline.Pipeline.Name == "" {
err := fmt.Errorf("pipeline name suport not be nil")
return err
}
if pipeline.Pipeline.Parameters != nil {
params := []*Parameter(*pipeline.Pipeline.Parameters)
for _, param := range params {
if param.Name == "" {
err := fmt.Errorf("param name should not be nil")
return err
}
}
}
case MultiBranchPipelineType:
if pipeline.MultiBranchPipeline == nil {
err := fmt.Errorf("request should contains MultiBranchPipeline struct")
return err
}
if pipeline.MultiBranchPipeline.Name == "" {
err := fmt.Errorf("pipeline name suport not be nil")
return err
}
default:
err := fmt.Errorf("unsupport job type")
return err
}
return nil
}

View File

@@ -799,3 +799,98 @@ pipeline { agent any parameters { string(name: 'PERSON', defaultValue: 'Mr Jenki
}
}
func Test_ValidatePipelineConfig(t *testing.T) {
rightInput := []*ProjectPipeline{
{
Type: "pipeline",
Pipeline: &NoScmPipeline{
Name: "test",
Parameters: nil,
Jenkinsfile: "echo 1",
},
}, {
Type: "multi-branch-pipeline",
MultiBranchPipeline: &MultiBranchPipeline{
Name: "multibranch-test",
Description: "xx",
},
}, {
Type: "pipeline",
Pipeline: &NoScmPipeline{
Name: "test2",
Description: "",
Discarder: nil,
Parameters: &Parameters{
&Parameter{
Name: "xx",
Type: "yy",
}, &Parameter{
Name: "tt",
DefaultValue: "",
Type: "",
Description: "ccc",
},
},
DisableConcurrent: false,
TimerTrigger: nil,
RemoteTrigger: nil,
Jenkinsfile: "",
},
},
}
errorInput := []*ProjectPipeline{
{
Type: "xx",
Pipeline: &NoScmPipeline{
Name: "",
Parameters: nil,
Jenkinsfile: "echo 1",
},
},
{
Type: "pipeline",
Pipeline: &NoScmPipeline{
Name: "",
Parameters: nil,
Jenkinsfile: "echo 1",
},
}, {
Type: "multi-branch-pipeline",
MultiBranchPipeline: &MultiBranchPipeline{
Name: "",
Description: "xx",
},
}, {
Type: "pipeline",
Pipeline: &NoScmPipeline{
Name: "test2",
Description: "",
Discarder: nil,
Parameters: &Parameters{
&Parameter{
Name: "",
Type: "yy",
}, &Parameter{
Name: "tt",
Description: "ccc",
},
},
Jenkinsfile: "",
},
},
}
for _, input := range rightInput {
err := ValidatePipelineConfig(input)
if err != nil {
t.Fatal(err)
}
}
for _, input := range errorInput {
err := ValidatePipelineConfig(input)
if err == nil {
t.Fatalf("%+v, is an error configuration", input)
}
}
}