Skip to content

Commit 038d714

Browse files
committed
Some refactors related repository model
1 parent fb8166c commit 038d714

Some content is hidden

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

43 files changed

+243
-243
lines changed

integrations/api_repo_lfs_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestAPILFSBatch(t *testing.T) {
7575

7676
content := []byte("dummy1")
7777
oid := storeObjectInRepo(t, repo.ID, &content)
78-
defer repo.RemoveLFSMetaObjectByOid(oid)
78+
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
7979

8080
session := loginUser(t, "user2")
8181

@@ -259,7 +259,7 @@ func TestAPILFSBatch(t *testing.T) {
259259
content := []byte("dummy0")
260260
storeObjectInRepo(t, repo2.ID, &content)
261261

262-
meta, err := repo.GetLFSMetaObjectByOid(p.Oid)
262+
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
263263
assert.Nil(t, meta)
264264
assert.Equal(t, models.ErrLFSObjectNotExist, err)
265265

@@ -274,7 +274,7 @@ func TestAPILFSBatch(t *testing.T) {
274274
assert.Nil(t, br.Objects[0].Error)
275275
assert.Empty(t, br.Objects[0].Actions)
276276

277-
meta, err = repo.GetLFSMetaObjectByOid(p.Oid)
277+
meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
278278
assert.NoError(t, err)
279279
assert.NotNil(t, meta)
280280
})
@@ -331,7 +331,7 @@ func TestAPILFSUpload(t *testing.T) {
331331

332332
content := []byte("dummy3")
333333
oid := storeObjectInRepo(t, repo.ID, &content)
334-
defer repo.RemoveLFSMetaObjectByOid(oid)
334+
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
335335

336336
session := loginUser(t, "user2")
337337

@@ -360,7 +360,7 @@ func TestAPILFSUpload(t *testing.T) {
360360
err = contentStore.Put(p, bytes.NewReader([]byte("dummy5")))
361361
assert.NoError(t, err)
362362

363-
meta, err := repo.GetLFSMetaObjectByOid(p.Oid)
363+
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
364364
assert.Nil(t, meta)
365365
assert.Equal(t, models.ErrLFSObjectNotExist, err)
366366

@@ -373,7 +373,7 @@ func TestAPILFSUpload(t *testing.T) {
373373
req := newRequest(t, p, "dummy5")
374374

375375
session.MakeRequest(t, req, http.StatusOK)
376-
meta, err = repo.GetLFSMetaObjectByOid(p.Oid)
376+
meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
377377
assert.NoError(t, err)
378378
assert.NotNil(t, meta)
379379
})
@@ -417,7 +417,7 @@ func TestAPILFSUpload(t *testing.T) {
417417
assert.NoError(t, err)
418418
assert.True(t, exist)
419419

420-
meta, err := repo.GetLFSMetaObjectByOid(p.Oid)
420+
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
421421
assert.NoError(t, err)
422422
assert.NotNil(t, meta)
423423
})
@@ -432,7 +432,7 @@ func TestAPILFSVerify(t *testing.T) {
432432

433433
content := []byte("dummy3")
434434
oid := storeObjectInRepo(t, repo.ID, &content)
435-
defer repo.RemoveLFSMetaObjectByOid(oid)
435+
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
436436

437437
session := loginUser(t, "user2")
438438

integrations/lfs_getobject_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, exp
4343
repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
4444
assert.NoError(t, err)
4545
oid := storeObjectInRepo(t, repo.ID, content)
46-
defer repo.RemoveLFSMetaObjectByOid(oid)
46+
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
4747

4848
session := loginUser(t, "user2")
4949

models/access.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func updateUserAccess(accessMap map[int64]*userAccess, user *user_model.User, mo
8181
}
8282

8383
// FIXME: do cross-comparison so reduce deletions and additions to the minimum?
84-
func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAccess) (err error) {
84+
func refreshAccesses(e db.Engine, repo *Repository, accessMap map[int64]*userAccess) (err error) {
8585
minMode := perm.AccessModeRead
8686
if !repo.IsPrivate {
8787
minMode = perm.AccessModeWrite
@@ -115,8 +115,8 @@ func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAc
115115
}
116116

117117
// refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
118-
func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[int64]*userAccess) error {
119-
collaborators, err := repo.getCollaborators(e, db.ListOptions{})
118+
func refreshCollaboratorAccesses(e db.Engine, repoID int64, accessMap map[int64]*userAccess) error {
119+
collaborators, err := getCollaborators(e, repoID, db.ListOptions{})
120120
if err != nil {
121121
return fmt.Errorf("getCollaborations: %v", err)
122122
}
@@ -132,7 +132,7 @@ func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[i
132132
// recalculateTeamAccesses recalculates new accesses for teams of an organization
133133
// except the team whose ID is given. It is used to assign a team ID when
134134
// remove repository from that team.
135-
func (repo *Repository) recalculateTeamAccesses(e db.Engine, ignTeamID int64) (err error) {
135+
func recalculateTeamAccesses(e db.Engine, repo *Repository, ignTeamID int64) (err error) {
136136
accessMap := make(map[int64]*userAccess, 20)
137137

138138
if err = repo.getOwner(e); err != nil {
@@ -141,7 +141,7 @@ func (repo *Repository) recalculateTeamAccesses(e db.Engine, ignTeamID int64) (e
141141
return fmt.Errorf("owner is not an organization: %d", repo.OwnerID)
142142
}
143143

144-
if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
144+
if err = refreshCollaboratorAccesses(e, repo.ID, accessMap); err != nil {
145145
return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
146146
}
147147

@@ -171,19 +171,19 @@ func (repo *Repository) recalculateTeamAccesses(e db.Engine, ignTeamID int64) (e
171171
}
172172
}
173173

174-
return repo.refreshAccesses(e, accessMap)
174+
return refreshAccesses(e, repo, accessMap)
175175
}
176176

177177
// recalculateUserAccess recalculates new access for a single user
178178
// Usable if we know access only affected one user
179-
func (repo *Repository) recalculateUserAccess(e db.Engine, uid int64) (err error) {
179+
func recalculateUserAccess(e db.Engine, repo *Repository, uid int64) (err error) {
180180
minMode := perm.AccessModeRead
181181
if !repo.IsPrivate {
182182
minMode = perm.AccessModeWrite
183183
}
184184

185185
accessMode := perm.AccessModeNone
186-
collaborator, err := repo.getCollaboration(e, uid)
186+
collaborator, err := getCollaboration(e, repo.ID, uid)
187187
if err != nil {
188188
return err
189189
} else if collaborator != nil {
@@ -223,19 +223,19 @@ func (repo *Repository) recalculateUserAccess(e db.Engine, uid int64) (err error
223223
return nil
224224
}
225225

226-
func (repo *Repository) recalculateAccesses(e db.Engine) error {
226+
func recalculateAccesses(e db.Engine, repo *Repository) error {
227227
if repo.Owner.IsOrganization() {
228-
return repo.recalculateTeamAccesses(e, 0)
228+
return recalculateTeamAccesses(e, repo, 0)
229229
}
230230

231231
accessMap := make(map[int64]*userAccess, 20)
232-
if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil {
232+
if err := refreshCollaboratorAccesses(e, repo.ID, accessMap); err != nil {
233233
return fmt.Errorf("refreshCollaboratorAccesses: %v", err)
234234
}
235-
return repo.refreshAccesses(e, accessMap)
235+
return refreshAccesses(e, repo, accessMap)
236236
}
237237

238238
// RecalculateAccesses recalculates all accesses for repository.
239-
func (repo *Repository) RecalculateAccesses() error {
240-
return repo.recalculateAccesses(db.GetEngine(db.DefaultContext))
239+
func RecalculateAccesses(repo *Repository) error {
240+
return recalculateAccesses(db.GetEngine(db.DefaultContext), repo)
241241
}

models/access_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
100100

101101
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 2, RepoID: 3})
102102
assert.NoError(t, err)
103-
assert.NoError(t, repo1.RecalculateAccesses())
103+
assert.NoError(t, RecalculateAccesses(repo1))
104104

105105
access := &Access{UserID: 2, RepoID: 3}
106106
has, err := db.GetEngine(db.DefaultContext).Get(access)
@@ -117,7 +117,7 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
117117

118118
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 4, RepoID: 4})
119119
assert.NoError(t, err)
120-
assert.NoError(t, repo1.RecalculateAccesses())
120+
assert.NoError(t, RecalculateAccesses(repo1))
121121

122122
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 4, RepoID: 4})
123123
assert.NoError(t, err)

models/branches.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
102102
}
103103

104104
// IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
105-
func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permissionInRepo Permission) bool {
105+
func IsUserMergeWhitelisted(protectBranch *ProtectedBranch, userID int64, permissionInRepo Permission) bool {
106106
if !protectBranch.EnableMergeWhitelist {
107107
// Then we need to fall back on whether the user has write permission
108108
return permissionInRepo.CanWrite(unit.TypeCode)
@@ -125,11 +125,11 @@ func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permi
125125
}
126126

127127
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
128-
func (protectBranch *ProtectedBranch) IsUserOfficialReviewer(user *user_model.User) (bool, error) {
129-
return protectBranch.isUserOfficialReviewer(db.GetEngine(db.DefaultContext), user)
128+
func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
129+
return isUserOfficialReviewer(db.GetEngine(db.DefaultContext), protectBranch, user)
130130
}
131131

132-
func (protectBranch *ProtectedBranch) isUserOfficialReviewer(e db.Engine, user *user_model.User) (bool, error) {
132+
func isUserOfficialReviewer(e db.Engine, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
133133
repo, err := getRepositoryByID(e, protectBranch.RepoID)
134134
if err != nil {
135135
return false, err
@@ -393,20 +393,20 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts
393393
}
394394

395395
// GetProtectedBranches get all protected branches
396-
func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
396+
func GetProtectedBranches(repoID int64) ([]*ProtectedBranch, error) {
397397
protectedBranches := make([]*ProtectedBranch, 0)
398-
return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
398+
return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repoID})
399399
}
400400

401401
// GetBranchProtection get the branch protection of a branch
402-
func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch, error) {
403-
return GetProtectedBranchBy(repo.ID, branchName)
402+
func GetBranchProtection(repoID int64, branchName string) (*ProtectedBranch, error) {
403+
return GetProtectedBranchBy(repoID, branchName)
404404
}
405405

406406
// IsProtectedBranch checks if branch is protected
407-
func (repo *Repository) IsProtectedBranch(branchName string) (bool, error) {
407+
func IsProtectedBranch(repoID int64, branchName string) (bool, error) {
408408
protectedBranch := &ProtectedBranch{
409-
RepoID: repo.ID,
409+
RepoID: repoID,
410410
BranchName: branchName,
411411
}
412412

@@ -427,7 +427,7 @@ func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []
427427

428428
whitelist = make([]int64, 0, len(newWhitelist))
429429
for _, userID := range newWhitelist {
430-
if reader, err := repo.IsReader(userID); err != nil {
430+
if reader, err := IsRepoReader(repo, userID); err != nil {
431431
return nil, err
432432
} else if !reader {
433433
continue
@@ -491,9 +491,9 @@ func updateTeamWhitelist(repo *Repository, currentWhitelist, newWhitelist []int6
491491
}
492492

493493
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
494-
func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
494+
func DeleteProtectedBranch(repoID, id int64) (err error) {
495495
protectedBranch := &ProtectedBranch{
496-
RepoID: repo.ID,
496+
RepoID: repoID,
497497
ID: id,
498498
}
499499

@@ -518,28 +518,28 @@ type DeletedBranch struct {
518518
}
519519

520520
// AddDeletedBranch adds a deleted branch to the database
521-
func (repo *Repository) AddDeletedBranch(branchName, commit string, deletedByID int64) error {
521+
func AddDeletedBranch(repoID int64, branchName, commit string, deletedByID int64) error {
522522
deletedBranch := &DeletedBranch{
523-
RepoID: repo.ID,
523+
RepoID: repoID,
524524
Name: branchName,
525525
Commit: commit,
526526
DeletedByID: deletedByID,
527527
}
528528

529-
_, err := db.GetEngine(db.DefaultContext).InsertOne(deletedBranch)
529+
_, err := db.GetEngine(db.DefaultContext).Insert(deletedBranch)
530530
return err
531531
}
532532

533533
// GetDeletedBranches returns all the deleted branches
534-
func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
534+
func GetDeletedBranches(repoID int64) ([]*DeletedBranch, error) {
535535
deletedBranches := make([]*DeletedBranch, 0)
536-
return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches)
536+
return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches)
537537
}
538538

539539
// GetDeletedBranchByID get a deleted branch by its ID
540-
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
540+
func GetDeletedBranchByID(repoID, id int64) (*DeletedBranch, error) {
541541
deletedBranch := &DeletedBranch{}
542-
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repo.ID).And("id = ?", id).Get(deletedBranch)
542+
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch)
543543
if err != nil {
544544
return nil, err
545545
}
@@ -549,10 +549,10 @@ func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
549549
return deletedBranch, nil
550550
}
551551

552-
// RemoveDeletedBranch removes a deleted branch from the database
553-
func (repo *Repository) RemoveDeletedBranch(id int64) (err error) {
552+
// RemoveDeletedBranchByID removes a deleted branch from the database
553+
func RemoveDeletedBranchByID(repoID, id int64) (err error) {
554554
deletedBranch := &DeletedBranch{
555-
RepoID: repo.ID,
555+
RepoID: repoID,
556556
ID: id,
557557
}
558558

@@ -575,8 +575,8 @@ func (deletedBranch *DeletedBranch) LoadUser() {
575575
deletedBranch.DeletedBy = user
576576
}
577577

578-
// RemoveDeletedBranch removes all deleted branches
579-
func RemoveDeletedBranch(repoID int64, branch string) error {
578+
// RemoveDeletedBranchByName removes all deleted branches
579+
func RemoveDeletedBranchByName(repoID int64, branch string) error {
580580
_, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
581581
return err
582582
}
@@ -615,7 +615,7 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist
615615
}
616616

617617
// RenameBranch rename a branch
618-
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) {
618+
func RenameBranch(repo *Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
619619
ctx, committer, err := db.TxContext()
620620
if err != nil {
621621
return err

models/branches_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ func TestAddDeletedBranch(t *testing.T) {
1717
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
1818
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
1919

20-
assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
21-
assert.NoError(t, repo.AddDeletedBranch("test", "5655464564554545466464656", int64(1)))
20+
assert.Error(t, AddDeletedBranch(repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
21+
assert.NoError(t, AddDeletedBranch(repo.ID, "test", "5655464564554545466464656", int64(1)))
2222
}
2323

2424
func TestGetDeletedBranches(t *testing.T) {
2525
assert.NoError(t, unittest.PrepareTestDatabase())
2626
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
2727

28-
branches, err := repo.GetDeletedBranches()
28+
branches, err := GetDeletedBranches(repo.ID)
2929
assert.NoError(t, err)
3030
assert.Len(t, branches, 2)
3131
}
@@ -62,7 +62,7 @@ func TestRemoveDeletedBranch(t *testing.T) {
6262

6363
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
6464

65-
err := repo.RemoveDeletedBranch(1)
65+
err := RemoveDeletedBranchByID(repo.ID, 1)
6666
assert.NoError(t, err)
6767
unittest.AssertNotExistsBean(t, firstBranch)
6868
unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2})
@@ -71,7 +71,7 @@ func TestRemoveDeletedBranch(t *testing.T) {
7171
func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
7272
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
7373

74-
deletedBranch, err := repo.GetDeletedBranchByID(branch.ID)
74+
deletedBranch, err := GetDeletedBranchByID(repo.ID, branch.ID)
7575
assert.NoError(t, err)
7676
assert.Equal(t, branch.ID, deletedBranch.ID)
7777
assert.Equal(t, branch.Name, deletedBranch.Name)
@@ -104,7 +104,7 @@ func TestRenameBranch(t *testing.T) {
104104
}, WhitelistOptions{})
105105
assert.NoError(t, err)
106106

107-
assert.NoError(t, repo1.RenameBranch("master", "main", func(isDefault bool) error {
107+
assert.NoError(t, RenameBranch(repo1, "master", "main", func(isDefault bool) error {
108108
_isDefault = isDefault
109109
return nil
110110
}))
@@ -138,7 +138,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
138138
// is actually on repo with ID 1.
139139
repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
140140

141-
deletedBranch, err := repo2.GetDeletedBranchByID(1)
141+
deletedBranch, err := GetDeletedBranchByID(repo2.ID, 1)
142142

143143
// Expect no error, and the returned branch is nil.
144144
assert.NoError(t, err)
@@ -148,7 +148,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
148148
// This should return the deletedBranch.
149149
repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
150150

151-
deletedBranch, err = repo1.GetDeletedBranchByID(1)
151+
deletedBranch, err = GetDeletedBranchByID(repo1.ID, 1)
152152

153153
// Expect no error, and the returned branch to be not nil.
154154
assert.NoError(t, err)

0 commit comments

Comments
 (0)