Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `NOTICE_PAGING_NUM`: **25**: Number of notices that are shown in one page.
- `ORG_PAGING_NUM`: **50**: Number of organizations that are shown in one page.

### UI - Expore (`ui.explore`)

- `REQUIRE_SIGNIN_VIEW`: **false**: Only allow signed in users to view the explore pages.
- `ONLY_SHOW_USERS_WITH_PUBLIC_REPOS`: **false**: Only show users with public repos on the explore users page.

### UI - Metadata (`ui.meta`)

- `AUTHOR`: **Gitea - Git with a cup of tea**: Author meta tag of the homepage.
Expand Down
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ menu:
- `NOTICE_PAGING_NUM`: 系统提示页面每页显示的提示数量。
- `ORG_PAGING_NUM`: 组织管理页面每页显示的组织数量。

### UI - Expore (`ui.explore`)

- `REQUIRE_SIGNIN_VIEW`: **false**: 仅允许已登录的用户查看探索页面。
- `ONLY_SHOW_USERS_WITH_PUBLIC_REPOS`: **false**:在浏览用户页面上仅显示拥有公共仓库的用户。

## Markdown (`markdown`)

- `ENABLE_HARD_LINE_BREAK`: 是否启用硬换行扩展。
Expand Down
10 changes: 10 additions & 0 deletions models/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ func assertCount(t *testing.T, bean interface{}, expected int) {
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
}

// assertCount test the count of database entries matching bean
func assertPublicReposCount(t *testing.T, user *User) {
count, err := x.Where("is_private = ?", false).Count(&Repository{OwnerID: user.ID})
assert.NoError(t, err)

assert.EqualValues(t, user.NumPublicRepos, count,
"Failed consistency test, NumPublicRepos of User[%d]", user.ID)
}

func (user *User) checkForConsistency(t *testing.T) {
assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
assertPublicReposCount(t, user)
assertCount(t, &Star{UID: user.ID}, user.NumStars)
assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
Expand Down
19 changes: 19 additions & 0 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
avatar: avatar2
avatar_email: [email protected]
num_repos: 9
num_public_repos: 5
num_stars: 2
num_followers: 2
num_following: 1
Expand All @@ -53,6 +54,7 @@
avatar: avatar3
avatar_email: [email protected]
num_repos: 3
num_public_repos: 1
num_members: 3
num_teams: 4

Expand Down Expand Up @@ -89,6 +91,7 @@
avatar: avatar5
avatar_email: [email protected]
num_repos: 1
num_public_repos: 1
allow_create_organization: false
is_active: true
num_following: 0
Expand Down Expand Up @@ -179,6 +182,7 @@
avatar: avatar10
avatar_email: [email protected]
num_repos: 3
num_public_repos: 1
is_active: true

-
Expand All @@ -195,6 +199,7 @@
avatar: avatar11
avatar_email: [email protected]
num_repos: 1
num_public_repos: 1
is_active: true

-
Expand All @@ -211,6 +216,7 @@
avatar: avatar12
avatar_email: [email protected]
num_repos: 1
num_public_repos: 1
is_active: true

-
Expand All @@ -227,6 +233,7 @@
avatar: avatar13
avatar_email: [email protected]
num_repos: 1
num_public_repos: 1
is_active: true

-
Expand All @@ -243,6 +250,7 @@
avatar: avatar14
avatar_email: [email protected]
num_repos: 3
num_public_repos: 2
is_active: true

-
Expand All @@ -259,6 +267,7 @@
avatar: avatar15
avatar_email: [email protected]
num_repos: 4
num_public_repos: 2
is_active: true

-
Expand All @@ -275,6 +284,7 @@
avatar: avatar16
avatar_email: [email protected]
num_repos: 2
num_public_repos: 1
is_active: true

-
Expand All @@ -291,6 +301,7 @@
avatar: avatar17
avatar_email: [email protected]
num_repos: 2
num_public_repos: 1
is_active: true
num_members: 3
num_teams: 3
Expand Down Expand Up @@ -325,6 +336,7 @@
avatar: avatar19
avatar_email: [email protected]
num_repos: 2
num_public_repos: 1
is_active: true
num_members: 1
num_teams: 1
Expand All @@ -343,6 +355,7 @@
avatar: avatar20
avatar_email: [email protected]
num_repos: 4
num_public_repos: 2
is_active: true

-
Expand All @@ -359,6 +372,7 @@
avatar: avatar21
avatar_email: [email protected]
num_repos: 2
num_public_repos: 2
is_active: true

-
Expand All @@ -375,6 +389,7 @@
avatar: avatar22
avatar_email: [email protected]
num_repos: 2
num_public_repos: 1
is_active: true
num_members: 0
num_teams: 0
Expand All @@ -394,6 +409,7 @@
avatar: avatar23
avatar_email: [email protected]
num_repos: 2
num_public_repos: 1
is_active: true
num_members: 0
num_teams: 0
Expand Down Expand Up @@ -451,6 +467,7 @@
avatar: avatar26
avatar_email: [email protected]
num_repos: 4
num_public_repos: 3
num_members: 0
num_teams: 1
repo_admin_change_team_access: true
Expand All @@ -470,6 +487,7 @@
avatar: avatar27
avatar_email: [email protected]
num_repos: 3
num_public_repos: 3

-
id: 28
Expand Down Expand Up @@ -524,4 +542,5 @@
avatar: avatar29
avatar_email: [email protected]
num_repos: 2
num_public_repos: 2
is_active: true
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ var migrations = []Migration{
NewMigration("Add time_id column to Comment", addTimeIDCommentColumn),
// v174 -> v175
NewMigration("create repo transfer table", addRepoTransfer),
// v175 -> v176
NewMigration("add num public repos", updateNumPublicRepos),
}

// GetCurrentDBVersion returns the current db version
Expand Down
73 changes: 73 additions & 0 deletions models/migrations/v175.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2021 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 migrations

import (
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)

func updateNumPublicRepos(x *xorm.Engine) error {
// User represents a user
type User struct {
ID int64
NumPublicRepos int `xorm:"INDEX NOT NULL DEFAULT 0"`
}

// Repository represents a Repo
type Repository struct {
ID int64
OwnerID int64
IsPrivate bool
}

if err := x.Sync2(&User{}, &Repository{}); err != nil {
return err
}
var batchSize = 100

users := make([]*User, 0, batchSize)

sess := x.NewSession()
defer sess.Close()

sess.SetExpr("num_public_repos", 0).Update(&User{})

for start := 0; ; start += batchSize {
users = users[:0]

if err := sess.Begin(); err != nil {
return err
}
switch {
case setting.Database.UseMSSQL:
if _, err := sess.Exec("SELECT owner_id AS id, COUNT(id) AS num_public_repos INTO #temp_user FROM repository WHERE repository.is_private = 0 GROUP BY owner_id"); err != nil {
return err
}
if err := sess.Table("#temp_user").Limit(batchSize, start).Asc("id").Find(&users); err != nil {
return err
}
default:
if err := sess.Select("owner_id AS id, count(id) AS num_public_repos").Table("repository").Where("repository.is_private = ?", false).GroupBy("repository.owner_id").Limit(batchSize, start).Asc("id").Find(&users); err != nil {
return err
}
}

if len(users) == 0 {
break
}

for _, user := range users {
if _, err := sess.ID(user.ID).Cols("num_public_repos").Update(user); err != nil {
return err
}
}

if err := sess.Commit(); err != nil {
return err
}
}
return nil
}
20 changes: 20 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,12 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
return fmt.Errorf("increment user total_repos: %v", err)
}
u.NumRepos++
if !repo.IsPrivate {
if _, err = ctx.e.Incr("num_public_repos").ID(u.ID).Update(new(User)); err != nil {
return fmt.Errorf("increment user total_public_repos: %v", err)
}
u.NumPublicRepos++
}

