From a6daf03534600ddb76eb2c16d23e6435a7bded22 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 25 Apr 2019 00:27:08 +0800 Subject: [PATCH 1/4] fix bug when sort repos on org home page login with non-admin --- integrations/org_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ models/org.go | 12 +++++++++-- routers/user/home.go | 1 + 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 integrations/org_test.go diff --git a/integrations/org_test.go b/integrations/org_test.go new file mode 100644 index 000000000000..ccce04f3078b --- /dev/null +++ b/integrations/org_test.go @@ -0,0 +1,43 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestOrgRepos(t *testing.T) { + prepareTestEnv(t) + + var ( + users = []string{"user1", "user2"} + kases = map[string][]string{ + "alphabetically": []string{"repo21", "repo3", "repo5"}, + "reversealphabetically": []string{"repo5", "repo3", "repo21"}, + } + ) + + for _, user := range users { + t.Run(user, func(t *testing.T) { + session := loginUser(t, user) + for sortBy, repos := range kases { + req := NewRequest(t, "GET", "/user3?sort="+sortBy) + resp := session.MakeRequest(t, req, http.StatusOK) + + htmlDoc := NewHTMLParser(t, resp.Body) + + sel := htmlDoc.doc.Find("a.name") + assert.EqualValues(t, len(repos), len(sel.Nodes)) + for i := 0; i < len(repos); i++ { + assert.EqualValues(t, repos[i], strings.TrimSpace(sel.Eq(i).Text())) + } + } + }) + } +} diff --git a/models/org.go b/models/org.go index 5235dad7a7c9..3573d8816d39 100644 --- a/models/org.go +++ b/models/org.go @@ -657,6 +657,7 @@ type AccessibleReposEnvironment interface { Repos(page, pageSize int) ([]*Repository, error) MirrorRepos() ([]*Repository, error) AddKeyword(keyword string) + SetSort(SearchOrderBy) } type accessibleReposEnv struct { @@ -665,6 +666,7 @@ type accessibleReposEnv struct { teamIDs []int64 e Engine keyword string + orderBy SearchOrderBy } // AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org` @@ -683,6 +685,7 @@ func (org *User) accessibleReposEnv(e Engine, userID int64) (AccessibleReposEnvi userID: userID, teamIDs: teamIDs, e: e, + orderBy: SearchOrderByRecentUpdated, }, nil } @@ -723,7 +726,7 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) { Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Where(env.cond()). GroupBy("`repository`.id,`repository`.updated_unix"). - OrderBy("updated_unix DESC"). + OrderBy(string(env.orderBy)). Limit(pageSize, (page-1)*pageSize). Cols("`repository`.id"). Find(&repoIDs) @@ -742,6 +745,7 @@ func (env *accessibleReposEnv) Repos(page, pageSize int) ([]*Repository, error) return repos, env.e. In("`repository`.id", repoIDs). + OrderBy(string(env.orderBy)). Find(&repos) } @@ -752,7 +756,7 @@ func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) { Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true). Where(env.cond()). GroupBy("`repository`.id, `repository`.updated_unix"). - OrderBy("updated_unix DESC"). + OrderBy(string(env.orderBy)). Cols("`repository`.id"). Find(&repoIDs) } @@ -776,3 +780,7 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*Repository, error) { func (env *accessibleReposEnv) AddKeyword(keyword string) { env.keyword = keyword } + +func (env *accessibleReposEnv) SetSort(orderBy SearchOrderBy) { + env.orderBy = orderBy +} diff --git a/routers/user/home.go b/routers/user/home.go index 2293461f08da..b53a47db941a 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -505,6 +505,7 @@ func showOrgProfile(ctx *context.Context) { ctx.ServerError("AccessibleReposEnv", err) return } + env.SetSort(orderBy) if len(keyword) != 0 { env.AddKeyword(keyword) } From a5763d351223f7a2ed2499f4a74a9f68eee1d1e7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 25 Apr 2019 00:36:35 +0800 Subject: [PATCH 2/4] fix fmt --- integrations/org_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/org_test.go b/integrations/org_test.go index ccce04f3078b..87074471f195 100644 --- a/integrations/org_test.go +++ b/integrations/org_test.go @@ -18,8 +18,8 @@ func TestOrgRepos(t *testing.T) { var ( users = []string{"user1", "user2"} kases = map[string][]string{ - "alphabetically": []string{"repo21", "repo3", "repo5"}, - "reversealphabetically": []string{"repo5", "repo3", "repo21"}, + "alphabetically": {"repo21", "repo3", "repo5"}, + "reversealphabetically": {"repo5", "repo3", "repo21"}, } ) From 4be7ed95d838381c32e1cf803ae32cb7f3eb8a49 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 25 Apr 2019 00:46:07 +0800 Subject: [PATCH 3/4] fix typo --- integrations/org_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/org_test.go b/integrations/org_test.go index 87074471f195..b17d229d67d6 100644 --- a/integrations/org_test.go +++ b/integrations/org_test.go @@ -17,7 +17,7 @@ func TestOrgRepos(t *testing.T) { var ( users = []string{"user1", "user2"} - kases = map[string][]string{ + cases = map[string][]string{ "alphabetically": {"repo21", "repo3", "repo5"}, "reversealphabetically": {"repo5", "repo3", "repo21"}, } @@ -26,7 +26,7 @@ func TestOrgRepos(t *testing.T) { for _, user := range users { t.Run(user, func(t *testing.T) { session := loginUser(t, user) - for sortBy, repos := range kases { + for sortBy, repos := range cases { req := NewRequest(t, "GET", "/user3?sort="+sortBy) resp := session.MakeRequest(t, req, http.StatusOK) From 82387d1677ec76ee38e6fc898d08ed02cf9327a3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 25 Apr 2019 01:20:11 +0800 Subject: [PATCH 4/4] fix tests --- models/org.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/org.go b/models/org.go index 3573d8816d39..149d6f7aa7e4 100644 --- a/models/org.go +++ b/models/org.go @@ -725,7 +725,7 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) { Table("repository"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Where(env.cond()). - GroupBy("`repository`.id,`repository`.updated_unix"). + GroupBy("`repository`.id,`repository`."+strings.Fields(string(env.orderBy))[0]). OrderBy(string(env.orderBy)). Limit(pageSize, (page-1)*pageSize). Cols("`repository`.id").