Skip to content

Commit

Permalink
Fix the problem of rendering choice parameter incorrectly (#371)
Browse files Browse the repository at this point in the history
* Bump Jenkins client

Signed-off-by: John Niang <[email protected]>

* Simplify type of parametere definition

Signed-off-by: John Niang <[email protected]>

* Bump jenkins-zh/jenins-client to v0.0.6

Signed-off-by: John Niang <[email protected]>

* Reuse ParameterTypeMap

Signed-off-by: John Niang <[email protected]>
  • Loading branch information
JohnNiang authored Nov 24, 2021
1 parent 93020c2 commit 9514afc
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
15 changes: 14 additions & 1 deletion controllers/jenkins/pipeline/metadata_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package pipeline

import (
"github.com/jenkins-zh/jenkins-client/pkg/job"
"kubesphere.io/devops/pkg/client/devops/jenkins"
"kubesphere.io/devops/pkg/models/pipeline"
)

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,
Expand All @@ -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
Expand Down
138 changes: 138 additions & 0 deletions controllers/jenkins/pipeline/metadata_converter_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions pkg/client/devops/jenkins/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9514afc

Please sign in to comment.