Fix: Sort repos on org home page with non-admin login (#6741)

This commit is contained in:
Lunny Xiao 2019-04-25 02:20:22 +08:00 committed by zeripath
parent 821184c203
commit ff03b2f606
3 changed files with 55 additions and 3 deletions

43
integrations/org_test.go Normal file
View file

@ -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"}
cases = map[string][]string{
"alphabetically": {"repo21", "repo3", "repo5"},
"reversealphabetically": {"repo5", "repo3", "repo21"},
}
)
for _, user := range users {
t.Run(user, func(t *testing.T) {
session := loginUser(t, user)
for sortBy, repos := range cases {
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()))
}
}
})
}
}

View file

@ -657,6 +657,7 @@ type AccessibleReposEnvironment interface {
Repos(page, pageSize int) ([]*Repository, error) Repos(page, pageSize int) ([]*Repository, error)
MirrorRepos() ([]*Repository, error) MirrorRepos() ([]*Repository, error)
AddKeyword(keyword string) AddKeyword(keyword string)
SetSort(SearchOrderBy)
} }
type accessibleReposEnv struct { type accessibleReposEnv struct {
@ -665,6 +666,7 @@ type accessibleReposEnv struct {
teamIDs []int64 teamIDs []int64
e Engine e Engine
keyword string keyword string
orderBy SearchOrderBy
} }
// AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org` // AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org`
@ -683,6 +685,7 @@ func (org *User) accessibleReposEnv(e Engine, userID int64) (AccessibleReposEnvi
userID: userID, userID: userID,
teamIDs: teamIDs, teamIDs: teamIDs,
e: e, e: e,
orderBy: SearchOrderByRecentUpdated,
}, nil }, nil
} }
@ -722,8 +725,8 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
Table("repository"). Table("repository").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where(env.cond()). Where(env.cond()).
GroupBy("`repository`.id,`repository`.updated_unix"). GroupBy("`repository`.id,`repository`."+strings.Fields(string(env.orderBy))[0]).
OrderBy("updated_unix DESC"). OrderBy(string(env.orderBy)).
Limit(pageSize, (page-1)*pageSize). Limit(pageSize, (page-1)*pageSize).
Cols("`repository`.id"). Cols("`repository`.id").
Find(&repoIDs) Find(&repoIDs)
@ -742,6 +745,7 @@ func (env *accessibleReposEnv) Repos(page, pageSize int) ([]*Repository, error)
return repos, env.e. return repos, env.e.
In("`repository`.id", repoIDs). In("`repository`.id", repoIDs).
OrderBy(string(env.orderBy)).
Find(&repos) 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). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
Where(env.cond()). Where(env.cond()).
GroupBy("`repository`.id, `repository`.updated_unix"). GroupBy("`repository`.id, `repository`.updated_unix").
OrderBy("updated_unix DESC"). OrderBy(string(env.orderBy)).
Cols("`repository`.id"). Cols("`repository`.id").
Find(&repoIDs) Find(&repoIDs)
} }
@ -776,3 +780,7 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*Repository, error) {
func (env *accessibleReposEnv) AddKeyword(keyword string) { func (env *accessibleReposEnv) AddKeyword(keyword string) {
env.keyword = keyword env.keyword = keyword
} }
func (env *accessibleReposEnv) SetSort(orderBy SearchOrderBy) {
env.orderBy = orderBy
}

View file

@ -505,6 +505,7 @@ func showOrgProfile(ctx *context.Context) {
ctx.ServerError("AccessibleReposEnv", err) ctx.ServerError("AccessibleReposEnv", err)
return return
} }
env.SetSort(orderBy)
if len(keyword) != 0 { if len(keyword) != 0 {
env.AddKeyword(keyword) env.AddKeyword(keyword)
} }