Fix the error caused by the wrong filter parse
Signed-off-by: rick <1450685+LinuxSuRen@users.noreply.github.com>
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) {
|
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
|
// this is a very trick way, but don't have a better solution for now
|
||||||
var (
|
var (
|
||||||
start int
|
start int
|
||||||
limit int
|
limit int
|
||||||
namespace string
|
namespace string
|
||||||
nameFilter string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// parse query from the request
|
// 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, ";") {
|
for _, val := range strings.Split(query, ";") {
|
||||||
if strings.HasPrefix(val, "pipeline:") {
|
if strings.HasPrefix(val, "pipeline:") {
|
||||||
nsAndName := strings.TrimLeft(val, "pipeline:")
|
nsAndName := strings.TrimPrefix(val, "pipeline:")
|
||||||
filterMeta := strings.Split(nsAndName, "/")
|
filterMeta := strings.Split(nsAndName, "/")
|
||||||
if len(filterMeta) >= 2 {
|
if len(filterMeta) >= 2 {
|
||||||
namespace = filterMeta[0]
|
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)
|
return strings.Contains(pipeline.Name, nameFilter)
|
||||||
}
|
}
|
||||||
if nameFilter == "" {
|
if nameFilter == "" {
|
||||||
pipelineFilter = nil
|
filter = nil
|
||||||
}
|
}
|
||||||
|
return
|
||||||
// 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 (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restful.Response) {
|
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
|
channelMaxCapacity = 100
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PipelineFilter func(pipeline *v1alpha3.Pipeline) bool
|
||||||
|
type PipelineSorter func([]*v1alpha3.Pipeline, int, int) bool
|
||||||
|
|
||||||
type DevopsOperator interface {
|
type DevopsOperator interface {
|
||||||
CreateDevOpsProject(workspace string, project *v1alpha3.DevOpsProject) (*v1alpha3.DevOpsProject, error)
|
CreateDevOpsProject(workspace string, project *v1alpha3.DevOpsProject) (*v1alpha3.DevOpsProject, error)
|
||||||
GetDevOpsProject(workspace string, projectName string) (*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)
|
GetPipelineObj(projectName string, pipelineName string) (*v1alpha3.Pipeline, error)
|
||||||
DeletePipelineObj(projectName string, pipelineName string) error
|
DeletePipelineObj(projectName string, pipelineName string) error
|
||||||
UpdatePipelineObj(projectName string, pipeline *v1alpha3.Pipeline) (*v1alpha3.Pipeline, 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)
|
CreateCredentialObj(projectName string, s *v1.Secret) (*v1.Secret, error)
|
||||||
GetCredentialObj(projectName string, secretName string) (*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{})
|
return d.ksclient.DevopsV1alpha3().Pipelines(ns).Update(context.Background(), pipeline, metav1.UpdateOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d devopsOperator) ListPipelineObj(projectName string, filterFunc func(pipeline *v1alpha3.Pipeline) bool,
|
func (d devopsOperator) ListPipelineObj(projectName string, filterFunc PipelineFilter,
|
||||||
sortFunc func([]*v1alpha3.Pipeline, int, int) bool, limit, offset int) (api.ListResult, error) {
|
sortFunc PipelineSorter, limit, offset int) (api.ListResult, error) {
|
||||||
projectObj, err := d.ksInformers.Devops().V1alpha3().DevOpsProjects().Lister().Get(projectName)
|
projectObj, err := d.ksInformers.Devops().V1alpha3().DevOpsProjects().Lister().Get(projectName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return api.ListResult{}, err
|
return api.ListResult{}, err
|
||||||
|
|||||||
Reference in New Issue
Block a user