From 4b30fd8c13ed427ac25bc6a897204215466515f2 Mon Sep 17 00:00:00 2001 From: runzexia Date: Tue, 14 Jan 2020 14:51:00 +0800 Subject: [PATCH 1/3] fix choice param parse Signed-off-by: runzexia --- pkg/models/devops/project_pipeline.go | 42 +++++- pkg/models/devops/project_pipeline_test.go | 168 +++++++++++++++++++++ 2 files changed, 205 insertions(+), 5 deletions(-) diff --git a/pkg/models/devops/project_pipeline.go b/pkg/models/devops/project_pipeline.go index bccd1625b..cf7e1eccf 100644 --- a/pkg/models/devops/project_pipeline.go +++ b/pkg/models/devops/project_pipeline.go @@ -15,14 +15,15 @@ package devops import ( "fmt" + "strconv" + "strings" + "time" + "github.com/beevik/etree" "github.com/kubesphere/sonargo/sonar" "k8s.io/klog" "kubesphere.io/kubesphere/pkg/gojenkins" "kubesphere.io/kubesphere/pkg/simple/client" - "strconv" - "strings" - "time" ) const ( @@ -375,16 +376,47 @@ func (s *Parameters) fromEtree(properties *etree.Element) *Parameters { *s = append(*s, &Parameter{ Name: param.SelectElement("name").Text(), Description: param.SelectElement("description").Text(), - DefaultValue: param.SelectElement("name").Text(), + DefaultValue: param.SelectElement("defaultValue").Text(), Type: ParameterTypeMap["hudson.model.PasswordParameterDefinition"], }) case "hudson.model.ChoiceParameterDefinition": + /* case1 + + 1 + x + + + 1 + 2 + 3 + + + + */ + /* case2 + + 1 + x + + 1 + 2 + 3 + + + */ choiceParameter := &Parameter{ Name: param.SelectElement("name").Text(), Description: param.SelectElement("description").Text(), Type: ParameterTypeMap["hudson.model.ChoiceParameterDefinition"], } - choices := param.SelectElement("choices").SelectElement("a").SelectElements("string") + choicesSection := param.SelectElement("choices") + a := choicesSection.SelectElement("a") + var choices []*etree.Element + if a != nil { + choices = a.SelectElements("string") + } else { + choices = choicesSection.SelectElements("string") + } for _, choice := range choices { choiceParameter.DefaultValue += fmt.Sprintf("%s\n", choice.Text()) } diff --git a/pkg/models/devops/project_pipeline_test.go b/pkg/models/devops/project_pipeline_test.go index 4f572b554..f1473abdf 100644 --- a/pkg/models/devops/project_pipeline_test.go +++ b/pkg/models/devops/project_pipeline_test.go @@ -631,3 +631,171 @@ func Test_MultiBranchPipelineMultibranchTrigger(t *testing.T) { } } + +func Test_ParsePipelineConfigChoiceParameters(t *testing.T) { + + case1 := ` + + + + + + +BIOGRAPHY +PERSON +PASSWORD +TOGGLE + + + + + +false + + + + +7 +10 +-1 +-1 + + + + + +PERSON +Who should I say hello to? +Mr Jenkins +false + + +BIOGRAPHY +Enter some information about the person + +false + + +TOGGLE +Toggle this value +true + + +PASSWORD +Enter a password + +{AQAAABAAAAAQHD/HoXKklrRKtlQ5ylr1lgN1dj7im57I8SGfkLIe17s=} + + + +1 +x + + +1 +2 +3 + + + + + + + + +true + + +false +` + case2 := ` + + + + + + + +BIOGRAPHY +PERSON +PASSWORD +TOGGLE + + + + + +false + + + + +7 +10 +-1 +-1 + + + + + +PERSON +Who should I say hello to? +Mr Jenkins +false + + +BIOGRAPHY +Enter some information about the person + +false + + +TOGGLE +Toggle this value +true + + +PASSWORD +Enter a password + +{AQAAABAAAAAQHD/HoXKklrRKtlQ5ylr1lgN1dj7im57I8SGfkLIe17s=} + + + +1 +x + +1 +2 +3 + + + + + + + +true + + +false +` + + case1Pipeline, err := parsePipelineConfigXml(case1) + if err != nil { + t.Fatal(err) + } + case2Pipeline, err := parsePipelineConfigXml(case2) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(case1Pipeline, case2Pipeline) { + t.Fatalf("case1 [%+v] case2 [%+v] should equal ", case1Pipeline, case2Pipeline) + } + +} From 0531e2ce484878155d15b362ea5349ffa27e9272 Mon Sep 17 00:00:00 2001 From: runzexia Date: Wed, 15 Jan 2020 11:11:04 +0800 Subject: [PATCH 2/3] add more comments Signed-off-by: runzexia --- pkg/models/devops/project_pipeline.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/models/devops/project_pipeline.go b/pkg/models/devops/project_pipeline.go index cf7e1eccf..495aba7da 100644 --- a/pkg/models/devops/project_pipeline.go +++ b/pkg/models/devops/project_pipeline.go @@ -380,7 +380,11 @@ func (s *Parameters) fromEtree(properties *etree.Element) *Parameters { Type: ParameterTypeMap["hudson.model.PasswordParameterDefinition"], }) case "hudson.model.ChoiceParameterDefinition": - /* case1 + /* + In Jenkins, different configuration methods will lead to different serialization results. + We need to be compatible with serialization results. + + case1: Configured by KubeSphere console / Jenkins console 1 x @@ -392,8 +396,7 @@ func (s *Parameters) fromEtree(properties *etree.Element) *Parameters { - */ - /* case2 + case2: Configured by pipeline syntax, sample: parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') } 1 x From b3690795a6346755b838c413cf86ebe02373c3d9 Mon Sep 17 00:00:00 2001 From: runzexia Date: Wed, 15 Jan 2020 11:45:16 +0800 Subject: [PATCH 3/3] refmt code Signed-off-by: runzexia --- pkg/models/devops/project_pipeline.go | 42 +++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/models/devops/project_pipeline.go b/pkg/models/devops/project_pipeline.go index 495aba7da..9cf748190 100644 --- a/pkg/models/devops/project_pipeline.go +++ b/pkg/models/devops/project_pipeline.go @@ -381,31 +381,31 @@ func (s *Parameters) fromEtree(properties *etree.Element) *Parameters { }) case "hudson.model.ChoiceParameterDefinition": /* - In Jenkins, different configuration methods will lead to different serialization results. - We need to be compatible with serialization results. + In Jenkins, different configuration methods will lead to different serialization results. + We need to be compatible with serialization results. - case1: Configured by KubeSphere console / Jenkins console - - 1 - x - - + case1: Configured by KubeSphere console / Jenkins console + + 1 + x + + + 1 + 2 + 3 + + + + case2: Configured by pipeline syntax, sample: parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') } + + 1 + x + 1 2 3 - - - - case2: Configured by pipeline syntax, sample: parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') } - - 1 - x - - 1 - 2 - 3 - - + + */ choiceParameter := &Parameter{ Name: param.SelectElement("name").Text(),