diff --git a/pkg/simple/client/devops/jenkins/pipeline.go b/pkg/simple/client/devops/jenkins/pipeline.go index 559f8894fabe1918f29754eb866ae83d486f0f52..de7e63c50fd0a90f1794d78a4609854567b247ae 100644 --- a/pkg/simple/client/devops/jenkins/pipeline.go +++ b/pkg/simple/client/devops/jenkins/pipeline.go @@ -122,13 +122,12 @@ func (p *Pipeline) ListPipelines() (*devops.PipelineList, error) { return nil, err } - pipelienList := devops.PipelineList{Total: count} - err = json.Unmarshal(res, &pipelienList.Items) + pipelienList, err := devops.UnmarshalPipeline(count, res) if err != nil { klog.Error(err) return nil, err } - return &pipelienList, err + return pipelienList, err } func (p *Pipeline) searchPipelineCount() (int, error) { diff --git a/pkg/simple/client/devops/pipeline.go b/pkg/simple/client/devops/pipeline.go index 98b4a0908917de9fbc79c2d9f34e9f528b77ee45..48565e80686844db481e47e6d3238f9cef737e6f 100644 --- a/pkg/simple/client/devops/pipeline.go +++ b/pkg/simple/client/devops/pipeline.go @@ -17,6 +17,7 @@ limitations under the License. package devops import ( + "encoding/json" "fmt" "io" "net/http" @@ -97,6 +98,17 @@ type Pipeline struct { TotalNumberOfPullRequests int `json:"totalNumberOfPullRequests,omitempty" description:"total number of pull requests"` } +// UnmarshalPipeline unmarshal data into the Pipeline list +func UnmarshalPipeline(total int, data []byte) (pipelineList *PipelineList, err error) { + pipelineList = &PipelineList{Total: total} + pipelineList.Items = make([]Pipeline, total) + for i, _ := range pipelineList.Items { + pipelineList.Items[i].WeatherScore = 100 + } + err = json.Unmarshal(data, &pipelineList.Items) + return +} + // GetPipeBranchRun & SearchPipelineRuns type PipelineRunList struct { Items []PipelineRun `json:"items"` diff --git a/pkg/simple/client/devops/pipeline_test.go b/pkg/simple/client/devops/pipeline_test.go index db60a4f75d9751477a6bfcfd12dacd8157c0fadb..92b9e5e8ef12ac0c6d91742667e9978d590902b0 100644 --- a/pkg/simple/client/devops/pipeline_test.go +++ b/pkg/simple/client/devops/pipeline_test.go @@ -1,6 +1,7 @@ package devops import ( + "fmt" "gotest.tools/assert" "testing" ) @@ -32,3 +33,38 @@ func TestApprovable(t *testing.T) { assert.Equal(t, input.Approvable("good"), true, "should be approvable") assert.Equal(t, input.Approvable("bad"), true, "should be approvable") } + +func TestPipelineJsonMarshall(t *testing.T) { + const name = "fakeName" + var err error + var pipelineText string + var pipelienList *PipelineList + + pipelineText = fmt.Sprintf(`[{"displayName":"%s", "weatherScore": 11}]`, name) + pipelienList, err = UnmarshalPipeline(1, []byte(pipelineText)) + assert.NilError(t, err, "pipeline json marshal should be success") + assert.Equal(t, pipelienList.Total, 1) + assert.Equal(t, len(pipelienList.Items), 1) + assert.Equal(t, pipelienList.Items[0].DisplayName, name) + assert.Equal(t, pipelienList.Items[0].WeatherScore, 11) + + // test against the default value of weatherScore, it should be 100 + pipelineText = fmt.Sprintf(`[{"displayName":"%s"}]`, name) + pipelienList, err = UnmarshalPipeline(1, []byte(pipelineText)) + assert.NilError(t, err, "pipeline json marshal should be success") + assert.Equal(t, pipelienList.Total, 1) + assert.Equal(t, len(pipelienList.Items), 1) + assert.Equal(t, pipelienList.Items[0].DisplayName, name) + assert.Equal(t, pipelienList.Items[0].WeatherScore, 100) + + // test against multiple items + pipelineText = fmt.Sprintf(`[{"displayName":"%s"}, {"displayName":"%s-1"}]`, name, name) + pipelienList, err = UnmarshalPipeline(2, []byte(pipelineText)) + assert.NilError(t, err, "pipeline json marshal should be success") + assert.Equal(t, pipelienList.Total, 2) + assert.Equal(t, len(pipelienList.Items), 2) + assert.Equal(t, pipelienList.Items[0].DisplayName, name) + assert.Equal(t, pipelienList.Items[0].WeatherScore, 100) + assert.Equal(t, pipelienList.Items[1].DisplayName, fmt.Sprintf("%s-1", name)) + assert.Equal(t, pipelienList.Items[1].WeatherScore, 100) +}