Skip to content

Commit 39da526

Browse files
committed
Move more content into repository model
1 parent fc0b2ca commit 39da526

File tree

2 files changed

+166
-162
lines changed

2 files changed

+166
-162
lines changed

models/organization/org.go

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
actions_model "code.gitea.io/gitea/models/actions"
1313
"code.gitea.io/gitea/models/db"
1414
"code.gitea.io/gitea/models/perm"
15-
repo_model "code.gitea.io/gitea/models/repo"
1615
secret_model "code.gitea.io/gitea/models/secret"
1716
"code.gitea.io/gitea/models/unit"
1817
user_model "code.gitea.io/gitea/models/user"
@@ -651,164 +650,3 @@ func (org *Organization) GetUserTeamIDs(ctx context.Context, userID int64) ([]in
651650
func (org *Organization) GetUserTeams(ctx context.Context, userID int64) ([]*Team, error) {
652651
return org.getUserTeams(ctx, userID)
653652
}
654-
655-
// AccessibleReposEnvironment operations involving the repositories that are
656-
// accessible to a particular user
657-
type AccessibleReposEnvironment interface {
658-
CountRepos() (int64, error)
659-
RepoIDs(page, pageSize int) ([]int64, error)
660-
Repos(page, pageSize int) (repo_model.RepositoryList, error)
661-
MirrorRepos() (repo_model.RepositoryList, error)
662-
AddKeyword(keyword string)
663-
SetSort(db.SearchOrderBy)
664-
}
665-
666-
type accessibleReposEnv struct {
667-
org *Organization
668-
user *user_model.User
669-
team *Team
670-
teamIDs []int64
671-
ctx context.Context
672-
keyword string
673-
orderBy db.SearchOrderBy
674-
}
675-
676-
// AccessibleReposEnv builds an AccessibleReposEnvironment for the repositories in `org`
677-
// that are accessible to the specified user.
678-
func AccessibleReposEnv(ctx context.Context, org *Organization, userID int64) (AccessibleReposEnvironment, error) {
679-
var user *user_model.User
680-
681-
if userID > 0 {
682-
u, err := user_model.GetUserByID(ctx, userID)
683-
if err != nil {
684-
return nil, err
685-
}
686-
user = u
687-
}
688-
689-
teamIDs, err := org.getUserTeamIDs(ctx, userID)
690-
if err != nil {
691-
return nil, err
692-
}
693-
return &accessibleReposEnv{
694-
org: org,
695-
user: user,
696-
teamIDs: teamIDs,
697-
ctx: ctx,
698-
orderBy: db.SearchOrderByRecentUpdated,
699-
}, nil
700-
}
701-
702-
// AccessibleTeamReposEnv an AccessibleReposEnvironment for the repositories in `org`
703-
// that are accessible to the specified team.
704-
func (org *Organization) AccessibleTeamReposEnv(ctx context.Context, team *Team) AccessibleReposEnvironment {
705-
return &accessibleReposEnv{
706-
org: org,
707-
team: team,
708-
ctx: ctx,
709-
orderBy: db.SearchOrderByRecentUpdated,
710-
}
711-
}
712-
713-
func (env *accessibleReposEnv) cond() builder.Cond {
714-
cond := builder.NewCond()
715-
if env.team != nil {
716-
cond = cond.And(builder.Eq{"team_repo.team_id": env.team.ID})
717-
} else {
718-
if env.user == nil || !env.user.IsRestricted {
719-
cond = cond.Or(builder.Eq{
720-
"`repository`.owner_id": env.org.ID,
721-
"`repository`.is_private": false,
722-
})
723-
}
724-
if len(env.teamIDs) > 0 {
725-
cond = cond.Or(builder.In("team_repo.team_id", env.teamIDs))
726-
}
727-
}
728-
if env.keyword != "" {
729-
cond = cond.And(builder.Like{"`repository`.lower_name", strings.ToLower(env.keyword)})
730-
}
731-
return cond
732-
}
733-
734-
func (env *accessibleReposEnv) CountRepos() (int64, error) {
735-
repoCount, err := db.GetEngine(env.ctx).
736-
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
737-
Where(env.cond()).
738-
Distinct("`repository`.id").
739-
Count(&repo_model.Repository{})
740-
if err != nil {
741-
return 0, fmt.Errorf("count user repositories in organization: %w", err)
742-
}
743-
return repoCount, nil
744-
}
745-
746-
func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
747-
if page <= 0 {
748-
page = 1
749-
}
750-
751-
repoIDs := make([]int64, 0, pageSize)
752-
return repoIDs, db.GetEngine(env.ctx).
753-
Table("repository").
754-
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
755-
Where(env.cond()).
756-
GroupBy("`repository`.id,`repository`."+strings.Fields(string(env.orderBy))[0]).
757-
OrderBy(string(env.orderBy)).
758-
Limit(pageSize, (page-1)*pageSize).
759-
Cols("`repository`.id").
760-
Find(&repoIDs)
761-
}
762-
763-
func (env *accessibleReposEnv) Repos(page, pageSize int) (repo_model.RepositoryList, error) {
764-
repoIDs, err := env.RepoIDs(page, pageSize)
765-
if err != nil {
766-
return nil, fmt.Errorf("GetUserRepositoryIDs: %w", err)
767-
}
768-
769-
repos := make([]*repo_model.Repository, 0, len(repoIDs))
770-
if len(repoIDs) == 0 {
771-
return repos, nil
772-
}
773-
774-
return repos, db.GetEngine(env.ctx).
775-
In("`repository`.id", repoIDs).
776-
OrderBy(string(env.orderBy)).
777-
Find(&repos)
778-
}
779-
780-
func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
781-
repoIDs := make([]int64, 0, 10)
782-
return repoIDs, db.GetEngine(env.ctx).
783-
Table("repository").
784-
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
785-
Where(env.cond()).
786-
GroupBy("`repository`.id, `repository`.updated_unix").
787-
OrderBy(string(env.orderBy)).
788-
Cols("`repository`.id").
789-
Find(&repoIDs)
790-
}
791-
792-
func (env *accessibleReposEnv) MirrorRepos() (repo_model.RepositoryList, error) {
793-
repoIDs, err := env.MirrorRepoIDs()
794-
if err != nil {
795-
return nil, fmt.Errorf("MirrorRepoIDs: %w", err)
796-
}
797-
798-
repos := make([]*repo_model.Repository, 0, len(repoIDs))
799-
if len(repoIDs) == 0 {
800-
return repos, nil
801-
}
802-
803-
return repos, db.GetEngine(env.ctx).
804-
In("`repository`.id", repoIDs).
805-
Find(&repos)
806-
}
807-
808-
func (env *accessibleReposEnv) AddKeyword(keyword string) {
809-
env.keyword = keyword
810-
}
811-
812-
func (env *accessibleReposEnv) SetSort(orderBy db.SearchOrderBy) {
813-
env.orderBy = orderBy
814-
}

