diff --git a/src/server/v2.0/handler/usergroup.go b/src/server/v2.0/handler/usergroup.go index 666a405d4..f95cb4aa1 100644 --- a/src/server/v2.0/handler/usergroup.go +++ b/src/server/v2.0/handler/usergroup.go @@ -17,6 +17,7 @@ package handler import ( "context" "fmt" + "sort" "strings" "github.com/go-openapi/runtime/middleware" @@ -206,7 +207,29 @@ func (u *userGroupAPI) SearchUserGroups(ctx context.Context, params operation.Se if err != nil { return u.SendError(ctx, err) } + result := getUserGroupSearchItem(ug) + sortMostMatch(result, params.Groupname) return operation.NewSearchUserGroupsOK().WithXTotalCount(total). - WithPayload(getUserGroupSearchItem(ug)). + WithPayload(result). 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) + }) +} diff --git a/src/server/v2.0/handler/usergroup_test.go b/src/server/v2.0/handler/usergroup_test.go new file mode 100644 index 000000000..c9fc39b2c --- /dev/null +++ b/src/server/v2.0/handler/usergroup_test.go @@ -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)) + }) + } +}