Skip to content

Commit 06e4687

Browse files
authored
more context for models (#19511)
make more usage of context, to have more db transaction in one session (make diff of #9307 smaller)
1 parent 332b2ec commit 06e4687

Some content is hidden

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

54 files changed

+274
-244
lines changed

models/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
508508
permPR[i] = false
509509
continue
510510
}
511-
perm, err := getUserRepoPermission(ctx, repo, user)
511+
perm, err := GetUserRepoPermission(ctx, repo, user)
512512
if err != nil {
513513
permCode[i] = false
514514
permIssue[i] = false

models/branches.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,57 +337,57 @@ type WhitelistOptions struct {
337337
// If ID is 0, it creates a new record. Otherwise, updates existing record.
338338
// This function also performs check if whitelist user and team's IDs have been changed
339339
// to avoid unnecessary whitelist delete and regenerate.
340-
func UpdateProtectBranch(repo *repo_model.Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
341-
if err = repo.GetOwner(db.DefaultContext); err != nil {
340+
func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
341+
if err = repo.GetOwner(ctx); err != nil {
342342
return fmt.Errorf("GetOwner: %v", err)
343343
}
344344

345-
whitelist, err := updateUserWhitelist(repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
345+
whitelist, err := updateUserWhitelist(ctx, repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
346346
if err != nil {
347347
return err
348348
}
349349
protectBranch.WhitelistUserIDs = whitelist
350350

351-
whitelist, err = updateUserWhitelist(repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
351+
whitelist, err = updateUserWhitelist(ctx, repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
352352
if err != nil {
353353
return err
354354
}
355355
protectBranch.MergeWhitelistUserIDs = whitelist
356356

357-
whitelist, err = updateApprovalWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
357+
whitelist, err = updateApprovalWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
358358
if err != nil {
359359
return err
360360
}
361361
protectBranch.ApprovalsWhitelistUserIDs = whitelist
362362

363363
// if the repo is in an organization
364-
whitelist, err = updateTeamWhitelist(repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
364+
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
365365
if err != nil {
366366
return err
367367
}
368368
protectBranch.WhitelistTeamIDs = whitelist
369369

370-
whitelist, err = updateTeamWhitelist(repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
370+
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
371371
if err != nil {
372372
return err
373373
}
374374
protectBranch.MergeWhitelistTeamIDs = whitelist
375375

376-
whitelist, err = updateTeamWhitelist(repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
376+
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
377377
if err != nil {
378378
return err
379379
}
380380
protectBranch.ApprovalsWhitelistTeamIDs = whitelist
381381

382382
// Make sure protectBranch.ID is not 0 for whitelists
383383
if protectBranch.ID == 0 {
384-
if _, err = db.GetEngine(db.DefaultContext).Insert(protectBranch); err != nil {
384+
if _, err = db.GetEngine(ctx).Insert(protectBranch); err != nil {
385385
return fmt.Errorf("Insert: %v", err)
386386
}
387387
return nil
388388
}
389389

390-
if _, err = db.GetEngine(db.DefaultContext).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
390+
if _, err = db.GetEngine(ctx).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
391391
return fmt.Errorf("Update: %v", err)
392392
}
393393

@@ -416,15 +416,15 @@ func IsProtectedBranch(repoID int64, branchName string) (bool, error) {
416416

417417
// updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
418418
// the users from newWhitelist which have explicit read or write access to the repo.
419-
func updateApprovalWhitelist(repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
419+
func updateApprovalWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
420420
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
421421
if !hasUsersChanged {
422422
return currentWhitelist, nil
423423
}
424424

425425
whitelist = make([]int64, 0, len(newWhitelist))
426426
for _, userID := range newWhitelist {
427-
if reader, err := IsRepoReader(repo, userID); err != nil {
427+
if reader, err := IsRepoReader(ctx, repo, userID); err != nil {
428428
return nil, err
429429
} else if !reader {
430430
continue
@@ -437,19 +437,19 @@ func updateApprovalWhitelist(repo *repo_model.Repository, currentWhitelist, newW
437437

438438
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
439439
// the users from newWhitelist which have write access to the repo.
440-
func updateUserWhitelist(repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
440+
func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
441441
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
442442
if !hasUsersChanged {
443443
return currentWhitelist, nil
444444
}
445445

446446
whitelist = make([]int64, 0, len(newWhitelist))
447447
for _, userID := range newWhitelist {
448-
user, err := user_model.GetUserByID(userID)
448+
user, err := user_model.GetUserByIDCtx(ctx, userID)
449449
if err != nil {
450450
return nil, fmt.Errorf("GetUserByID [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
451451
}
452-
perm, err := GetUserRepoPermission(repo, user)
452+
perm, err := GetUserRepoPermission(ctx, repo, user)
453453
if err != nil {
454454
return nil, fmt.Errorf("GetUserRepoPermission [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
455455
}
@@ -466,13 +466,13 @@ func updateUserWhitelist(repo *repo_model.Repository, currentWhitelist, newWhite
466466

467467
// updateTeamWhitelist checks whether the team whitelist changed and returns a whitelist with
468468
// the teams from newWhitelist which have write access to the repo.
469-
func updateTeamWhitelist(repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
469+
func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
470470
hasTeamsChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
471471
if !hasTeamsChanged {
472472
return currentWhitelist, nil
473473
}
474474

475-
teams, err := organization.GetTeamsWithAccessToRepo(repo.OwnerID, repo.ID, perm.AccessModeRead)
475+
teams, err := organization.GetTeamsWithAccessToRepo(ctx, repo.OwnerID, repo.ID, perm.AccessModeRead)
476476
if err != nil {
477477
return nil, fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
478478
}

models/branches_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"testing"
99

10+
"code.gitea.io/gitea/models/db"
1011
repo_model "code.gitea.io/gitea/models/repo"
1112
"code.gitea.io/gitea/models/unittest"
1213

@@ -99,11 +100,14 @@ func TestRenameBranch(t *testing.T) {
99100
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
100101
_isDefault := false
101102

102-
err := UpdateProtectBranch(repo1, &ProtectedBranch{
103+
ctx, committer, err := db.TxContext()
104+
defer committer.Close()
105+
assert.NoError(t, err)
106+
assert.NoError(t, UpdateProtectBranch(ctx, repo1, &ProtectedBranch{
103107
RepoID: repo1.ID,
104108
BranchName: "master",
105-
}, WhitelistOptions{})
106-
assert.NoError(t, err)
109+
}, WhitelistOptions{}))
110+
assert.NoError(t, committer.Commit())
107111

108112
assert.NoError(t, RenameBranch(repo1, "master", "main", func(isDefault bool) error {
109113
_isDefault = isDefault

models/commit_status.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,13 @@ type CommitStatusIndex struct {
232232

233233
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
234234
func GetLatestCommitStatus(repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
235-
return getLatestCommitStatus(db.GetEngine(db.DefaultContext), repoID, sha, listOptions)
235+
return GetLatestCommitStatusCtx(db.DefaultContext, repoID, sha, listOptions)
236236
}
237237

238-
func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
238+
// GetLatestCommitStatusCtx returns all statuses with a unique context for a given commit.
239+
func GetLatestCommitStatusCtx(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
239240
ids := make([]int64, 0, 10)
240-
sess := e.Table(&CommitStatus{}).
241+
sess := db.GetEngine(ctx).Table(&CommitStatus{}).
241242
Where("repo_id = ?", repoID).And("sha = ?", sha).
242243
Select("max( id ) as id").
243244
GroupBy("context_hash").OrderBy("max( id ) desc")
@@ -252,7 +253,7 @@ func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db
252253
if len(ids) == 0 {
253254
return statuses, count, nil
254255
}
255-
return statuses, count, e.In("id", ids).Find(&statuses)
256+
return statuses, count, db.GetEngine(ctx).In("id", ids).Find(&statuses)
256257
}
257258

258259
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts

models/issue.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,9 @@ func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
162162
}
163163

164164
// LoadLabels loads labels
165-
func (issue *Issue) LoadLabels() error {
166-
return issue.loadLabels(db.GetEngine(db.DefaultContext))
167-
}
168-
169-
func (issue *Issue) loadLabels(e db.Engine) (err error) {
165+
func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
170166
if issue.Labels == nil {
171-
issue.Labels, err = getLabelsByIssueID(e, issue.ID)
167+
issue.Labels, err = getLabelsByIssueID(db.GetEngine(ctx), issue.ID)
172168
if err != nil {
173169
return fmt.Errorf("getLabelsByIssueID [%d]: %v", issue.ID, err)
174170
}
@@ -313,7 +309,7 @@ func (issue *Issue) loadAttributes(ctx context.Context) (err error) {
313309
return
314310
}
315311

316-
if err = issue.loadLabels(e); err != nil {
312+
if err = issue.LoadLabels(ctx); err != nil {
317313
return
318314
}
319315

@@ -493,7 +489,7 @@ func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) {
493489
return err
494490
}
495491

496-
perm, err := getUserRepoPermission(ctx, issue.Repo, doer)
492+
perm, err := GetUserRepoPermission(ctx, issue.Repo, doer)
497493
if err != nil {
498494
return err
499495
}
@@ -539,7 +535,7 @@ func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (e
539535
return err
540536
}
541537

542-
if err = issue.loadLabels(db.GetEngine(ctx)); err != nil {
538+
if err = issue.LoadLabels(ctx); err != nil {
543539
return err
544540
}
545541

@@ -587,7 +583,7 @@ func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (e
587583
}
588584

589585
issue.Labels = nil
590-
if err = issue.loadLabels(db.GetEngine(ctx)); err != nil {
586+
if err = issue.LoadLabels(ctx); err != nil {
591587
return err
592588
}
593589

@@ -2341,9 +2337,9 @@ func ResolveIssueMentionsByVisibility(ctx context.Context, issue *Issue, doer *u
23412337
continue
23422338
}
23432339
// Normal users must have read access to the referencing issue
2344-
perm, err := getUserRepoPermission(ctx, issue.Repo, user)
2340+
perm, err := GetUserRepoPermission(ctx, issue.Repo, user)
23452341
if err != nil {
2346-
return nil, fmt.Errorf("getUserRepoPermission [%d]: %v", user.ID, err)
2342+
return nil, fmt.Errorf("GetUserRepoPermission [%d]: %v", user.ID, err)
23472343
}
23482344
if !perm.CanReadIssuesOrPulls(issue.IsPull) {
23492345
continue

models/issue_label.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,6 @@ func NewIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error
613613
return err
614614
}
615615
defer committer.Close()
616-
sess := db.GetEngine(ctx)
617616

618617
if err = issue.LoadRepo(ctx); err != nil {
619618
return err
@@ -629,7 +628,7 @@ func NewIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error
629628
}
630629

631630
issue.Labels = nil
632-
if err = issue.loadLabels(sess); err != nil {
631+
if err = issue.LoadLabels(ctx); err != nil {
633632
return err
634633
}
635634

@@ -670,7 +669,7 @@ func NewIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err e
670669
}
671670

672671
issue.Labels = nil
673-
if err = issue.loadLabels(db.GetEngine(ctx)); err != nil {
672+
if err = issue.LoadLabels(ctx); err != nil {
674673
return err
675674
}
676675

@@ -707,23 +706,13 @@ func deleteIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *use
707706
}
708707

709708
// DeleteIssueLabel deletes issue-label relation.
710-
func DeleteIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error) {
711-
ctx, committer, err := db.TxContext()
712-
if err != nil {
713-
return err
714-
}
715-
defer committer.Close()
716-
717-
if err = deleteIssueLabel(ctx, issue, label, doer); err != nil {
709+
func DeleteIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_model.User) error {
710+
if err := deleteIssueLabel(ctx, issue, label, doer); err != nil {
718711
return err
719712
}
720713

721714
issue.Labels = nil
722-
if err = issue.loadLabels(db.GetEngine(ctx)); err != nil {
723-
return err
724-
}
725-
726-
return committer.Commit()
715+
return issue.LoadLabels(ctx)
727716
}
728717

729718
func deleteLabelsByRepoID(sess db.Engine, repoID int64) error {

models/issue_label_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ func TestDeleteIssueLabel(t *testing.T) {
369369
}
370370
}
371371

372-
assert.NoError(t, DeleteIssueLabel(issue, label, doer))
372+
ctx, committer, err := db.TxContext()
373+
defer committer.Close()
374+
assert.NoError(t, err)
375+
assert.NoError(t, DeleteIssueLabel(ctx, issue, label, doer))
376+
assert.NoError(t, committer.Commit())
377+
373378
unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
374379
unittest.AssertExistsAndLoadBean(t, &Comment{
375380
Type: CommentTypeLabel,

models/issue_xref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (issue *Issue) verifyReferencedIssue(stdCtx context.Context, ctx *crossRefe
215215

216216
// Check doer permissions; set action to None if the doer can't change the destination
217217
if refIssue.RepoID != ctx.OrigIssue.RepoID || ref.Action != references.XRefActionNone {
218-
perm, err := getUserRepoPermission(stdCtx, refIssue.Repo, ctx.Doer)
218+
perm, err := GetUserRepoPermission(stdCtx, refIssue.Repo, ctx.Doer)
219219
if err != nil {
220220
return nil, references.XRefActionNone, err
221221
}

0 commit comments

Comments
 (0)