Skip to content

Commit 6310dfd

Browse files
committed
Fix test
1 parent 6e22af2 commit 6310dfd

File tree

8 files changed

+9
-139
lines changed

8 files changed

+9
-139
lines changed

models/notification.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
266266

267267
return err
268268
}
269-
if issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypePullRequests) {
269+
if issue.IsPull && !CheckRepoUnitUser(ctx, issue.Repo, user, unit.TypePullRequests) {
270270
continue
271271
}
272-
if !issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypeIssues) {
272+
if !issue.IsPull && !CheckRepoUnitUser(ctx, issue.Repo, user, unit.TypeIssues) {
273273
continue
274274
}
275275

models/repo.go

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ func NewRepoContext() {
4343
}
4444

4545
// CheckRepoUnitUser check whether user could visit the unit of this repository
46-
func CheckRepoUnitUser(repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
47-
return checkRepoUnitUser(db.DefaultContext, repo, user, unitType)
48-
}
49-
50-
func checkRepoUnitUser(ctx context.Context, repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
46+
func CheckRepoUnitUser(ctx context.Context, repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
5147
if user != nil && user.IsAdmin {
5248
return true
5349
}
@@ -60,116 +56,6 @@ func checkRepoUnitUser(ctx context.Context, repo *repo_model.Repository, user *u
6056
return perm.CanRead(unitType)
6157
}
6258

63-
// GetReviewerTeams get all teams can be requested to review
64-
func GetReviewerTeams(repo *repo_model.Repository) ([]*organization.Team, error) {
65-
if err := repo.GetOwner(db.DefaultContext); err != nil {
66-
return nil, err
67-
}
68-
if !repo.Owner.IsOrganization() {
69-
return nil, nil
70-
}
71-
72-
teams, err := organization.GetTeamsWithAccessToRepo(db.DefaultContext, repo.OwnerID, repo.ID, perm.AccessModeRead)
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
return teams, err
78-
}
79-
80-
// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize
81-
func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
82-
size, err := util.GetDirectorySize(repo.RepoPath())
83-
if err != nil {
84-
return fmt.Errorf("updateSize: %v", err)
85-
}
86-
87-
lfsSize, err := GetRepoLFSSize(ctx, repo.ID)
88-
if err != nil {
89-
return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
90-
}
91-
92-
return repo_model.UpdateRepoSize(ctx, repo.ID, size+lfsSize)
93-
}
94-
95-
// CanUserForkRepo returns true if specified user can fork repository.
96-
func CanUserForkRepo(user *user_model.User, repo *repo_model.Repository) (bool, error) {
97-
if user == nil {
98-
return false, nil
99-
}
100-
if repo.OwnerID != user.ID && !repo_model.HasForkedRepo(user.ID, repo.ID) {
101-
return true, nil
102-
}
103-
ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(user.ID)
104-
if err != nil {
105-
return false, err
106-
}
107-
for _, org := range ownedOrgs {
108-
if repo.OwnerID != org.ID && !repo_model.HasForkedRepo(org.ID, repo.ID) {
109-
return true, nil
110-
}
111-
}
112-
return false, nil
113-
}
114-
115-
// FindUserOrgForks returns the forked repositories for one user from a repository
116-
func FindUserOrgForks(ctx context.Context, repoID, userID int64) ([]*repo_model.Repository, error) {
117-
cond := builder.And(
118-
builder.Eq{"fork_id": repoID},
119-
builder.In("owner_id",
120-
builder.Select("org_id").
121-
From("org_user").
122-
Where(builder.Eq{"uid": userID}),
123-
),
124-
)
125-
126-
var repos []*repo_model.Repository
127-
return repos, db.GetEngine(ctx).Table("repository").Where(cond).Find(&repos)
128-
}
129-
130-
// GetForksByUserAndOrgs return forked repos of the user and owned orgs
131-
func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) {
132-
var repoList []*repo_model.Repository
133-
if user == nil {
134-
return repoList, nil
135-
}
136-
forkedRepo, err := repo_model.GetUserFork(ctx, repo.ID, user.ID)
137-
if err != nil {
138-
return repoList, err
139-
}
140-
if forkedRepo != nil {
141-
repoList = append(repoList, forkedRepo)
142-
}
143-
orgForks, err := FindUserOrgForks(ctx, repo.ID, user.ID)
144-
if err != nil {
145-
return nil, err
146-
}
147-
repoList = append(repoList, orgForks...)
148-
return repoList, nil
149-
}
150-
151-
// CanUserDelete returns true if user could delete the repository
152-
func CanUserDelete(repo *repo_model.Repository, user *user_model.User) (bool, error) {
153-
if user.IsAdmin || user.ID == repo.OwnerID {
154-
return true, nil
155-
}
156-
157-
if err := repo.GetOwner(db.DefaultContext); err != nil {
158-
return false, err
159-
}
160-
161-
if repo.Owner.IsOrganization() {
162-
isOwner, err := organization.OrgFromUser(repo.Owner).IsOwnedBy(user.ID)
163-
if err != nil {
164-
return false, err
165-
} else if isOwner {
166-
return true, nil
167-
}
168-
}
169-
170-
return false, nil
171-
}
172-
17359
// CreateRepoOptions contains the create repository options
17460
type CreateRepoOptions struct {
17561
Name string

models/repo_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,3 @@ func TestDoctorUserStarNum(t *testing.T) {
8989

9090
assert.NoError(t, DoctorUserStarNum())
9191
}
92-
93-
func TestRepoGetReviewerTeams(t *testing.T) {
94-
assert.NoError(t, unittest.PrepareTestDatabase())
95-
96-
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
97-
teams, err := GetReviewerTeams(repo2)
98-
assert.NoError(t, err)
99-
assert.Empty(t, teams)
100-
101-
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
102-
teams, err = GetReviewerTeams(repo3)
103-
assert.NoError(t, err)
104-
assert.Len(t, teams, 2)
105-
}

routers/web/explore/code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Code(ctx *context.Context) {
7979
rightRepoMap := make(map[int64]*repo_model.Repository, len(repoMaps))
8080
repoIDs = make([]int64, 0, len(repoMaps))
8181
for id, repo := range repoMaps {
82-
if models.CheckRepoUnitUser(repo, ctx.Doer, unit.TypeCode) {
82+
if models.CheckRepoUnitUser(ctx, repo, ctx.Doer, unit.TypeCode) {
8383
rightRepoMap[id] = repo
8484
repoIDs = append(repoIDs, id)
8585
}

routers/web/repo/compare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
457457
if rootRepo != nil &&
458458
rootRepo.ID != ci.HeadRepo.ID &&
459459
rootRepo.ID != baseRepo.ID {
460-
canRead := models.CheckRepoUnitUser(rootRepo, ctx.Doer, unit.TypeCode)
460+
canRead := models.CheckRepoUnitUser(ctx, rootRepo, ctx.Doer, unit.TypeCode)
461461
if canRead {
462462
ctx.Data["RootRepo"] = rootRepo
463463
if !fileOnly {
@@ -482,7 +482,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
482482
ownForkRepo.ID != ci.HeadRepo.ID &&
483483
ownForkRepo.ID != baseRepo.ID &&
484484
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
485-
canRead := models.CheckRepoUnitUser(ownForkRepo, ctx.Doer, unit.TypeCode)
485+
canRead := models.CheckRepoUnitUser(ctx, ownForkRepo, ctx.Doer, unit.TypeCode)
486486
if canRead {
487487
ctx.Data["OwnForkRepo"] = ownForkRepo
488488
if !fileOnly {

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func Create(ctx *context.Context) {
152152
templateID := ctx.FormInt64("template_id")
153153
if templateID > 0 {
154154
templateRepo, err := repo_model.GetRepositoryByID(templateID)
155-
if err == nil && models.CheckRepoUnitUser(templateRepo, ctxUser, unit.TypeCode) {
155+
if err == nil && models.CheckRepoUnitUser(ctx, templateRepo, ctxUser, unit.TypeCode) {
156156
ctx.Data["repo_template"] = templateID
157157
ctx.Data["repo_template_name"] = templateRepo.Name
158158
}

services/mailer/mail_issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, vi
145145
visited[user.ID] = true
146146

147147
// test if this user is allowed to see the issue/pull
148-
if !models.CheckRepoUnitUser(ctx.Issue.Repo, user, checkUnit) {
148+
if !models.CheckRepoUnitUser(ctx, ctx.Issue.Repo, user, checkUnit) {
149149
continue
150150
}
151151

services/repository/review.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package repository
66

77
import (
8-
"fmt"
9-
108
"code.gitea.io/gitea/models/db"
119
"code.gitea.io/gitea/models/organization"
1210
"code.gitea.io/gitea/models/perm"
@@ -19,7 +17,7 @@ func GetReviewerTeams(repo *repo_model.Repository) ([]*organization.Team, error)
1917
return nil, err
2018
}
2119
if !repo.Owner.IsOrganization() {
22-
return nil, fmt.Errorf("repo is not owned by an organization")
20+
return nil, nil
2321
}
2422

2523
return organization.GetTeamsWithAccessToRepo(db.DefaultContext, repo.OwnerID, repo.ID, perm.AccessModeRead)

0 commit comments

Comments
 (0)