diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-03-22 23:22:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 16:22:54 +0100 |
commit | 7a550b3af20dad94cf468012a6b817b43b91c1ba (patch) | |
tree | 1b836eabf8e911f35d0446c0cdad053cb3572c79 /services | |
parent | 2b55422cd71b0b325f054646de5cebf39b72b502 (diff) | |
download | gitea-7a550b3af20dad94cf468012a6b817b43b91c1ba.tar.gz gitea-7a550b3af20dad94cf468012a6b817b43b91c1ba.zip |
Use `ctx` instead of `db.DefaultContext` in some packages(routers/services/modules) (#19163)
* Remove `db.DefaultContext` usage in routers, use `ctx` directly
* Use `ctx` directly if there is one, remove some `db.DefaultContext` in `services`
* Use ctx instead of db.DefaultContext for `cmd` and some `modules` packages
* fix incorrect context usage
Diffstat (limited to 'services')
-rw-r--r-- | services/asymkey/sign.go | 8 | ||||
-rw-r--r-- | services/auth/source/ldap/source_sync.go | 2 | ||||
-rw-r--r-- | services/mailer/mail.go | 2 | ||||
-rw-r--r-- | services/mailer/mail_issue.go | 4 | ||||
-rw-r--r-- | services/mailer/mail_release.go | 3 | ||||
-rw-r--r-- | services/mailer/mail_test.go | 24 | ||||
-rw-r--r-- | services/migrations/gitea_uploader.go | 2 | ||||
-rw-r--r-- | services/mirror/mirror_pull.go | 3 | ||||
-rw-r--r-- | services/pull/check.go | 3 | ||||
-rw-r--r-- | services/pull/merge.go | 5 | ||||
-rw-r--r-- | services/pull/pull.go | 2 | ||||
-rw-r--r-- | services/pull/review.go | 2 | ||||
-rw-r--r-- | services/pull/temp_repo.go | 5 | ||||
-rw-r--r-- | services/release/release.go | 4 | ||||
-rw-r--r-- | services/repository/check.go | 8 | ||||
-rw-r--r-- | services/repository/hooks.go | 2 | ||||
-rw-r--r-- | services/repository/push.go | 2 | ||||
-rw-r--r-- | services/user/user.go | 4 |
18 files changed, 45 insertions, 40 deletions
diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index c2c6829d61..fa0afe0a57 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -143,7 +143,7 @@ Loop: case always: break Loop case pubkey: - keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{}) + keys, err := asymkey_model.ListGPGKeys(ctx, u.ID, db.ListOptions{}) if err != nil { return false, "", nil, err } @@ -179,7 +179,7 @@ Loop: case always: break Loop case pubkey: - keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{}) + keys, err := asymkey_model.ListGPGKeys(ctx, u.ID, db.ListOptions{}) if err != nil { return false, "", nil, err } @@ -232,7 +232,7 @@ Loop: case always: break Loop case pubkey: - keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{}) + keys, err := asymkey_model.ListGPGKeys(ctx, u.ID, db.ListOptions{}) if err != nil { return false, "", nil, err } @@ -294,7 +294,7 @@ Loop: case always: break Loop case pubkey: - keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{}) + keys, err := asymkey_model.ListGPGKeys(ctx, u.ID, db.ListOptions{}) if err != nil { return false, "", nil, err } diff --git a/services/auth/source/ldap/source_sync.go b/services/auth/source/ldap/source_sync.go index 0d4d94a3ad..b7ea1f0584 100644 --- a/services/auth/source/ldap/source_sync.go +++ b/services/auth/source/ldap/source_sync.go @@ -202,7 +202,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { log.Trace("SyncExternalUsers[%s]: Deactivating user %s", source.authSource.Name, usr.Name) usr.IsActive = false - err = user_model.UpdateUserCols(db.DefaultContext, usr, "is_active") + err = user_model.UpdateUserCols(ctx, usr, "is_active") if err != nil { log.Error("SyncExternalUsers[%s]: Error deactivating user %s: %v", source.authSource.Name, usr.Name, err) } diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 8c99b05620..8e04e7e4d2 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -7,6 +7,7 @@ package mailer import ( "bytes" + "context" "fmt" "html/template" "mime" @@ -414,6 +415,7 @@ func SendIssueAssignedMail(issue *models.Issue, doer *user_model.User, content s for lang, tos := range langMap { msgs, err := composeIssueCommentMessages(&mailCommentContext{ + Context: context.TODO(), // TODO: use a correct context Issue: issue, Doer: doer, ActionType: models.ActionType(0), diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index 1451150347..7f0fb9a3e1 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -9,7 +9,6 @@ import ( "fmt" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -81,7 +80,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo // =========== Repo watchers =========== // Make repo watchers last, since it's likely the list with the most users if !(ctx.Issue.IsPull && ctx.Issue.PullRequest.IsWorkInProgress() && ctx.ActionType != models.ActionCreatePullRequest) { - ids, err = repo_model.GetRepoWatchersIDs(db.DefaultContext, ctx.Issue.RepoID) + ids, err = repo_model.GetRepoWatchersIDs(ctx, ctx.Issue.RepoID) if err != nil { return fmt.Errorf("GetRepoWatchersIDs(%d): %v", ctx.Issue.RepoID, err) } @@ -186,6 +185,7 @@ func MailParticipants(issue *models.Issue, doer *user_model.User, opType models. } if err := mailIssueCommentToParticipants( &mailCommentContext{ + Context: context.TODO(), // TODO: use a correct context Issue: issue, Doer: doer, ActionType: opType, diff --git a/services/mailer/mail_release.go b/services/mailer/mail_release.go index 1ca9ad02d7..76dceb2387 100644 --- a/services/mailer/mail_release.go +++ b/services/mailer/mail_release.go @@ -9,7 +9,6 @@ import ( "context" "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/base" @@ -32,7 +31,7 @@ func MailNewRelease(ctx context.Context, rel *models.Release) { return } - watcherIDList, err := repo_model.GetRepoWatchersIDs(db.DefaultContext, rel.RepoID) + watcherIDList, err := repo_model.GetRepoWatchersIDs(ctx, rel.RepoID) if err != nil { log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err) return diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index ba82fc6ff8..c06bae1b98 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -6,6 +6,7 @@ package mailer import ( "bytes" + "context" "fmt" "html/template" "strings" @@ -70,7 +71,8 @@ func TestComposeIssueCommentMessage(t *testing.T) { recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}} msgs, err := composeIssueCommentMessages(&mailCommentContext{ - Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, Content: "test body", Comment: comment, }, "en-US", recipients, false, "issue comment") assert.NoError(t, err) @@ -99,7 +101,8 @@ func TestComposeIssueMessage(t *testing.T) { recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}} msgs, err := composeIssueCommentMessages(&mailCommentContext{ - Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, Content: "test body", }, "en-US", recipients, false, "issue create") assert.NoError(t, err) @@ -145,13 +148,15 @@ func TestTemplateSelection(t *testing.T) { } msg := testComposeIssueCommentMessage(t, &mailCommentContext{ - Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue, Content: "test body", }, recipients, false, "TestTemplateSelection") expect(t, msg, "issue/new/subject", "issue/new/body") msg = testComposeIssueCommentMessage(t, &mailCommentContext{ - Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue, Content: "test body", Comment: comment, }, recipients, false, "TestTemplateSelection") expect(t, msg, "issue/default/subject", "issue/default/body") @@ -159,13 +164,15 @@ func TestTemplateSelection(t *testing.T) { pull := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue) comment = unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment) msg = testComposeIssueCommentMessage(t, &mailCommentContext{ - Issue: pull, Doer: doer, ActionType: models.ActionCommentPull, + Context: context.TODO(), // TODO: use a correct context + Issue: pull, Doer: doer, ActionType: models.ActionCommentPull, Content: "test body", Comment: comment, }, recipients, false, "TestTemplateSelection") expect(t, msg, "pull/comment/subject", "pull/comment/body") msg = testComposeIssueCommentMessage(t, &mailCommentContext{ - Issue: issue, Doer: doer, ActionType: models.ActionCloseIssue, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: models.ActionCloseIssue, Content: "test body", Comment: comment, }, recipients, false, "TestTemplateSelection") expect(t, msg, "Re: [user2/repo1] issue1 (#1)", "issue/close/body") @@ -184,7 +191,8 @@ func TestTemplateServices(t *testing.T) { recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}} msg := testComposeIssueCommentMessage(t, &mailCommentContext{ - Issue: issue, Doer: doer, ActionType: actionType, + Context: context.TODO(), // TODO: use a correct context + Issue: issue, Doer: doer, ActionType: actionType, Content: "test body", Comment: comment, }, recipients, fromMention, "TestTemplateServices") @@ -226,7 +234,7 @@ func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recip func TestGenerateAdditionalHeaders(t *testing.T) { doer, _, issue, _ := prepareMailerTest(t) - ctx := &mailCommentContext{Issue: issue, Doer: doer} + ctx := &mailCommentContext{Context: context.TODO() /* TODO: use a correct context */, Issue: issue, Doer: doer} recipient := &user_model.User{Name: "Test", Email: "test@gitea.com"} headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 2faa0a1f2a..607dd0a076 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -817,7 +817,7 @@ func (g *GiteaLocalUploader) Finish() error { return err } - if err := models.UpdateRepoStats(db.DefaultContext, g.repo.ID); err != nil { + if err := models.UpdateRepoStats(g.ctx, g.repo.ID); err != nil { return err } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 6c9c4a0483..979ef7f03c 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" @@ -287,7 +286,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo gitRepo.Close() log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo) - if err := models.UpdateRepoSize(db.DefaultContext, m.Repo); err != nil { + if err := models.UpdateRepoSize(ctx, m.Repo); err != nil { log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo, err) } diff --git a/services/pull/check.go b/services/pull/check.go index b1e9237d11..3c79abf4b8 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -13,7 +13,6 @@ import ( "strings" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -168,7 +167,7 @@ func manuallyMerged(ctx context.Context, pr *models.PullRequest) bool { // When the commit author is unknown set the BaseRepo owner as merger if merger == nil { if pr.BaseRepo.Owner == nil { - if err = pr.BaseRepo.GetOwner(db.DefaultContext); err != nil { + if err = pr.BaseRepo.GetOwner(ctx); err != nil { log.Error("BaseRepo.GetOwner[%d]: %v", pr.ID, err) return false } diff --git a/services/pull/merge.go b/services/pull/merge.go index cb857cc60d..6108a7956e 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -17,7 +17,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -80,7 +79,7 @@ func Merge(ctx context.Context, pr *models.PullRequest, doer *user_model.User, b if err := pr.Issue.LoadRepo(); err != nil { log.Error("loadRepo for issue [%d]: %v", pr.ID, err) } - if err := pr.Issue.Repo.GetOwner(db.DefaultContext); err != nil { + if err := pr.Issue.Repo.GetOwner(ctx); err != nil { log.Error("GetOwner for issue repo [%d]: %v", pr.ID, err) } @@ -487,7 +486,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User } var headUser *user_model.User - err = pr.HeadRepo.GetOwner(db.DefaultContext) + err = pr.HeadRepo.GetOwner(ctx) if err != nil { if !user_model.IsErrUserNotExist(err) { log.Error("Can't find user: %d for head repository - %v", pr.HeadRepo.OwnerID, err) diff --git a/services/pull/pull.go b/services/pull/pull.go index 82deb74a4e..7a746d937f 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -69,7 +69,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *mode return err } - mentions, err := pull.FindAndUpdateIssueMentions(db.DefaultContext, pull.Poster, pull.Content) + mentions, err := pull.FindAndUpdateIssueMentions(ctx, pull.Poster, pull.Content) if err != nil { return err } diff --git a/services/pull/review.go b/services/pull/review.go index 25eef78d97..7f9797579a 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -61,7 +61,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git. return nil, err } - mentions, err := issue.FindAndUpdateIssueMentions(db.DefaultContext, doer, comment.Content) + mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, comment.Content) if err != nil { return nil, err } diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index 831d98745e..c6c6d8b6e7 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -13,7 +13,6 @@ import ( "strings" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -38,10 +37,10 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e return "", &repo_model.ErrRepoNotExist{ ID: pr.BaseRepoID, } - } else if err := pr.HeadRepo.GetOwner(db.DefaultContext); err != nil { + } else if err := pr.HeadRepo.GetOwner(ctx); err != nil { log.Error("HeadRepo.GetOwner: %v", err) return "", fmt.Errorf("HeadRepo.GetOwner: %v", err) - } else if err := pr.BaseRepo.GetOwner(db.DefaultContext); err != nil { + } else if err := pr.BaseRepo.GetOwner(ctx); err != nil { log.Error("BaseRepo.GetOwner: %v", err) return "", fmt.Errorf("BaseRepo.GetOwner: %v", err) } diff --git a/services/release/release.go b/services/release/release.go index 0df8635230..4d16e66aec 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -130,7 +130,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs return err } - if err = models.AddReleaseAttachments(db.DefaultContext, rel.ID, attachmentUUIDs); err != nil { + if err = models.AddReleaseAttachments(gitRepo.Ctx, rel.ID, attachmentUUIDs); err != nil { return err } @@ -319,7 +319,7 @@ func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, del } else { rel.IsTag = true - if err = models.UpdateRelease(db.DefaultContext, rel); err != nil { + if err = models.UpdateRelease(ctx, rel); err != nil { return fmt.Errorf("Update: %v", err) } } diff --git a/services/repository/check.go b/services/repository/check.go index 6adb8479c4..6962090f84 100644 --- a/services/repository/check.go +++ b/services/repository/check.go @@ -27,7 +27,7 @@ func GitFsck(ctx context.Context, timeout time.Duration, args []string) error { log.Trace("Doing: GitFsck") if err := db.Iterate( - db.DefaultContext, + ctx, new(repo_model.Repository), builder.Expr("id>0 AND is_fsck_enabled=?", true), func(idx int, bean interface{}) error { @@ -62,7 +62,7 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro args = append([]string{"gc"}, args...) if err := db.Iterate( - db.DefaultContext, + ctx, new(repo_model.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { @@ -97,7 +97,7 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro } // Now update the size of the repository - if err := models.UpdateRepoSize(db.DefaultContext, repo); err != nil { + if err := models.UpdateRepoSize(ctx, repo); err != nil { log.Error("Updating size as part of garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err) desc := fmt.Sprintf("Updating size as part of garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err) if err = admin_model.CreateRepositoryNotice(desc); err != nil { @@ -119,7 +119,7 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro func gatherMissingRepoRecords(ctx context.Context) ([]*repo_model.Repository, error) { repos := make([]*repo_model.Repository, 0, 10) if err := db.Iterate( - db.DefaultContext, + ctx, new(repo_model.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { diff --git a/services/repository/hooks.go b/services/repository/hooks.go index 3905249499..40303d5937 100644 --- a/services/repository/hooks.go +++ b/services/repository/hooks.go @@ -23,7 +23,7 @@ func SyncRepositoryHooks(ctx context.Context) error { log.Trace("Doing: SyncRepositoryHooks") if err := db.Iterate( - db.DefaultContext, + ctx, new(repo_model.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { diff --git a/services/repository/push.go b/services/repository/push.go index fafe4736ab..6cdfa1b4c2 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -95,7 +95,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } defer gitRepo.Close() - if err = models.UpdateRepoSize(db.DefaultContext, repo); err != nil { + if err = models.UpdateRepoSize(ctx, repo); err != nil { log.Error("Failed to update size for repository: %v", err) } diff --git a/services/user/user.go b/services/user/user.go index 21f1a74f62..e31b174c0c 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -78,7 +78,7 @@ func DeleteUser(u *user_model.User) error { path := user_model.UserPath(u.Name) if err := util.RemoveAll(path); err != nil { err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err) - _ = admin_model.CreateNotice(db.DefaultContext, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) + _ = admin_model.CreateNotice(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) return err } @@ -86,7 +86,7 @@ func DeleteUser(u *user_model.User) error { avatarPath := u.CustomAvatarRelativePath() if err := storage.Avatars.Delete(avatarPath); err != nil { err = fmt.Errorf("Failed to remove %s: %v", avatarPath, err) - _ = admin_model.CreateNotice(db.DefaultContext, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) + _ = admin_model.CreateNotice(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) return err } } |