models/repo/org_repo.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ package repo
55

66
import (
77
"context"
8+
"fmt"
9+
"strings"
810

911
"code.gitea.io/gitea/models/db"
12+
org_model "code.gitea.io/gitea/models/organization"
13+
user_model "code.gitea.io/gitea/models/user"
14+
1015
"xorm.io/builder"
1116
)
1217

@@ -38,3 +43,164 @@ func GetTeamRepositories(ctx context.Context, opts *SearchTeamRepoOptions) (Repo
3843
return repos, sess.OrderBy("repository.name").
3944
Find(&repos)
4045
}
46+
47+
// AccessibleReposEnvironment operations involving the repositories that are
48+
// accessible to a particular user
49+
type AccessibleReposEnvironment interface {
50+
CountRepos() (int64, error)
51+
RepoIDs(page, pageSize int) ([]int64, error)
52+
Repos(page, pageSize int) (RepositoryList, error)
53+
MirrorRepos() (RepositoryList, error)
54+
AddKeyword(keyword string)
55+
SetSort(db.SearchOrderBy)
56+
}
57+
58+
type accessibleReposEnv struct {
59+
org *org_model.Organization
60+
user *user_model.User
61+
team *org_model.Team
62+
teamIDs []int64
63+
ctx context.Context
64+
keyword string
65+
orderBy db.SearchOrderBy
66+
}
67+
68+
// AccessibleReposEnv builds an AccessibleReposEnvironment for the repositories in `org`
69+
// that are accessible to the specified user.
70+
func AccessibleReposEnv(ctx context.Context, org *org_model.Organization, userID int64) (AccessibleReposEnvironment, error) {
71+
var user *user_model.User
72+
73+
if userID > 0 {
74+
u, err := user_model.GetUserByID(ctx, userID)
75+
if err != nil {
76+
return nil, err
77+
}
78+
user = u
79+
}
80+
81+
teamIDs, err := org.getUserTeamIDs(ctx, userID)
82+
if err != nil {
83+
return nil, err
84+
}
85+
return &accessibleReposEnv{
86+
org: org,
87+
user: user,
88+
teamIDs: teamIDs,
89+
ctx: ctx,
90+
orderBy: db.SearchOrderByRecentUpdated,
91+
}, nil
92+
}
93+
94+
// AccessibleTeamReposEnv an AccessibleReposEnvironment for the repositories in `org`
95+
// that are accessible to the specified team.
96+
func AccessibleTeamReposEnv(ctx context.Context, org *org_model.Organization, team *org_model.Team) AccessibleReposEnvironment {
97+
return &accessibleReposEnv{
98+
org: org,
99+
team: team,
100+
ctx: ctx,
101+
orderBy: db.SearchOrderByRecentUpdated,
102+
}
103+
}
104+
105+
func (env *accessibleReposEnv) cond() builder.Cond {
106+
cond := builder.NewCond()
107+
if env.team != nil {
108+
cond = cond.And(builder.Eq{"team_repo.team_id": env.team.ID})
109+
} else {
110+
if env.user == nil || !env.user.IsRestricted {
111+
cond = cond.Or(builder.Eq{
112+
"`repository`.owner_id": env.org.ID,
113+
"`repository`.is_private": false,
114+
})
115+
}
116+
if len(env.teamIDs) > 0 {
117+
cond = cond.Or(builder.In("team_repo.team_id", env.teamIDs))
118+
}
119+
}
120+
if env.keyword != "" {
121+
cond = cond.And(builder.Like{"`repository`.lower_name", strings.ToLower(env.keyword)})
122+
}
123+
return cond
124+
}
125+
126+
func (env *accessibleReposEnv) CountRepos() (int64, error) {
127+
repoCount, err := db.GetEngine(env.ctx).
128+
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
129+
Where(env.cond()).
130+
Distinct("`repository`.id").
131+
Count(&repo_model.Repository{})
132+
if err != nil {
133+
return 0, fmt.Errorf("count user repositories in organization: %w", err)
134+
}
135+
return repoCount, nil
136+
}
137+
138+
func (env *accessibleReposEnv) RepoIDs(page, pageSize int) ([]int64, error) {
139+
if page <= 0 {
140+
page = 1
141+
}
142+
143+
repoIDs := make([]int64, 0, pageSize)
144+
return repoIDs, db.GetEngine(env.ctx).
145+
Table("repository").
146+
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
147+
Where(env.cond()).
148+
GroupBy("`repository`.id,`repository`."+strings.Fields(string(env.orderBy))[0]).
149+
OrderBy(string(env.orderBy)).
150+
Limit(pageSize, (page-1)*pageSize).
151+
Cols("`repository`.id").
152+
Find(&repoIDs)
153+
}
154+
155+
func (env *accessibleReposEnv) Repos(page, pageSize int) (repo_model.RepositoryList, error) {
156+
repoIDs, err := env.RepoIDs(page, pageSize)
157+
if err != nil {
158+
return nil, fmt.Errorf("GetUserRepositoryIDs: %w", err)
159+
}
160+
161+
repos := make([]*repo_model.Repository, 0, len(repoIDs))
162+
if len(repoIDs) == 0 {
163+
return repos, nil
164+
}
165+
166+
return repos, db.GetEngine(env.ctx).
167+
In("`repository`.id", repoIDs).
168+
OrderBy(string(env.orderBy)).
169+
Find(&repos)
170+
}
171+
172+
func (env *accessibleReposEnv) MirrorRepoIDs() ([]int64, error) {
173+
repoIDs := make([]int64, 0, 10)
174+
return repoIDs, db.GetEngine(env.ctx).
175+
Table("repository").
176+
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
177+
Where(env.cond()).
178+
GroupBy("`repository`.id, `repository`.updated_unix").
179+
OrderBy(string(env.orderBy)).
180+
Cols("`repository`.id").
181+
Find(&repoIDs)
182+
}
183+
184+
func (env *accessibleReposEnv) MirrorRepos() (repo_model.RepositoryList, error) {
185+
repoIDs, err := env.MirrorRepoIDs()
186+
if err != nil {
187+
return nil, fmt.Errorf("MirrorRepoIDs: %w", err)
188+
}
189+
190+
repos := make([]*repo_model.Repository, 0, len(repoIDs))
191+
if len(repoIDs) == 0 {
192+
return repos, nil
193+
}
194+
195+
return repos, db.GetEngine(env.ctx).
196+
In("`repository`.id", repoIDs).
197+
Find(&repos)
198+
}
199+
200+
func (env *accessibleReposEnv) AddKeyword(keyword string) {
201+
env.keyword = keyword
202+
}
203+
204+
func (env *accessibleReposEnv) SetSort(orderBy db.SearchOrderBy) {
205+
env.orderBy = orderBy
206+
}

0 commit comments

Comments
 (0)