// Give access to all members in teams with access to all repositories.
if u.IsOrganization() {
Expand Down Expand Up @@ -1293,6 +1299,14 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
if err != nil {
return err
}
repo.Owner.NumPublicRepos--
} else {
repo.Owner.NumPublicRepos++
}

_, err = e.ID(repo.OwnerID).Cols("num_public_repos").Update(repo.Owner)
if err != nil {
return err
}

// Create/Remove git-daemon-export-ok for git-daemon...
Expand Down Expand Up @@ -1842,6 +1856,12 @@ func CheckRepoStats(ctx context.Context) error {
"UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?",
"user count 'num_repos'",
},
// User.NumPublicRepos
{
"SELECT `user`.id FROM `user` WHERE `user`.num_public_repos!=(SELECT COUNT(*) FROM `repository` WHERE owner_id=`user`.id AND is_private=0)",
"UPDATE `user` SET num_public_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=? AND is_private=0) WHERE id=?",
"user count 'num_public_repos'",
},
// Issue.NumComments
{
"SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)",
Expand Down
8 changes: 8 additions & 0 deletions models/repo_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
return fmt.Errorf("decrease old owner repository count: %v", err)
}

if !repo.IsPrivate {
if _, err = sess.Exec("UPDATE `user` SET num_public_repos=num_public_repos+1 WHERE id=?", newOwner.ID); err != nil {
return fmt.Errorf("increase new owner public repository count: %v", err)
} else if _, err = sess.Exec("UPDATE `user` SET num_public_repos=num_public_repos-1 WHERE id=?", oldOwner.ID); err != nil {
return fmt.Errorf("decrease old owner public repository count: %v", err)
}
}

if err := watchRepo(sess, doer.ID, repo.ID, true); err != nil {
return fmt.Errorf("watchRepo: %v", err)
}
Expand Down
30 changes: 18 additions & 12 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ type User struct {
UseCustomAvatar bool

// Counters
NumFollowers int
NumFollowing int `xorm:"NOT NULL DEFAULT 0"`
NumStars int
NumRepos int
NumFollowers int
NumFollowing int `xorm:"NOT NULL DEFAULT 0"`
NumStars int
NumRepos int
NumPublicRepos int `xorm:"INDEX NOT NULL DEFAULT 0"`

// For organization
NumTeams int
Expand Down Expand Up @@ -1559,14 +1560,15 @@ func GetUser(user *User) (bool, error) {
// SearchUserOptions contains the options for searching
type SearchUserOptions struct {
ListOptions
Keyword string
Type UserType
UID int64
OrderBy SearchOrderBy
Visible []structs.VisibleType
Actor *User // The user doing the search
IsActive util.OptionalBool
SearchByEmail bool // Search by email as well as username/full name
Keyword string
Type UserType
UID int64
OrderBy SearchOrderBy
Visible []structs.VisibleType
Actor *User // The user doing the search
IsActive util.OptionalBool
SearchByEmail bool // Search by email as well as username/full name
OnlyUsersWithPublicRepos bool
}

func (opts *SearchUserOptions) toConds() builder.Cond {
Expand Down Expand Up @@ -1620,6 +1622,10 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
cond = cond.And(builder.Eq{"is_active": opts.IsActive.IsTrue()})
}

if opts.OnlyUsersWithPublicRepos {
cond = cond.And(builder.Gt{"num_public_repos": 0})
}

return cond
}

Expand Down
Loading