Skip to content

Commit 2609511

Browse files
lunnydelvh
andauthored
Move some repository related code into sub package (#19711)
* Move some repository related code into sub package * Move more repository functions out of models * Fix lint * Some performance optimization for webhooks and others * some refactors * Fix lint * Fix * Update modules/repository/delete.go Co-authored-by: delvh <[email protected]> * Fix test * Merge * Fix test * Fix test * Fix test * Fix test Co-authored-by: delvh <[email protected]>
1 parent ebeb6e7 commit 2609511

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1755
-1673
lines changed

cmd/admin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
asymkey_model "code.gitea.io/gitea/models/asymkey"
1818
"code.gitea.io/gitea/models/auth"
1919
"code.gitea.io/gitea/models/db"
20+
repo_model "code.gitea.io/gitea/models/repo"
2021
user_model "code.gitea.io/gitea/models/user"
2122
"code.gitea.io/gitea/modules/git"
2223
"code.gitea.io/gitea/modules/graceful"
@@ -722,9 +723,9 @@ func runRepoSyncReleases(_ *cli.Context) error {
722723

723724
log.Trace("Synchronizing repository releases (this may take a while)")
724725
for page := 1; ; page++ {
725-
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
726+
repos, count, err := repo_model.SearchRepositoryByName(&repo_model.SearchRepoOptions{
726727
ListOptions: db.ListOptions{
727-
PageSize: models.RepositoryListDefaultPageSize,
728+
PageSize: repo_model.RepositoryListDefaultPageSize,
728729
Page: page,
729730
},
730731
Private: true,

models/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
393393

394394
// check readable repositories by doer/actor
395395
if opts.Actor == nil || !opts.Actor.IsAdmin {
396-
cond = cond.And(builder.In("repo_id", AccessibleRepoIDsQuery(opts.Actor)))
396+
cond = cond.And(builder.In("repo_id", repo_model.AccessibleRepoIDsQuery(opts.Actor)))
397397
}
398398

399399
if opts.RequestedRepo != nil {

models/asymkey/ssh_key_deploy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ func GetDeployKeyByRepo(ctx context.Context, keyID, repoID int64) (*DeployKey, e
190190
return key, nil
191191
}
192192

193+
// IsDeployKeyExistByKeyID return true if there is at least one deploykey with the key id
194+
func IsDeployKeyExistByKeyID(ctx context.Context, keyID int64) (bool, error) {
195+
return db.GetEngine(ctx).
196+
Where("key_id = ?", keyID).
197+
Get(new(DeployKey))
198+
}
199+
193200
// UpdateDeployKeyCols updates deploy key information in the specified columns.
194201
func UpdateDeployKeyCols(key *DeployKey, cols ...string) error {
195202
_, err := db.GetEngine(db.DefaultContext).ID(key.ID).Cols(cols...).Update(key)

models/db/engine.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,6 @@ func MaxBatchInsertSize(bean interface{}) int {
271271
return 999 / len(t.ColumnsSeq())
272272
}
273273

274-
// Count returns records number according struct's fields as database query conditions
275-
func Count(bean interface{}) (int64, error) {
276-
return x.Count(bean)
277-
}
278-
279274
// IsTableNotEmpty returns true if table has at least one record
280275
func IsTableNotEmpty(tableName string) (bool, error) {
281276
return x.Table(tableName).Exist()

models/issue.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,48 @@ func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
13431343
}
13441344
}
13451345

1346+
// teamUnitsRepoCond returns query condition for those repo id in the special org team with special units access
1347+
func teamUnitsRepoCond(id string, userID, orgID, teamID int64, units ...unit.Type) builder.Cond {
1348+
return builder.In(id,
1349+
builder.Select("repo_id").From("team_repo").Where(
1350+
builder.Eq{
1351+
"team_id": teamID,
1352+
}.And(
1353+
builder.Or(
1354+
// Check if the user is member of the team.
1355+
builder.In(
1356+
"team_id", builder.Select("team_id").From("team_user").Where(
1357+
builder.Eq{
1358+
"uid": userID,
1359+
},
1360+
),
1361+
),
1362+
// Check if the user is in the owner team of the organisation.
1363+
builder.Exists(builder.Select("team_id").From("team_user").
1364+
Where(builder.Eq{
1365+
"org_id": orgID,
1366+
"team_id": builder.Select("id").From("team").Where(
1367+
builder.Eq{
1368+
"org_id": orgID,
1369+
"lower_name": strings.ToLower(organization.OwnerTeamName),
1370+
}),
1371+
"uid": userID,
1372+
}),
1373+
),
1374+
)).And(
1375+
builder.In(
1376+
"team_id", builder.Select("team_id").From("team_unit").Where(
1377+
builder.Eq{
1378+
"`team_unit`.org_id": orgID,
1379+
}.And(
1380+
builder.In("`team_unit`.type", units),
1381+
),
1382+
),
1383+
),
1384+
),
1385+
))
1386+
}
1387+
13461388
// issuePullAccessibleRepoCond userID must not be zero, this condition require join repository table
13471389
func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organization.Organization, team *organization.Team, isPull bool) builder.Cond {
13481390
cond := builder.NewCond()
@@ -1356,19 +1398,19 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati
13561398
} else {
13571399
cond = cond.And(
13581400
builder.Or(
1359-
userOrgUnitRepoCond(repoIDstr, userID, org.ID, unitType), // team member repos
1360-
userOrgPublicUnitRepoCond(userID, org.ID), // user org public non-member repos, TODO: check repo has issues
1401+
repo_model.UserOrgUnitRepoCond(repoIDstr, userID, org.ID, unitType), // team member repos
1402+
repo_model.UserOrgPublicUnitRepoCond(userID, org.ID), // user org public non-member repos, TODO: check repo has issues
13611403
),
13621404
)
13631405
}
13641406
} else {
13651407
cond = cond.And(
13661408
builder.Or(
1367-
userOwnedRepoCond(userID), // owned repos
1368-
userCollaborationRepoCond(repoIDstr, userID), // collaboration repos
1369-
userAssignedRepoCond(repoIDstr, userID), // user has been assigned accessible public repos
1370-
userMentionedRepoCond(repoIDstr, userID), // user has been mentioned accessible public repos
1371-
userCreateIssueRepoCond(repoIDstr, userID, isPull), // user has created issue/pr accessible public repos
1409+
repo_model.UserOwnedRepoCond(userID), // owned repos
1410+
repo_model.UserCollaborationRepoCond(repoIDstr, userID), // collaboration repos
1411+
repo_model.UserAssignedRepoCond(repoIDstr, userID), // user has been assigned accessible public repos
1412+
repo_model.UserMentionedRepoCond(repoIDstr, userID), // user has been mentioned accessible public repos
1413+
repo_model.UserCreateIssueRepoCond(repoIDstr, userID, isPull), // user has created issue/pr accessible public repos
13721414
),
13731415
)
13741416
}
@@ -1434,7 +1476,7 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14341476

14351477
opts.setupSessionNoLimit(sess)
14361478

1437-
accessCond := accessibleRepositoryCondition(user)
1479+
accessCond := repo_model.AccessibleRepositoryCondition(user)
14381480
if err := sess.Where(accessCond).
14391481
Distinct("issue.repo_id").
14401482
Table("issue").

models/issue_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Rep
7575
}
7676
}
7777
}
78-
return valuesRepository(repoMaps), nil
78+
return repo_model.ValuesRepository(repoMaps), nil
7979
}
8080

