aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo
diff options
context:
space:
mode:
authorJakobDev <jakobdev@gmx.de>2023-09-29 14:12:54 +0200
committerGitHub <noreply@github.com>2023-09-29 12:12:54 +0000
commitcf0df023be06c8acec4fc8fb05eab1d1c2e52fd1 (patch)
tree4fd233354202942b597f3591a22e5ea14fe7d0eb /models/repo
parent3945c26722dececf2433107c47821238dae7e3c2 (diff)
downloadgitea-cf0df023be06c8acec4fc8fb05eab1d1c2e52fd1.tar.gz
gitea-cf0df023be06c8acec4fc8fb05eab1d1c2e52fd1.zip
More `db.DefaultContext` refactor (#27265)
Part of #27065 This PR touches functions used in templates. As templates are not static typed, errors are harder to find, but I hope I catch it all. I think some tests from other persons do not hurt.
Diffstat (limited to 'models/repo')
-rw-r--r--models/repo/collaboration.go10
-rw-r--r--models/repo/collaboration_test.go16
-rw-r--r--models/repo/mirror.go12
3 files changed, 19 insertions, 19 deletions
diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go
index 7989e5bdf9..2018ae2a7d 100644
--- a/models/repo/collaboration.go
+++ b/models/repo/collaboration.go
@@ -62,8 +62,8 @@ func GetCollaborators(ctx context.Context, repoID int64, listOptions db.ListOpti
}
// CountCollaborators returns total number of collaborators for a repository
-func CountCollaborators(repoID int64) (int64, error) {
- return db.GetEngine(db.DefaultContext).Where("repo_id = ? ", repoID).Count(&Collaboration{})
+func CountCollaborators(ctx context.Context, repoID int64) (int64, error) {
+ return db.GetEngine(ctx).Where("repo_id = ? ", repoID).Count(&Collaboration{})
}
// GetCollaboration get collaboration for a repository id with a user id
@@ -138,11 +138,11 @@ func ChangeCollaborationAccessMode(ctx context.Context, repo *Repository, uid in
}
// IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
-func IsOwnerMemberCollaborator(repo *Repository, userID int64) (bool, error) {
+func IsOwnerMemberCollaborator(ctx context.Context, repo *Repository, userID int64) (bool, error) {
if repo.OwnerID == userID {
return true, nil
}
- teamMember, err := db.GetEngine(db.DefaultContext).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
+ teamMember, err := db.GetEngine(ctx).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
Where("team_repo.repo_id = ?", repo.ID).
And("team_unit.`type` = ?", unit.TypeCode).
@@ -154,5 +154,5 @@ func IsOwnerMemberCollaborator(repo *Repository, userID int64) (bool, error) {
return true, nil
}
- return db.GetEngine(db.DefaultContext).Get(&Collaboration{RepoID: repo.ID, UserID: userID})
+ return db.GetEngine(ctx).Get(&Collaboration{RepoID: repo.ID, UserID: userID})
}
diff --git a/models/repo/collaboration_test.go b/models/repo/collaboration_test.go
index a29dfe99a5..38114c307f 100644
--- a/models/repo/collaboration_test.go
+++ b/models/repo/collaboration_test.go
@@ -89,17 +89,17 @@ func TestRepository_CountCollaborators(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
- count, err := repo_model.CountCollaborators(repo1.ID)
+ count, err := repo_model.CountCollaborators(db.DefaultContext, repo1.ID)
assert.NoError(t, err)
assert.EqualValues(t, 2, count)
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
- count, err = repo_model.CountCollaborators(repo2.ID)
+ count, err = repo_model.CountCollaborators(db.DefaultContext, repo2.ID)
assert.NoError(t, err)
assert.EqualValues(t, 2, count)
// Non-existent repository.
- count, err = repo_model.CountCollaborators(unittest.NonexistentID)
+ count, err = repo_model.CountCollaborators(db.DefaultContext, unittest.NonexistentID)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
}
@@ -110,31 +110,31 @@ func TestRepository_IsOwnerMemberCollaborator(t *testing.T) {
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
// Organisation owner.
- actual, err := repo_model.IsOwnerMemberCollaborator(repo1, 2)
+ actual, err := repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 2)
assert.NoError(t, err)
assert.True(t, actual)
// Team member.
- actual, err = repo_model.IsOwnerMemberCollaborator(repo1, 4)
+ actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 4)
assert.NoError(t, err)
assert.True(t, actual)
// Normal user.
- actual, err = repo_model.IsOwnerMemberCollaborator(repo1, 1)
+ actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 1)
assert.NoError(t, err)
assert.False(t, actual)
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
// Collaborator.
- actual, err = repo_model.IsOwnerMemberCollaborator(repo2, 4)
+ actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo2, 4)
assert.NoError(t, err)
assert.True(t, actual)
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 15})
// Repository owner.
- actual, err = repo_model.IsOwnerMemberCollaborator(repo3, 2)
+ actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo3, 2)
assert.NoError(t, err)
assert.True(t, actual)
}
diff --git a/models/repo/mirror.go b/models/repo/mirror.go
index fffc7577c7..be7b785612 100644
--- a/models/repo/mirror.go
+++ b/models/repo/mirror.go
@@ -47,12 +47,12 @@ func (m *Mirror) BeforeInsert() {
}
// GetRepository returns the repository.
-func (m *Mirror) GetRepository() *Repository {
+func (m *Mirror) GetRepository(ctx context.Context) *Repository {
if m.Repo != nil {
return m.Repo
}
var err error
- m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
+ m.Repo, err = GetRepositoryByID(ctx, m.RepoID)
if err != nil {
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
}
@@ -99,14 +99,14 @@ func TouchMirror(ctx context.Context, m *Mirror) error {
}
// DeleteMirrorByRepoID deletes a mirror by repoID
-func DeleteMirrorByRepoID(repoID int64) error {
- _, err := db.GetEngine(db.DefaultContext).Delete(&Mirror{RepoID: repoID})
+func DeleteMirrorByRepoID(ctx context.Context, repoID int64) error {
+ _, err := db.GetEngine(ctx).Delete(&Mirror{RepoID: repoID})
return err
}
// MirrorsIterate iterates all mirror repositories.
-func MirrorsIterate(limit int, f func(idx int, bean any) error) error {
- sess := db.GetEngine(db.DefaultContext).
+func MirrorsIterate(ctx context.Context, limit int, f func(idx int, bean any) error) error {
+ sess := db.GetEngine(ctx).
Where("next_update_unix<=?", time.Now().Unix()).
And("next_update_unix!=0").
OrderBy("updated_unix ASC")