diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /modules | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/repo.go | 14 | ||||
-rw-r--r-- | modules/convert/convert.go | 2 | ||||
-rw-r--r-- | modules/convert/issue.go | 2 | ||||
-rw-r--r-- | modules/convert/issue_comment.go | 3 | ||||
-rw-r--r-- | modules/convert/repository.go | 2 | ||||
-rw-r--r-- | modules/doctor/authorizedkeys.go | 2 | ||||
-rw-r--r-- | modules/gitgraph/graph_models.go | 2 | ||||
-rw-r--r-- | modules/indexer/code/git.go | 2 | ||||
-rw-r--r-- | modules/indexer/code/indexer.go | 2 | ||||
-rw-r--r-- | modules/indexer/issues/indexer.go | 2 | ||||
-rw-r--r-- | modules/indexer/stats/db.go | 2 | ||||
-rw-r--r-- | modules/indexer/stats/indexer_test.go | 3 | ||||
-rw-r--r-- | modules/ssh/ssh.go | 4 |
13 files changed, 23 insertions, 19 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index eb773dfb2e..539b111f15 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -16,6 +16,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -116,7 +117,7 @@ type CanCommitToBranchResults struct { // CanCommitToBranch returns true if repository is editable and user has proper access level // and branch is not protected for push func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) { - protectedBranch, err := models.GetProtectedBranchBy(r.Repository.ID, r.BranchName) + protectedBranch, err := models.GetProtectedBranchBy(ctx, r.Repository.ID, r.BranchName) if err != nil { return CanCommitToBranchResults{}, err } @@ -159,7 +160,7 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *user_model.Use // Checking for following: // 1. Is timetracker enabled // 2. Is the user a contributor, admin, poster or assignee and do the repository policies require this? - isAssigned, _ := models.IsUserAssignedToIssue(issue, user) + isAssigned, _ := models.IsUserAssignedToIssue(db.DefaultContext, issue, user) return r.Repository.IsTimetrackerEnabled() && (!r.Repository.AllowOnlyContributorsToTrackTime() || r.Permission.CanWriteIssuesOrPulls(issue.IsPull) || issue.IsPoster(user.ID) || isAssigned) } @@ -278,7 +279,7 @@ func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) { // RetrieveTemplateRepo retrieves template repository used to generate this repository func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) { // Non-generated repository will not return error in this method. - templateRepo, err := repo_model.GetTemplateRepo(repo) + templateRepo, err := repo_model.GetTemplateRepo(ctx, repo) if err != nil { if repo_model.IsErrRepoNotExist(err) { repo.TemplateID = 0 @@ -385,11 +386,12 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { return } if finishedMigrating { - ctx.Repo.Mirror, err = repo_model.GetMirrorByRepoID(repo.ID) + ctx.Repo.Mirror, err = repo_model.GetMirrorByRepoID(ctx, repo.ID) if err != nil { ctx.ServerError("GetMirrorByRepoID", err) return } + ctx.Repo.Mirror.Repo = ctx.Repo.Repository ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval ctx.Data["Mirror"] = ctx.Repo.Mirror @@ -451,7 +453,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { owner = ctx.Doer } else { - owner, err = user_model.GetUserByName(userName) + owner, err = user_model.GetUserByName(ctx, userName) if err != nil { if user_model.IsErrUserNotExist(err) { if ctx.FormString("go-get") == "1" { @@ -587,7 +589,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { if ctx.IsSigned { ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx.Doer.ID, repo.ID) - ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx.Doer.ID, repo.ID) + ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, repo.ID) } if repo.IsFork { diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 53357e7505..67b3902cd7 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -340,7 +340,7 @@ func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) { if loadOrgs { apiOrg, ok := cache[teams[i].OrgID] if !ok { - org, err := organization.GetOrgByID(teams[i].OrgID) + org, err := organization.GetOrgByID(db.DefaultContext, teams[i].OrgID) if err != nil { return nil, err } diff --git a/modules/convert/issue.go b/modules/convert/issue.go index bf116e2283..a4512e424f 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -72,7 +72,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { apiIssue.Milestone = ToAPIMilestone(issue.Milestone) } - if err := issue.LoadAssignees(); err != nil { + if err := issue.LoadAssignees(db.DefaultContext); err != nil { return &api.Issue{} } if len(issue.Assignees) > 0 { diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go index 6d72849bca..eaa7f64ea3 100644 --- a/modules/convert/issue_comment.go +++ b/modules/convert/issue_comment.go @@ -6,6 +6,7 @@ package convert import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" @@ -113,7 +114,7 @@ func ToTimelineComment(c *models.Comment, doer *user_model.User) *api.TimelineCo } if c.RefCommentID != 0 { - com, err := models.GetCommentByID(c.RefCommentID) + com, err := models.GetCommentByID(db.DefaultContext, c.RefCommentID) if err != nil { log.Error("GetCommentByID(%d): %v", c.RefCommentID, err) return nil diff --git a/modules/convert/repository.go b/modules/convert/repository.go index b813d69699..eb6bb37707 100644 --- a/modules/convert/repository.go +++ b/modules/convert/repository.go @@ -104,7 +104,7 @@ func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent boo var mirrorUpdated time.Time if repo.IsMirror { var err error - repo.Mirror, err = repo_model.GetMirrorByRepoID(repo.ID) + repo.Mirror, err = repo_model.GetMirrorByRepoID(db.DefaultContext, repo.ID) if err == nil { mirrorInterval = repo.Mirror.Interval.String() mirrorUpdated = repo.Mirror.UpdatedUnix.AsTime() diff --git a/modules/doctor/authorizedkeys.go b/modules/doctor/authorizedkeys.go index 18e7a3cbf4..34dfe939d3 100644 --- a/modules/doctor/authorizedkeys.go +++ b/modules/doctor/authorizedkeys.go @@ -54,7 +54,7 @@ func checkAuthorizedKeys(ctx context.Context, logger log.Logger, autofix bool) e // now we regenerate and check if there are any lines missing regenerated := &bytes.Buffer{} - if err := asymkey_model.RegeneratePublicKeys(regenerated); err != nil { + if err := asymkey_model.RegeneratePublicKeys(ctx, regenerated); err != nil { logger.Critical("Unable to regenerate authorized_keys file. ERROR: %v", err) return fmt.Errorf("Unable to regenerate authorized_keys file. ERROR: %v", err) } diff --git a/modules/gitgraph/graph_models.go b/modules/gitgraph/graph_models.go index 653384252d..551e56f63e 100644 --- a/modules/gitgraph/graph_models.go +++ b/modules/gitgraph/graph_models.go @@ -120,7 +120,7 @@ func (graph *Graph) LoadAndProcessCommits(repository *repo_model.Repository, git return models.IsOwnerMemberCollaborator(repository, user.ID) }, &keyMap) - statuses, _, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), db.ListOptions{}) + statuses, _, err := models.GetLatestCommitStatus(db.DefaultContext, repository.ID, c.Commit.ID.String(), db.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } else { diff --git a/modules/indexer/code/git.go b/modules/indexer/code/git.go index 60018af20c..66d76377ad 100644 --- a/modules/indexer/code/git.go +++ b/modules/indexer/code/git.go @@ -38,7 +38,7 @@ func getDefaultBranchSha(ctx context.Context, repo *repo_model.Repository) (stri // getRepoChanges returns changes to repo since last indexer update func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision string) (*repoChanges, error) { - status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeCode) + status, err := repo_model.GetIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeCode) if err != nil { return nil, err } diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index f15b8d8651..9845ade3dd 100644 --- a/modules/indexer/code/indexer.go +++ b/modules/indexer/code/indexer.go @@ -108,7 +108,7 @@ func index(ctx context.Context, indexer Indexer, repoID int64) error { return err } - return repo_model.UpdateIndexerStatus(repo, repo_model.RepoIndexerTypeCode, sha) + return repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeCode, sha) } // Init initialize the repo indexer diff --git a/modules/indexer/issues/indexer.go b/modules/indexer/issues/indexer.go index d4df4f8a4f..7adc938dcc 100644 --- a/modules/indexer/issues/indexer.go +++ b/modules/indexer/issues/indexer.go @@ -362,7 +362,7 @@ func UpdateIssueIndexer(issue *models.Issue) { // DeleteRepoIssueIndexer deletes repo's all issues indexes func DeleteRepoIssueIndexer(repo *repo_model.Repository) { var ids []int64 - ids, err := models.GetIssueIDsByRepoID(repo.ID) + ids, err := models.GetIssueIDsByRepoID(db.DefaultContext, repo.ID) if err != nil { log.Error("getIssueIDsByRepoID failed: %v", err) return diff --git a/modules/indexer/stats/db.go b/modules/indexer/stats/db.go index bb3385ab63..d39b1dcf2a 100644 --- a/modules/indexer/stats/db.go +++ b/modules/indexer/stats/db.go @@ -30,7 +30,7 @@ func (db *DBIndexer) Index(id int64) error { return nil } - status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeStats) + status, err := repo_model.GetIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeStats) if err != nil { return err } diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index c8bd8d1783..9d9de5413c 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" @@ -49,7 +50,7 @@ func TestRepoStatsIndex(t *testing.T) { queue.GetManager().FlushAll(context.Background(), 5*time.Second) - status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeStats) + status, err := repo_model.GetIndexerStatus(db.DefaultContext, repo, repo_model.RepoIndexerTypeStats) assert.NoError(t, err) assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.CommitSha) langs, err := repo_model.GetTopLanguageStats(repo, 5) diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 44ed431c93..fe3561cefa 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -174,7 +174,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool { // look for the exact principal principalLoop: for _, principal := range cert.ValidPrincipals { - pkey, err := asymkey_model.SearchPublicKeyByContentExact(principal) + pkey, err := asymkey_model.SearchPublicKeyByContentExact(ctx, principal) if err != nil { if asymkey_model.IsErrKeyNotExist(err) { log.Debug("Principal Rejected: %s Unknown Principal: %s", ctx.RemoteAddr(), principal) @@ -234,7 +234,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool { log.Debug("Handle Public Key: %s Fingerprint: %s is not a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key)) } - pkey, err := asymkey_model.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key)))) + pkey, err := asymkey_model.SearchPublicKeyByContent(ctx, strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key)))) if err != nil { if asymkey_model.IsErrKeyNotExist(err) { if log.IsWarn() { |