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
This commit is contained in:
parent
d81e31ad78
commit
fd7d83ace6
232 changed files with 1463 additions and 2108 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue