Skip to content

Commit ff03b2f

Browse files
lunnyzeripath
authored andcommitted
Fix: Sort repos on org home page with non-admin login (#6741)
1 parent 821184c commit ff03b2f

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

integrations/org_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"strings"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestOrgRepos(t *testing.T) {
16+
prepareTestEnv(t)
17+
18+
var (
19+
users = []string{"user1", "user2"}
20+
cases = map[string][]string{
21+
"alphabetically": {"repo21", "repo3", "repo5"},
22+
"reversealphabetically": {"repo5", "repo3", "repo21"},
23+
}
24+
)
25+
26+
for _, user := range users {
27+
t.Run(user, func(t *testing.T) {
28+
session := loginUser(t, user)
29+
for sortBy, repos := range cases {
30+
req := NewRequest(t, "GET", "/user3?sort="+sortBy)
31+
resp := session.MakeRequest(t, req, http.StatusOK)
32+
33+
htmlDoc := NewHTMLParser(t, resp.Body)
34+
35+
sel := htmlDoc.doc.Find("a.name")
36+
assert.EqualValues(t, len(repos), len(sel.Nodes))
37+
for i := 0; i < len(repos); i++ {
38+
assert.EqualValues(t, repos[i], strings.TrimSpace(sel.Eq(i).Text()))
39+
}
40+
}
41+
})
42+
}
43+
}

models/org.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ type AccessibleReposEnvironment interface {
657657
Repos(page, pageSize int) ([]*Repository, error)
658658
MirrorRepos() ([]*Repository, error)
659659
AddKeyword(keyword string)
660+
SetSort(SearchOrderBy)
660661
}
661662

662663
type accessibleReposEnv struct {
@@ -665,6 +666,7 @@ type accessibleReposEnv struct {
665666
teamIDs []int64
666667
e Engine
667668
keyword string
669+
orderBy SearchOrderBy
668670
}
669671

670672
// AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org`
@@ -683,6 +685,7 @@ func (org *User) accessibleReposEnv(e Engine, userID int64) (AccessibleReposEnvi
683685
userID: userID,
684686
teamIDs: teamIDs,
685687
e: e,
688+
orderBy: SearchOrderByRecentUpdated,
686689
}, nil
687690
}
688691

@@ -722,8 +725,8 @@ func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
722725
Table("repository").
723726
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
724727
Where(env.cond()).
725-
GroupBy("`repository`.id,`repository`.updated_unix").
726-
OrderBy("updated_unix DESC").
728+
GroupBy("`repository`.id,`repository`."+strings.Fields(string(env.orderBy))[0]).
729+
OrderBy(string(env.orderBy)).
727730
Limit(pageSize, (page-1)*pageSize).
728731
Cols("`repository`.id").
729732
Find(&repoIDs)
@@ -742,6 +745,7 @@ func (env *accessibleReposEnv) Repos(page, pageSize int) ([]*Repository, error)
742745

743746
return repos, env.e.
744747
In("`repository`.id", repoIDs).
748+
OrderBy(string(env.orderBy)).
745749
Find(&repos)
746750
}
747751

@@ -752,7 +756,7 @@ func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
752756
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
753757
Where(env.cond()).
754758
GroupBy("`repository`.id, `repository`.updated_unix").
755-
OrderBy("updated_unix DESC").
759+
OrderBy(string(env.orderBy)).
756760
Cols("`repository`.id").
757761
Find(&repoIDs)
758762
}
@@ -776,3 +780,7 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*Repository, error) {
776780
func (env *accessibleReposEnv) AddKeyword(keyword string) {
777781
env.keyword = keyword
778782
}
783+
784+
func (env *accessibleReposEnv) SetSort(orderBy SearchOrderBy) {
785+
env.orderBy = orderBy
786+
}

routers/user/home.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ func showOrgProfile(ctx *context.Context) {
505505
ctx.ServerError("AccessibleReposEnv", err)
506506
return
507507
}
508+
env.SetSort(orderBy)
508509
if len(keyword) != 0 {
509510
env.AddKeyword(keyword)
510511
}

0 commit comments

Comments
 (0)