diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-11-19 09:12:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-19 16:12:33 +0800 |
commit | 044c754ea53f5b81f451451df53aea366f6f700a (patch) | |
tree | 45688c28a84f87f71ec3f99eb0e8456eb7d19c42 /modules/convert | |
parent | fefdb7ffd11bbfbff66dae8e88681ec840dedfde (diff) | |
download | gitea-044c754ea53f5b81f451451df53aea366f6f700a.tar.gz gitea-044c754ea53f5b81f451451df53aea366f6f700a.zip |
Add `context.Context` to more methods (#21546)
This PR adds a context parameter to a bunch of methods. Some helper
`xxxCtx()` methods got replaced with the normal name now.
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/convert')
-rw-r--r-- | modules/convert/issue.go | 29 | ||||
-rw-r--r-- | modules/convert/issue_comment.go | 27 | ||||
-rw-r--r-- | modules/convert/pull.go | 6 | ||||
-rw-r--r-- | modules/convert/pull_test.go | 9 |
4 files changed, 37 insertions, 34 deletions
diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 5364367a80..7c11b2a89f 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -5,6 +5,7 @@ package convert import ( + "context" "fmt" "net/url" "strings" @@ -22,17 +23,17 @@ import ( // it assumes some fields assigned with values: // Required - Poster, Labels, // Optional - Milestone, Assignee, PullRequest -func ToAPIIssue(issue *issues_model.Issue) *api.Issue { - if err := issue.LoadLabels(db.DefaultContext); err != nil { +func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue { + if err := issue.LoadLabels(ctx); err != nil { return &api.Issue{} } - if err := issue.LoadPoster(); err != nil { + if err := issue.LoadPoster(ctx); err != nil { return &api.Issue{} } - if err := issue.LoadRepo(db.DefaultContext); err != nil { + if err := issue.LoadRepo(ctx); err != nil { return &api.Issue{} } - if err := issue.Repo.GetOwner(db.DefaultContext); err != nil { + if err := issue.Repo.GetOwner(ctx); err != nil { return &api.Issue{} } @@ -64,14 +65,14 @@ func ToAPIIssue(issue *issues_model.Issue) *api.Issue { apiIssue.Closed = issue.ClosedUnix.AsTimePtr() } - if err := issue.LoadMilestone(); err != nil { + if err := issue.LoadMilestone(ctx); err != nil { return &api.Issue{} } if issue.Milestone != nil { apiIssue.Milestone = ToAPIMilestone(issue.Milestone) } - if err := issue.LoadAssignees(db.DefaultContext); err != nil { + if err := issue.LoadAssignees(ctx); err != nil { return &api.Issue{} } if len(issue.Assignees) > 0 { @@ -81,7 +82,7 @@ func ToAPIIssue(issue *issues_model.Issue) *api.Issue { apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` } if issue.IsPull { - if err := issue.LoadPullRequest(); err != nil { + if err := issue.LoadPullRequest(ctx); err != nil { return &api.Issue{} } apiIssue.PullRequest = &api.PullRequestMeta{ @@ -99,16 +100,16 @@ func ToAPIIssue(issue *issues_model.Issue) *api.Issue { } // ToAPIIssueList converts an IssueList to API format -func ToAPIIssueList(il issues_model.IssueList) []*api.Issue { +func ToAPIIssueList(ctx context.Context, il issues_model.IssueList) []*api.Issue { result := make([]*api.Issue, len(il)) for i := range il { - result[i] = ToAPIIssue(il[i]) + result[i] = ToAPIIssue(ctx, il[i]) } return result } // ToTrackedTime converts TrackedTime to API format -func ToTrackedTime(t *issues_model.TrackedTime) (apiT *api.TrackedTime) { +func ToTrackedTime(ctx context.Context, t *issues_model.TrackedTime) (apiT *api.TrackedTime) { apiT = &api.TrackedTime{ ID: t.ID, IssueID: t.IssueID, @@ -118,7 +119,7 @@ func ToTrackedTime(t *issues_model.TrackedTime) (apiT *api.TrackedTime) { Created: t.Created, } if t.Issue != nil { - apiT.Issue = ToAPIIssue(t.Issue) + apiT.Issue = ToAPIIssue(ctx, t.Issue) } if t.User != nil { apiT.UserName = t.User.Name @@ -169,10 +170,10 @@ func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) { } // ToTrackedTimeList converts TrackedTimeList to API format -func ToTrackedTimeList(tl issues_model.TrackedTimeList) api.TrackedTimeList { +func ToTrackedTimeList(ctx context.Context, tl issues_model.TrackedTimeList) api.TrackedTimeList { result := make([]*api.TrackedTime, 0, len(tl)) for _, t := range tl { - result = append(result, ToTrackedTime(t)) + result = append(result, ToTrackedTime(ctx, t)) } return result } diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go index 73ad345fa4..c33cf5c111 100644 --- a/modules/convert/issue_comment.go +++ b/modules/convert/issue_comment.go @@ -5,7 +5,8 @@ package convert import ( - "code.gitea.io/gitea/models/db" + "context" + issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -28,8 +29,8 @@ func ToComment(c *issues_model.Comment) *api.Comment { } // ToTimelineComment converts a issues_model.Comment to the api.TimelineComment format -func ToTimelineComment(c *issues_model.Comment, doer *user_model.User) *api.TimelineComment { - err := c.LoadMilestone() +func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_model.User) *api.TimelineComment { + err := c.LoadMilestone(ctx) if err != nil { log.Error("LoadMilestone: %v", err) return nil @@ -107,25 +108,25 @@ func ToTimelineComment(c *issues_model.Comment, doer *user_model.User) *api.Time return nil } - comment.TrackedTime = ToTrackedTime(c.Time) + comment.TrackedTime = ToTrackedTime(ctx, c.Time) } if c.RefIssueID != 0 { - issue, err := issues_model.GetIssueByID(db.DefaultContext, c.RefIssueID) + issue, err := issues_model.GetIssueByID(ctx, c.RefIssueID) if err != nil { log.Error("GetIssueByID(%d): %v", c.RefIssueID, err) return nil } - comment.RefIssue = ToAPIIssue(issue) + comment.RefIssue = ToAPIIssue(ctx, issue) } if c.RefCommentID != 0 { - com, err := issues_model.GetCommentByID(db.DefaultContext, c.RefCommentID) + com, err := issues_model.GetCommentByID(ctx, c.RefCommentID) if err != nil { log.Error("GetCommentByID(%d): %v", c.RefCommentID, err) return nil } - err = com.LoadPoster() + err = com.LoadPoster(ctx) if err != nil { log.Error("LoadPoster: %v", err) return nil @@ -138,17 +139,17 @@ func ToTimelineComment(c *issues_model.Comment, doer *user_model.User) *api.Time var repo *repo_model.Repository if c.Label.BelongsToOrg() { var err error - org, err = user_model.GetUserByID(c.Label.OrgID) + org, err = user_model.GetUserByIDCtx(ctx, c.Label.OrgID) if err != nil { - log.Error("GetUserByID(%d): %v", c.Label.OrgID, err) + log.Error("GetUserByIDCtx(%d): %v", c.Label.OrgID, err) return nil } } if c.Label.BelongsToRepo() { var err error - repo, err = repo_model.GetRepositoryByID(c.Label.RepoID) + repo, err = repo_model.GetRepositoryByIDCtx(ctx, c.Label.RepoID) if err != nil { - log.Error("GetRepositoryByID(%d): %v", c.Label.RepoID, err) + log.Error("GetRepositoryByIDCtx(%d): %v", c.Label.RepoID, err) return nil } } @@ -167,7 +168,7 @@ func ToTimelineComment(c *issues_model.Comment, doer *user_model.User) *api.Time } if c.DependentIssue != nil { - comment.DependentIssue = ToAPIIssue(c.DependentIssue) + comment.DependentIssue = ToAPIIssue(ctx, c.DependentIssue) } return comment diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 9c31f9bd2c..ca9a4c39c5 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -33,13 +33,13 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u return nil } - apiIssue := ToAPIIssue(pr.Issue) - if err := pr.LoadBaseRepoCtx(ctx); err != nil { + apiIssue := ToAPIIssue(ctx, pr.Issue) + if err := pr.LoadBaseRepo(ctx); err != nil { log.Error("GetRepositoryById[%d]: %v", pr.ID, err) return nil } - if err := pr.LoadHeadRepoCtx(ctx); err != nil { + if err := pr.LoadHeadRepo(ctx); err != nil { log.Error("GetRepositoryById[%d]: %v", pr.ID, err) return nil } diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go index a6ccbaca58..a0a672d3a5 100644 --- a/modules/convert/pull_test.go +++ b/modules/convert/pull_test.go @@ -7,6 +7,7 @@ package convert import ( "testing" + "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" @@ -22,8 +23,8 @@ func TestPullRequest_APIFormat(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) headRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) - assert.NoError(t, pr.LoadAttributes()) - assert.NoError(t, pr.LoadIssue()) + assert.NoError(t, pr.LoadAttributes(db.DefaultContext)) + assert.NoError(t, pr.LoadIssue(db.DefaultContext)) apiPullRequest := ToAPIPullRequest(git.DefaultContext, pr, nil) assert.NotNil(t, apiPullRequest) assert.EqualValues(t, &structs.PRBranchInfo{ @@ -36,8 +37,8 @@ func TestPullRequest_APIFormat(t *testing.T) { // withOut HeadRepo pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) - assert.NoError(t, pr.LoadIssue()) - assert.NoError(t, pr.LoadAttributes()) + assert.NoError(t, pr.LoadIssue(db.DefaultContext)) + assert.NoError(t, pr.LoadAttributes(db.DefaultContext)) // simulate fork deletion pr.HeadRepo = nil pr.HeadRepoID = 100000 |