Fix the Pipeline name filter issues
Signed-off-by: rick <rick@jenkins-zh.cn>
This commit is contained in:
@@ -31,18 +31,44 @@ const (
|
||||
ReverseParam = "reverse"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultLimit = 10
|
||||
DefaultPage = 1
|
||||
)
|
||||
|
||||
// ParsePaging parse the paging parameters from the request, then returns the limit and offset
|
||||
// supported format: ?limit=1&page=1
|
||||
// supported format: ?paging=limit=100,page=1
|
||||
func ParsePaging(req *restful.Request) (limit, offset int) {
|
||||
paging := req.QueryParameter(PagingParam)
|
||||
limit = 10
|
||||
offset = 0
|
||||
if groups := regexp.MustCompile(`^limit=(-?\d+),page=(\d+)$`).FindStringSubmatch(paging); len(groups) == 3 {
|
||||
limit, _ = strconv.Atoi(groups[1])
|
||||
page, _ := strconv.Atoi(groups[2])
|
||||
offset = (page - 1) * limit
|
||||
page := DefaultPage
|
||||
if paging != "" {
|
||||
if groups := regexp.MustCompile(`^limit=(-?\d+),page=(\d+)$`).FindStringSubmatch(paging); len(groups) == 3 {
|
||||
limit = AtoiOrDefault(groups[1], DefaultLimit)
|
||||
page = AtoiOrDefault(groups[2], DefaultPage)
|
||||
}
|
||||
} else {
|
||||
// try to parse from format ?limit=10&page=1
|
||||
limit = AtoiOrDefault(req.QueryParameter("limit"), DefaultLimit)
|
||||
page = AtoiOrDefault(req.QueryParameter("page"), DefaultPage)
|
||||
}
|
||||
offset = (page - 1) * limit
|
||||
|
||||
// use the explict offset
|
||||
if start := req.QueryParameter("start"); start != "" {
|
||||
offset = AtoiOrDefault(start, offset)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AtoiOrDefault(str string, defVal int) int {
|
||||
if result, err := strconv.Atoi(str); err == nil {
|
||||
return result
|
||||
}
|
||||
return defVal
|
||||
}
|
||||
|
||||
var (
|
||||
invalidKeyRegex = regexp.MustCompile(`[\s(){}\[\]]`)
|
||||
)
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package params
|
||||
|
||||
import (
|
||||
"gotest.tools/assert"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
@@ -171,3 +172,104 @@ func Test_parseConditions(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePaging(t *testing.T) {
|
||||
type testData struct {
|
||||
req *restful.Request
|
||||
limit int
|
||||
offset int
|
||||
}
|
||||
|
||||
table := []testData{{
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "",
|
||||
}}},
|
||||
limit: 10, offset: 0,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "paging=limit=11,page=1",
|
||||
}}},
|
||||
limit: 11, offset: 0,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "paging=limit=10,page=2",
|
||||
}}},
|
||||
limit: 10, offset: 10,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "paging=limit=a,page=2",
|
||||
}}},
|
||||
limit: 10, offset: 0,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "paging=limit=10,page=a",
|
||||
}}},
|
||||
limit: 10, offset: 0,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "paging=limit=a,page=a",
|
||||
}}},
|
||||
limit: 10, offset: 0,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "limit=10&page=2",
|
||||
}}},
|
||||
limit: 10, offset: 10,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "page=2",
|
||||
}}},
|
||||
limit: 10, offset: 10,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "page=3",
|
||||
}}},
|
||||
limit: 10, offset: 20,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "start=10&limit10",
|
||||
}}},
|
||||
limit: 10, offset: 10,
|
||||
}, {
|
||||
req: &restful.Request{Request: &http.Request{URL: &url.URL{
|
||||
RawQuery: "page=3&start=10&limit10",
|
||||
}}},
|
||||
limit: 10, offset: 10,
|
||||
}}
|
||||
|
||||
for index, item := range table {
|
||||
limit, offset := ParsePaging(item.req)
|
||||
assert.Equal(t, item.limit, limit, "index: [%d], wrong limit", index)
|
||||
assert.Equal(t, item.offset, offset, "index: [%d], wrong offset", index)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAtoiOrDefault(t *testing.T) {
|
||||
type testData struct {
|
||||
msg string
|
||||
str string
|
||||
def int
|
||||
expected int
|
||||
}
|
||||
table := []testData{{
|
||||
msg: "non-numerical",
|
||||
str: "a",
|
||||
def: 1,
|
||||
expected: 1,
|
||||
}, {
|
||||
msg: "non-numerical, empty string",
|
||||
str: "",
|
||||
def: 1,
|
||||
expected: 1,
|
||||
}, {
|
||||
msg: "numerical",
|
||||
str: "2",
|
||||
def: 1,
|
||||
expected: 2,
|
||||
}}
|
||||
|
||||
for index, item := range table {
|
||||
result := AtoiOrDefault(item.str, item.def)
|
||||
assert.Equal(t, item.expected, result, "test case[%d]: '%s' is wrong", index, item.msg)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user