aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-12-10 10:46:31 +0800
committerGitHub <noreply@github.com>2022-12-10 10:46:31 +0800
commit68704532c28cf09db96c988291b2f82c5e615984 (patch)
treec6537092dc11054f96b202fdb957755ed116cd99 /models/repo
parent097d4e30b180eef30600beef2c08095e2571319c (diff)
downloadgitea-68704532c28cf09db96c988291b2f82c5e615984.tar.gz
gitea-68704532c28cf09db96c988291b2f82c5e615984.zip
Rename almost all Ctx functions (#22071)
Diffstat (limited to 'models/repo')
-rw-r--r--models/repo/collaboration.go69
-rw-r--r--models/repo/collaboration_test.go6
-rw-r--r--models/repo/issue.go23
-rw-r--r--models/repo/release.go4
-rw-r--r--models/repo/repo.go22
5 files changed, 45 insertions, 79 deletions
diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go
index bd49b9aec9..29bcab70f3 100644
--- a/models/repo/collaboration.go
+++ b/models/repo/collaboration.go
@@ -99,55 +99,42 @@ func getCollaborations(ctx context.Context, repoID int64, listOptions db.ListOpt
}
// ChangeCollaborationAccessMode sets new access mode for the collaboration.
-func ChangeCollaborationAccessModeCtx(ctx context.Context, repo *Repository, uid int64, mode perm.AccessMode) error {
+func ChangeCollaborationAccessMode(ctx context.Context, repo *Repository, uid int64, mode perm.AccessMode) error {
// Discard invalid input
if mode <= perm.AccessModeNone || mode > perm.AccessModeOwner {
return nil
}
- e := db.GetEngine(ctx)
-
- collaboration := &Collaboration{
- RepoID: repo.ID,
- UserID: uid,
- }
- has, err := e.Get(collaboration)
- if err != nil {
- return fmt.Errorf("get collaboration: %w", err)
- } else if !has {
- return nil
- }
-
- if collaboration.Mode == mode {
- return nil
- }
- collaboration.Mode = mode
-
- if _, err = e.
- ID(collaboration.ID).
- Cols("mode").
- Update(collaboration); err != nil {
- return fmt.Errorf("update collaboration: %w", err)
- } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
- return fmt.Errorf("update access table: %w", err)
- }
+ return db.AutoTx(ctx, func(ctx context.Context) error {
+ e := db.GetEngine(ctx)
- return nil
-}
-
-// ChangeCollaborationAccessMode sets new access mode for the collaboration.
-func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.AccessMode) error {
- ctx, committer, err := db.TxContext(db.DefaultContext)
- if err != nil {
- return err
- }
- defer committer.Close()
+ collaboration := &Collaboration{
+ RepoID: repo.ID,
+ UserID: uid,
+ }
+ has, err := e.Get(collaboration)
+ if err != nil {
+ return fmt.Errorf("get collaboration: %w", err)
+ } else if !has {
+ return nil
+ }
- if err := ChangeCollaborationAccessModeCtx(ctx, repo, uid, mode); err != nil {
- return err
- }
+ if collaboration.Mode == mode {
+ return nil
+ }
+ collaboration.Mode = mode
+
+ if _, err = e.
+ ID(collaboration.ID).
+ Cols("mode").
+ Update(collaboration); err != nil {
+ return fmt.Errorf("update collaboration: %w", err)
+ } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
+ return fmt.Errorf("update access table: %w", err)
+ }
- return committer.Commit()
+ return nil
+ })
}
// IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
diff --git a/models/repo/collaboration_test.go b/models/repo/collaboration_test.go
index d240e9cbef..0a6444de85 100644
--- a/models/repo/collaboration_test.go
+++ b/models/repo/collaboration_test.go
@@ -54,7 +54,7 @@ func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
- assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, 4, perm.AccessModeAdmin))
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessModeAdmin))
collaboration := unittest.AssertExistsAndLoadBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4})
assert.EqualValues(t, perm.AccessModeAdmin, collaboration.Mode)
@@ -62,9 +62,9 @@ func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
access := unittest.AssertExistsAndLoadBean(t, &access_model.Access{UserID: 4, RepoID: repo.ID})
assert.EqualValues(t, perm.AccessModeAdmin, access.Mode)
- assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, 4, perm.AccessModeAdmin))
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessModeAdmin))
- assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, unittest.NonexistentID, perm.AccessModeAdmin))
+ assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, unittest.NonexistentID, perm.AccessModeAdmin))
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID})
}
diff --git a/models/repo/issue.go b/models/repo/issue.go
index e27179caf5..6f6b565a00 100644
--- a/models/repo/issue.go
+++ b/models/repo/issue.go
@@ -6,7 +6,6 @@ package repo
import (
"context"
- "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -26,44 +25,34 @@ func (repo *Repository) CanEnableTimetracker() bool {
}
// IsTimetrackerEnabled returns whether or not the timetracker is enabled. It returns the default value from config if an error occurs.
-func (repo *Repository) IsTimetrackerEnabled() bool { // Notice: It will be used in template so don't remove directly
- return repo.IsTimetrackerEnabledCtx(db.DefaultContext)
-}
-
-// IsTimetrackerEnabledCtx returns whether or not the timetracker is enabled. It returns the default value from config if an error occurs.
-func (repo *Repository) IsTimetrackerEnabledCtx(ctx context.Context) bool {
+func (repo *Repository) IsTimetrackerEnabled(ctx context.Context) bool {
if !setting.Service.EnableTimetracking {
return false
}
var u *RepoUnit
var err error
- if u, err = repo.GetUnitCtx(ctx, unit.TypeIssues); err != nil {
+ if u, err = repo.GetUnit(ctx, unit.TypeIssues); err != nil {
return setting.Service.DefaultEnableTimetracking
}
return u.IssuesConfig().EnableTimetracker
}
// AllowOnlyContributorsToTrackTime returns value of IssuesConfig or the default value
-func (repo *Repository) AllowOnlyContributorsToTrackTime() bool {
+func (repo *Repository) AllowOnlyContributorsToTrackTime(ctx context.Context) bool {
var u *RepoUnit
var err error
- if u, err = repo.GetUnit(unit.TypeIssues); err != nil {
+ if u, err = repo.GetUnit(ctx, unit.TypeIssues); err != nil {
return setting.Service.DefaultAllowOnlyContributorsToTrackTime
}
return u.IssuesConfig().AllowOnlyContributorsToTrackTime
}
// IsDependenciesEnabled returns if dependencies are enabled and returns the default setting if not set.
-func (repo *Repository) IsDependenciesEnabled() bool {
- return repo.IsDependenciesEnabledCtx(db.DefaultContext)
-}
-
-// IsDependenciesEnabledCtx returns if dependencies are enabled and returns the default setting if not set.
-func (repo *Repository) IsDependenciesEnabledCtx(ctx context.Context) bool {
+func (repo *Repository) IsDependenciesEnabled(ctx context.Context) bool {
var u *RepoUnit
var err error
- if u, err = repo.GetUnitCtx(ctx, unit.TypeIssues); err != nil {
+ if u, err = repo.GetUnit(ctx, unit.TypeIssues); err != nil {
log.Trace("%s", err)
return setting.Service.DefaultEnableDependencies
}
diff --git a/models/repo/release.go b/models/repo/release.go
index c6cb755344..25eba10e05 100644
--- a/models/repo/release.go
+++ b/models/repo/release.go
@@ -286,8 +286,8 @@ func GetReleasesByRepoIDAndNames(ctx context.Context, repoID int64, tagNames []s
}
// GetReleaseCountByRepoID returns the count of releases of repository
-func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
- return db.GetEngine(db.DefaultContext).Where(opts.toConds(repoID)).Count(&Release{})
+func GetReleaseCountByRepoID(ctx context.Context, repoID int64, opts FindReleasesOptions) (int64, error) {
+ return db.GetEngine(ctx).Where(opts.toConds(repoID)).Count(&Release{})
}
type releaseMetaSearch struct {
diff --git a/models/repo/repo.go b/models/repo/repo.go
index 7cb7138523..e5e1ac43b4 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -315,12 +315,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
}
// UnitEnabled if this repository has the given unit enabled
-func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) { // Notice: Don't remove this function directly, because it has been used in go template.
- return repo.UnitEnabledCtx(db.DefaultContext, tp)
-}
-
-// UnitEnabled if this repository has the given unit enabled
-func (repo *Repository) UnitEnabledCtx(ctx context.Context, tp unit.Type) bool {
+func (repo *Repository) UnitEnabled(ctx context.Context, tp unit.Type) bool {
if err := repo.LoadUnits(ctx); err != nil {
log.Warn("Error loading repository (ID: %d) units: %s", repo.ID, err.Error())
}
@@ -333,8 +328,8 @@ func (repo *Repository) UnitEnabledCtx(ctx context.Context, tp unit.Type) bool {
}
// MustGetUnit always returns a RepoUnit object
-func (repo *Repository) MustGetUnit(tp unit.Type) *RepoUnit {
- ru, err := repo.GetUnit(tp)
+func (repo *Repository) MustGetUnit(ctx context.Context, tp unit.Type) *RepoUnit {
+ ru, err := repo.GetUnit(ctx, tp)
if err == nil {
return ru
}
@@ -367,12 +362,7 @@ func (repo *Repository) MustGetUnit(tp unit.Type) *RepoUnit {
}
// GetUnit returns a RepoUnit object
-func (repo *Repository) GetUnit(tp unit.Type) (*RepoUnit, error) {
- return repo.GetUnitCtx(db.DefaultContext, tp)
-}
-
-// GetUnitCtx returns a RepoUnit object
-func (repo *Repository) GetUnitCtx(ctx context.Context, tp unit.Type) (*RepoUnit, error) {
+func (repo *Repository) GetUnit(ctx context.Context, tp unit.Type) (*RepoUnit, error) {
if err := repo.LoadUnits(ctx); err != nil {
return nil, err
}
@@ -419,7 +409,7 @@ func (repo *Repository) ComposeMetas() map[string]string {
"mode": "comment",
}
- unit, err := repo.GetUnit(unit.TypeExternalTracker)
+ unit, err := repo.GetUnit(db.DefaultContext, unit.TypeExternalTracker)
if err == nil {
metas["format"] = unit.ExternalTrackerConfig().ExternalTrackerFormat
switch unit.ExternalTrackerConfig().ExternalTrackerStyle {
@@ -518,7 +508,7 @@ func (repo *Repository) CanEnablePulls() bool {
// AllowsPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
func (repo *Repository) AllowsPulls() bool {
- return repo.CanEnablePulls() && repo.UnitEnabled(unit.TypePullRequests)
+ return repo.CanEnablePulls() && repo.UnitEnabled(db.DefaultContext, unit.TypePullRequests)
}
// CanEnableEditor returns true if repository meets the requirements of web editor.