Set default value of weatherScore to 100
Having a ressonable default value can avoid confusing result. 0 should not be default value of weatherScore Signed-off-by: rick <1450685+LinuxSuRen@users.noreply.github.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user