Skip to content

Commit

Permalink
fix: wrong pages number which includes private repository count.
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Feb 6, 2017
1 parent e921dcf commit ffa72dc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
36 changes: 36 additions & 0 deletions models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,39 @@
num_pulls: 0
num_closed_pulls: 0
is_mirror: true

-
id: 6
owner_id: 10
lower_name: repo6
name: repo6
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false

-
id: 7
owner_id: 10
lower_name: repo7
name: repo7
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false

-
id: 8
owner_id: 10
lower_name: repo8
name: repo8
is_private: false
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: false
18 changes: 18 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,11 +1734,29 @@ func getRepositoryCount(e Engine, u *User) (int64, error) {
return x.Count(&Repository{OwnerID: u.ID})
}

func getPublicRepositoryCount(e Engine, u *User) (int64, error) {
return x.Where("is_private = ?", false).Count(&Repository{OwnerID: u.ID})
}

func getPrivateRepositoryCount(e Engine, u *User) (int64, error) {
return x.Where("is_private = ?", true).Count(&Repository{OwnerID: u.ID})
}

// GetRepositoryCount returns the total number of repositories of user.
func GetRepositoryCount(u *User) (int64, error) {
return getRepositoryCount(x, u)
}

// GetPublicRepositoryCount returns the total number of public repositories of user.
func GetPublicRepositoryCount(u *User) (int64, error) {
return getPublicRepositoryCount(x, u)
}

// GetPrivateRepositoryCount returns the total number of private repositories of user.
func GetPrivateRepositoryCount(u *User) (int64, error) {
return getPrivateRepositoryCount(x, u)
}

// SearchRepoOptions holds the search options
type SearchRepoOptions struct {
Keyword string
Expand Down
38 changes: 36 additions & 2 deletions models/repo_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package models_test
// Copyright 2017 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 models

import (
"testing"

. "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/markdown"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
)

func TestRepo(t *testing.T) {
Expand Down Expand Up @@ -68,3 +73,32 @@ func TestRepo(t *testing.T) {
})
})
}

func TestGetRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

count, err1 := GetRepositoryCount(&User{ID: int64(10)})
privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err1)
assert.NoError(t, err2)
assert.NoError(t, err3)
assert.Equal(t, int64(3), count)
assert.Equal(t, (privateCount + publicCount), count)
}

func TestGetPublicRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
}

func TestGetPrivateRepositoryCount(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
}
17 changes: 15 additions & 2 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,27 @@ func Profile(ctx *context.Context) {

keyword := ctx.Query("q")
if len(keyword) == 0 {
var total int
repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy)
if err != nil {
ctx.Handle(500, "GetRepositories", err)
return
}
ctx.Data["Repos"] = repos
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5)
ctx.Data["Total"] = ctxUser.NumRepos

if showPrivate {
total = ctxUser.NumRepos
} else {
count, err := models.GetPublicRepositoryCount(ctxUser)
if err != nil {
ctx.Handle(500, "GetPublicRepositoryCount", err)
return
}
total = int(count)
}

ctx.Data["Page"] = paginater.New(total, setting.UI.User.RepoPagingNum, page, 5)
ctx.Data["Total"] = total
} else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
Expand Down

0 comments on commit ffa72dc

Please sign in to comment.