From 71d35dae8cd33760b9be266a38d4f76b00ceb373 Mon Sep 17 00:00:00 2001
From: Bo-Yi Wu <appleboy.tw@gmail.com>
Date: Mon, 6 Feb 2017 23:18:36 +0800
Subject: [PATCH] fix: wrong pages number which includes private repository
 count. (#844)

---
 models/fixtures/repository.yml | 36 ++++++++++++++++++++++++++++++++
 models/repo.go                 | 18 ++++++++++++++++
 models/repo_test.go            | 38 ++++++++++++++++++++++++++++++++--
 routers/user/profile.go        | 17 +++++++++++++--
 4 files changed, 105 insertions(+), 4 deletions(-)

diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml
index c45374f4f2..9f4b496010 100644
--- a/models/fixtures/repository.yml
+++ b/models/fixtures/repository.yml
@@ -56,3 +56,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
diff --git a/models/repo.go b/models/repo.go
index ab10cb6651..9b1b868778 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1737,11 +1737,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
diff --git a/models/repo_test.go b/models/repo_test.go
index 42bde62b46..499c8c83eb 100644
--- a/models/repo_test.go
+++ b/models/repo_test.go
@@ -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) {
@@ -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)
+}
diff --git a/routers/user/profile.go b/routers/user/profile.go
index 3b4d5a6b3c..ef6cb9cd9d 100644
--- a/routers/user/profile.go
+++ b/routers/user/profile.go
@@ -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,