mirror of
https://github.com/goharbor/harbor
synced 2025-04-22 17:59:30 +00:00
Sort group search result by most match order (#17708)
fixes #17535 Signed-off-by: stonezdj <stonezdj@gmail.com> Signed-off-by: stonezdj <stonezdj@gmail.com>
This commit is contained in:
parent
e86e9aa529
commit
ea27197a82
@ -17,6 +17,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-openapi/runtime/middleware"
|
"github.com/go-openapi/runtime/middleware"
|
||||||
@ -206,7 +207,29 @@ func (u *userGroupAPI) SearchUserGroups(ctx context.Context, params operation.Se
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return u.SendError(ctx, err)
|
return u.SendError(ctx, err)
|
||||||
}
|
}
|
||||||
|
result := getUserGroupSearchItem(ug)
|
||||||
|
sortMostMatch(result, params.Groupname)
|
||||||
return operation.NewSearchUserGroupsOK().WithXTotalCount(total).
|
return operation.NewSearchUserGroupsOK().WithXTotalCount(total).
|
||||||
WithPayload(getUserGroupSearchItem(ug)).
|
WithPayload(result).
|
||||||
WithLink(u.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String())
|
WithLink(u.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sortMostMatch given a matchWord, sort the input by the most match,
|
||||||
|
// for example, search with "user", input is {"harbor_user", "user", "users, "admin_user"}
|
||||||
|
// it returns with this order {"user", "users", "admin_user", "harbor_user"}
|
||||||
|
func sortMostMatch(input []*models.UserGroupSearchItem, matchWord string) {
|
||||||
|
sort.Slice(input, func(i, j int) bool {
|
||||||
|
// exact match always first
|
||||||
|
if input[i].GroupName == matchWord {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if input[j].GroupName == matchWord {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// sort by length, then sort by alphabet
|
||||||
|
if len(input[i].GroupName) == len(input[j].GroupName) {
|
||||||
|
return input[i].GroupName < input[j].GroupName
|
||||||
|
}
|
||||||
|
return len(input[i].GroupName) < len(input[j].GroupName)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
56
src/server/v2.0/handler/usergroup_test.go
Normal file
56
src/server/v2.0/handler/usergroup_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// Copyright Project Harbor Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goharbor/harbor/src/server/v2.0/models"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_sortMostMatch(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
input []*models.UserGroupSearchItem
|
||||||
|
matchWord string
|
||||||
|
expected []*models.UserGroupSearchItem
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
}{
|
||||||
|
{"normal", args{[]*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "user"}, {GroupName: "harbor_user"}, {GroupName: "admin_user"}, {GroupName: "users"},
|
||||||
|
}, "user", []*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "user"}, {GroupName: "users"}, {GroupName: "admin_user"}, {GroupName: "harbor_user"},
|
||||||
|
}}},
|
||||||
|
{"duplicate_item", args{[]*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "user"}, {GroupName: "user"}, {GroupName: "harbor_user"}, {GroupName: "admin_user"}, {GroupName: "users"},
|
||||||
|
}, "user", []*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "user"}, {GroupName: "user"}, {GroupName: "users"}, {GroupName: "admin_user"}, {GroupName: "harbor_user"},
|
||||||
|
}}},
|
||||||
|
{"miss_exact_match", args{[]*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "harbor_user"}, {GroupName: "admin_user"}, {GroupName: "users"},
|
||||||
|
}, "user", []*models.UserGroupSearchItem{
|
||||||
|
{GroupName: "users"}, {GroupName: "admin_user"}, {GroupName: "harbor_user"},
|
||||||
|
}}},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
sortMostMatch(tt.args.input, tt.args.matchWord)
|
||||||
|
assert.True(t, reflect.DeepEqual(tt.args.input, tt.args.expected))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user