8181
// LoadRepositories loads issues' all repositories

models/issue_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func init() {
2626
}
2727

2828
func newIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issue) error {
29-
assignees, err := getRepoAssignees(ctx, repo)
29+
assignees, err := repo_model.GetRepoAssignees(ctx, repo)
3030
if err != nil {
3131
return fmt.Errorf("getAssignees: %v", err)
3232
}

models/lfs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func LFSObjectAccessible(user *user_model.User, oid string) (bool, error) {
142142
count, err := db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
143143
return count > 0, err
144144
}
145-
cond := accessibleRepositoryCondition(user)
145+
cond := repo_model.AccessibleRepositoryCondition(user)
146146
count, err := db.GetEngine(db.DefaultContext).Where(cond).Join("INNER", "repository", "`lfs_meta_object`.repository_id = `repository`.id").Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
147147
return count > 0, err
148148
}
@@ -173,7 +173,7 @@ func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int6
173173
newMetas := make([]*LFSMetaObject, 0, len(metas))
174174
cond := builder.In(
175175
"`lfs_meta_object`.repository_id",
176-
builder.Select("`repository`.id").From("repository").Where(accessibleRepositoryCondition(user)),
176+
builder.Select("`repository`.id").From("repository").Where(repo_model.AccessibleRepositoryCondition(user)),
177177
)
178178
err = sess.Cols("oid").Where(cond).In("oid", oids...).GroupBy("oid").Find(&newMetas)
179179
if err != nil {
@@ -246,3 +246,12 @@ func CopyLFS(ctx context.Context, newRepo, oldRepo *repo_model.Repository) error
246246

247247
return nil
248248
}
249+
250+
// GetRepoLFSSize return a repository's lfs files size
251+
func GetRepoLFSSize(ctx context.Context, repoID int64) (int64, error) {
252+
lfsSize, err := db.GetEngine(ctx).Where("repository_id = ?", repoID).SumInt(new(LFSMetaObject), "size")
253+
if err != nil {
254+
return 0, fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
255+
}
256+
return lfsSize, nil
257+
}

models/notification.go

Lines changed: 5 additions & 5 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

@@ -510,9 +510,9 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
510510
}
511511

512512
// LoadRepos loads repositories from database
513-
func (nl NotificationList) LoadRepos() (RepositoryList, []int, error) {
513+
func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error) {
514514
if len(nl) == 0 {
515-
return RepositoryList{}, []int{}, nil
515+
return repo_model.RepositoryList{}, []int{}, nil
516516
}
517517

518518
repoIDs := nl.getPendingRepoIDs()
@@ -548,7 +548,7 @@ func (nl NotificationList) LoadRepos() (RepositoryList, []int, error) {
548548

549549
failed := []int{}
550550

551-
reposList := make(RepositoryList, 0, len(repoIDs))
551+
reposList := make(repo_model.RepositoryList, 0, len(repoIDs))
552552
for i, notification := range nl {
553553
if notification.Repository == nil {
554554
notification.Repository = repos[notification.RepoID]

models/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func GetUserOrgsList(user *user_model.User) ([]*MinimalOrg, error) {
5454
Join("LEFT", builder.
5555
Select("id as repo_id, owner_id as repo_owner_id").
5656
From("repository").
57-
Where(accessibleRepositoryCondition(user)), "`repository`.repo_owner_id = `team`.org_id").
57+
Where(repo_model.AccessibleRepositoryCondition(user)), "`repository`.repo_owner_id = `team`.org_id").
5858
Where("`team_user`.uid = ?", user.ID).
5959
GroupBy(groupByStr)
6060

0 commit comments

Comments
 (0)