From 7a550b3af20dad94cf468012a6b817b43b91c1ba Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 22 Mar 2022 23:22:54 +0800 Subject: 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 --- services/mailer/mail.go | 2 ++ services/mailer/mail_issue.go | 4 ++-- services/mailer/mail_release.go | 3 +-- services/mailer/mail_test.go | 24 ++++++++++++++++-------- 4 files changed, 21 insertions(+), 12 deletions(-) (limited to 'services/mailer') 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) -- cgit v1.2.3