From f232d8f5309fbf316e297cb7c04eb431f2c7ae24 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 13 Feb 2025 22:49:58 -0800 Subject: [PATCH] Performance optimization for pull request files loading comments attachments (#33585) --- models/issues/comment_code.go | 6 ++++-- routers/web/repo/pull.go | 12 ++++++------ services/gitdiff/gitdiff.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index 67a77ceb13..b562aab500 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -86,8 +86,10 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu ids = append(ids, comment.ReviewID) } } - if err := e.In("id", ids).Find(&reviews); err != nil { - return nil, err + if len(ids) > 0 { + if err := e.In("id", ids).Find(&reviews); err != nil { + return nil, err + } } n := 0 diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e6fb492d6e..0c82736eef 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -784,18 +784,18 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi return } + allComments := issues_model.CommentList{} for _, file := range diff.Files { for _, section := range file.Sections { for _, line := range section.Lines { - for _, comment := range line.Comments { - if err := comment.LoadAttachments(ctx); err != nil { - ctx.ServerError("LoadAttachments", err) - return - } - } + allComments = append(allComments, line.Comments...) } } } + if err := allComments.LoadAttachments(ctx); err != nil { + ctx.ServerError("LoadAttachments", err) + return + } pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch) if err != nil { diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index f046e59678..064f05cfce 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -80,7 +80,7 @@ type DiffLine struct { Match int Type DiffLineType Content string - Comments []*issues_model.Comment + Comments issues_model.CommentList SectionInfo *DiffLineSectionInfo }