diff --git a/controllers/jenkins/pipeline/metadata_converter.go b/controllers/jenkins/pipeline/metadata_converter.go index 7881aa16..293f75a5 100644 --- a/controllers/jenkins/pipeline/metadata_converter.go +++ b/controllers/jenkins/pipeline/metadata_converter.go @@ -2,6 +2,7 @@ package pipeline import ( "github.com/jenkins-zh/jenkins-client/pkg/job" + "kubesphere.io/devops/pkg/client/devops/jenkins" "kubesphere.io/devops/pkg/models/pipeline" ) @@ -9,7 +10,7 @@ func convertPipeline(jobPipeline *job.Pipeline) *pipeline.Metadata { return &pipeline.Metadata{ WeatherScore: jobPipeline.WeatherScore, EstimatedDurationInMillis: jobPipeline.EstimatedDurationInMillis, - Parameters: jobPipeline.Parameters, + Parameters: convertParameterDefinitions(jobPipeline.Parameters), Name: jobPipeline.Name, Disabled: jobPipeline.Disabled, NumberOfPipelines: jobPipeline.NumberOfPipelines, @@ -27,6 +28,18 @@ func convertPipeline(jobPipeline *job.Pipeline) *pipeline.Metadata { } } +func convertParameterDefinitions(paramDefs []job.ParameterDefinition) []job.ParameterDefinition { + newParamDefs := []job.ParameterDefinition{} + for _, paramDef := range paramDefs { + // copy the parameter definition + if simpleType, ok := jenkins.ParameterTypeMap["hudson.model."+paramDef.Type]; ok { + paramDef.Type = simpleType + } + newParamDefs = append(newParamDefs, paramDef) + } + return newParamDefs +} + func convertLatestRun(jobLatestRun *job.PipelineRunSummary) *pipeline.LatestRun { if jobLatestRun == nil { return nil diff --git a/controllers/jenkins/pipeline/metadata_converter_test.go b/controllers/jenkins/pipeline/metadata_converter_test.go new file mode 100644 index 00000000..0a9e4bc1 --- /dev/null +++ b/controllers/jenkins/pipeline/metadata_converter_test.go @@ -0,0 +1,138 @@ +package pipeline + +import ( + "reflect" + "testing" + + "github.com/jenkins-zh/jenkins-client/pkg/job" +) + +func Test_convertParameterDefinitions(t *testing.T) { + type args struct { + paramDefs []job.ParameterDefinition + } + tests := []struct { + name string + args args + want []job.ParameterDefinition + }{{ + name: "Convert nil parameter definitions", + args: args{}, + want: []job.ParameterDefinition{}, + }, { + name: "Convert empty parameter definitions", + args: args{ + paramDefs: []job.ParameterDefinition{}, + }, + want: []job.ParameterDefinition{}, + }, { + name: "Convert string parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "string-name", + Type: "StringParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "string-name", + Type: "string", + }}, + }, { + name: "Convert choice parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "choice-name", + Type: "ChoiceParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "choice-name", + Type: "choice", + }}, + }, { + name: "Convert text parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "text-name", + Type: "TextParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "text-name", + Type: "text", + }}, + }, { + name: "Convert boolean parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "boolean-name", + Type: "BooleanParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "boolean-name", + Type: "boolean", + }}, + }, { + name: "Convert file parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "file-name", + Type: "FileParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "file-name", + Type: "file", + }}, + }, { + name: "Convert password parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "password-name", + Type: "PasswordParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "password-name", + Type: "password", + }}, + }, { + name: "Convert multi parameter definitions", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "password-name", + Type: "PasswordParameterDefinition", + }, { + Name: "file-name", + Type: "FileParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "password-name", + Type: "password", + }, { + Name: "file-name", + Type: "file", + }}, + }, { + name: "Convert invalid parameter definition", + args: args{ + paramDefs: []job.ParameterDefinition{{ + Name: "invalid-name", + Type: "InvalidParameterDefinition", + }}, + }, + want: []job.ParameterDefinition{{ + Name: "invalid-name", + Type: "InvalidParameterDefinition", + }}, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := convertParameterDefinitions(tt.args.paramDefs); !reflect.DeepEqual(got, tt.want) { + t.Errorf("convertParameterDefinitions() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/go.mod b/go.mod index 3c491d4a..a5f2361d 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/golang/mock v1.6.0 github.com/google/go-cmp v0.5.5 github.com/google/go-querystring v1.1.0 // indirect - github.com/jenkins-zh/jenkins-client v0.0.5 + github.com/jenkins-zh/jenkins-client v0.0.6 github.com/kubesphere/sonargo v0.0.2 github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.15.0 diff --git a/go.sum b/go.sum index 657163cc..45c0f0ac 100644 --- a/go.sum +++ b/go.sum @@ -317,8 +317,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jenkins-zh/jenkins-cli v0.0.32/go.mod h1:uE1mH9PNITrg0sugv6HXuM/CSddg0zxXoYu3w57I3JY= -github.com/jenkins-zh/jenkins-client v0.0.5 h1:oKsVlcqrq7mSg/qk0gG16Ev7g23ego+X0VDiQNZrbRY= -github.com/jenkins-zh/jenkins-client v0.0.5/go.mod h1:ICBk7OOoTafVP//f/VfKZ34c0ff8vJwVnOsF9btiMYU= +github.com/jenkins-zh/jenkins-client v0.0.6 h1:Rs10HIXBP6evnmW+zeASJye/VSwrtT1bgXj+IC+oXXk= +github.com/jenkins-zh/jenkins-client v0.0.6/go.mod h1:ICBk7OOoTafVP//f/VfKZ34c0ff8vJwVnOsF9btiMYU= github.com/jenkins-zh/jenkins-formulas v0.0.5/go.mod h1:zS8fm8u5L6FcjZM0QznXsLV9T2UtSVK+hT6Sm76iUZ4= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= diff --git a/pkg/client/devops/jenkins/constants.go b/pkg/client/devops/jenkins/constants.go index 99f95904..e862c829 100644 --- a/pkg/client/devops/jenkins/constants.go +++ b/pkg/client/devops/jenkins/constants.go @@ -35,6 +35,7 @@ const ( PROJECT_ROLE = "projectRoles" ) +// ParameterTypeMap aims for simplifying representation of parameter definition type. var ParameterTypeMap = map[string]string{ "hudson.model.StringParameterDefinition": "string", "hudson.model.ChoiceParameterDefinition": "choice",