Merge pull request #3773 from LinuxSuRen/fix-filter-parse-err
Fix the error caused by the wrong filter parse
This commit is contained in:
@@ -58,17 +58,29 @@ func (h *ProjectPipelineHandler) GetPipeline(req *restful.Request, resp *restful
|
||||
func (h *ProjectPipelineHandler) getPipelinesByRequest(req *restful.Request) (api.ListResult, error) {
|
||||
// this is a very trick way, but don't have a better solution for now
|
||||
var (
|
||||
start int
|
||||
limit int
|
||||
namespace string
|
||||
nameFilter string
|
||||
start int
|
||||
limit int
|
||||
namespace string
|
||||
)
|
||||
|
||||
// parse query from the request
|
||||
query := req.QueryParameter("q")
|
||||
pipelineFilter, namespace := parseNameFilterFromQuery(req.QueryParameter("q"))
|
||||
|
||||
// make sure we have an appropriate value
|
||||
limit, start = params.ParsePaging(req)
|
||||
return h.devopsOperator.ListPipelineObj(namespace, pipelineFilter, func(list []*v1alpha3.Pipeline, i int, j int) bool {
|
||||
return strings.Compare(strings.ToUpper(list[i].Name), strings.ToUpper(list[j].Name)) < 0
|
||||
}, limit, start)
|
||||
}
|
||||
|
||||
func parseNameFilterFromQuery(query string) (filter devops.PipelineFilter, namespace string) {
|
||||
var (
|
||||
nameFilter string
|
||||
)
|
||||
|
||||
for _, val := range strings.Split(query, ";") {
|
||||
if strings.HasPrefix(val, "pipeline:") {
|
||||
nsAndName := strings.TrimLeft(val, "pipeline:")
|
||||
nsAndName := strings.TrimPrefix(val, "pipeline:")
|
||||
filterMeta := strings.Split(nsAndName, "/")
|
||||
if len(filterMeta) >= 2 {
|
||||
namespace = filterMeta[0]
|
||||
@@ -81,18 +93,13 @@ func (h *ProjectPipelineHandler) getPipelinesByRequest(req *restful.Request) (ap
|
||||
}
|
||||
}
|
||||
|
||||
pipelineFilter := func(pipeline *v1alpha3.Pipeline) bool {
|
||||
filter = func(pipeline *v1alpha3.Pipeline) bool {
|
||||
return strings.Contains(pipeline.Name, nameFilter)
|
||||
}
|
||||
if nameFilter == "" {
|
||||
pipelineFilter = nil
|
||||
filter = nil
|
||||
}
|
||||
|
||||
// make sure we have an appropriate value
|
||||
limit, start = params.ParsePaging(req)
|
||||
return h.devopsOperator.ListPipelineObj(namespace, pipelineFilter, func(list []*v1alpha3.Pipeline, i int, j int) bool {
|
||||
return strings.Compare(strings.ToUpper(list[i].Name), strings.ToUpper(list[j].Name)) < 0
|
||||
}, limit, start)
|
||||
return
|
||||
}
|
||||
|
||||
func (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restful.Response) {
|
||||
|
||||
61
pkg/kapis/devops/v1alpha2/devops_test.go
Normal file
61
pkg/kapis/devops/v1alpha2/devops_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package v1alpha2
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
|
||||
"kubesphere.io/kubesphere/pkg/models/devops"
|
||||
)
|
||||
|
||||
func TestParseNameFilterFromQuery(t *testing.T) {
|
||||
table := []struct {
|
||||
query string
|
||||
pipeline *v1alpha3.Pipeline
|
||||
expectFilter devops.PipelineFilter
|
||||
expectNamespace string
|
||||
message string
|
||||
}{{
|
||||
query: "type:pipeline;organization:jenkins;pipeline:serverjkq4c/*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "serverjkq4c",
|
||||
message: "query all pipelines with filter *",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:cccc/*abc*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: func(pipeline *v1alpha3.Pipeline) bool {
|
||||
return strings.Contains(pipeline.Name, "abc")
|
||||
},
|
||||
expectNamespace: "cccc",
|
||||
message: "query all pipelines with filter abc",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:pai-serverjkq4c/*",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "pai-serverjkq4c",
|
||||
message: "query all pipelines with filter *",
|
||||
}, {
|
||||
query: "type:pipeline;organization:jenkins;pipeline:defdef",
|
||||
pipeline: &v1alpha3.Pipeline{},
|
||||
expectFilter: nil,
|
||||
expectNamespace: "defdef",
|
||||
message: "query all pipelines with filter *",
|
||||
}}
|
||||
|
||||
for i, item := range table {
|
||||
filter, ns := parseNameFilterFromQuery(item.query)
|
||||
if item.expectFilter == nil {
|
||||
if filter != nil {
|
||||
t.Fatalf("invalid filter, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
} else {
|
||||
if filter == nil || filter(item.pipeline) != item.expectFilter(item.pipeline) {
|
||||
t.Fatalf("invalid filter, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
}
|
||||
if ns != item.expectNamespace {
|
||||
t.Fatalf("invalid namespace, index: %d, message: %s", i, item.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,9 @@ const (
|
||||
channelMaxCapacity = 100
|
||||
)
|
||||
|
||||
type PipelineFilter func(pipeline *v1alpha3.Pipeline) bool
|
||||
type PipelineSorter func([]*v1alpha3.Pipeline, int, int) bool
|
||||
|
||||
type DevopsOperator interface {
|
||||
CreateDevOpsProject(workspace string, project *v1alpha3.DevOpsProject) (*v1alpha3.DevOpsProject, error)
|
||||
GetDevOpsProject(workspace string, projectName string) (*v1alpha3.DevOpsProject, error)
|
||||
@@ -64,7 +67,7 @@ type DevopsOperator interface {
|
||||
GetPipelineObj(projectName string, pipelineName string) (*v1alpha3.Pipeline, error)
|
||||
DeletePipelineObj(projectName string, pipelineName string) error
|
||||
UpdatePipelineObj(projectName string, pipeline *v1alpha3.Pipeline) (*v1alpha3.Pipeline, error)
|
||||
ListPipelineObj(projectName string, filterFunc func(*v1alpha3.Pipeline) bool, sortFunc func([]*v1alpha3.Pipeline, int, int) bool, limit, offset int) (api.ListResult, error)
|
||||
ListPipelineObj(projectName string, filterFunc PipelineFilter, sortFunc PipelineSorter, limit, offset int) (api.ListResult, error)
|
||||
|
||||
CreateCredentialObj(projectName string, s *v1.Secret) (*v1.Secret, error)
|
||||
GetCredentialObj(projectName string, secretName string) (*v1.Secret, error)
|
||||
@@ -268,8 +271,8 @@ func (d devopsOperator) UpdatePipelineObj(projectName string, pipeline *v1alpha3
|
||||
return d.ksclient.DevopsV1alpha3().Pipelines(ns).Update(context.Background(), pipeline, metav1.UpdateOptions{})
|
||||
}
|
||||
|
||||
func (d devopsOperator) ListPipelineObj(projectName string, filterFunc func(pipeline *v1alpha3.Pipeline) bool,
|
||||
sortFunc func([]*v1alpha3.Pipeline, int, int) bool, limit, offset int) (api.ListResult, error) {
|
||||
func (d devopsOperator) ListPipelineObj(projectName string, filterFunc PipelineFilter,
|
||||
sortFunc PipelineSorter, limit, offset int) (api.ListResult, error) {
|
||||
projectObj, err := d.ksInformers.Devops().V1alpha3().DevOpsProjects().Lister().Get(projectName)
|
||||
if err != nil {
|
||||
return api.ListResult{}, err
|
||||
|
||||
Reference in New Issue
Block a user