From d612e38923ca144cb0f826fc099a38dac0bbdb7d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 00:36:49 +0000 Subject: [PATCH 01/11] fix --- models/issues/comment_code.go | 10 +++++++--- services/gitdiff/gitdiff.go | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index 6f23d3326a223..a18e8b880617a 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -5,6 +5,7 @@ package issues import ( "context" + "strings" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" @@ -15,7 +16,7 @@ import ( ) // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS -type CodeComments map[string]map[int64][]*Comment +type CodeComments map[string]map[string][]*Comment // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) { @@ -40,9 +41,12 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u for _, comment := range comments { if pathToLineToComment[comment.TreePath] == nil { - pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment) + pathToLineToComment[comment.TreePath] = make(map[string][]*Comment) } - pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment) + // always show the comment at the last line + patchLines := strings.Split(comment.Patch, "\n") + lineContent := patchLines[len(patchLines)-1] + pathToLineToComment[comment.TreePath][lineContent] = append(pathToLineToComment[comment.TreePath][lineContent], comment) } return pathToLineToComment, nil } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 0ddd5a48e2148..824aa022b9b43 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -475,10 +475,10 @@ func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, c if lineCommits, ok := allComments[file.Name]; ok { for _, section := range file.Sections { for _, line := range section.Lines { - if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok { + if comments, ok := lineCommits[line.Content]; ok { line.Comments = append(line.Comments, comments...) } - if comments, ok := lineCommits[int64(line.RightIdx)]; ok { + if comments, ok := lineCommits[""]; ok { line.Comments = append(line.Comments, comments...) } sort.SliceStable(line.Comments, func(i, j int) bool { From 20e93625b71f71fd1bd57b5334ecfa80ebdfe88d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 01:39:31 +0000 Subject: [PATCH 02/11] fix web ui --- models/issues/comment.go | 11 +++++++++ models/issues/comment_code.go | 24 ++++++++++++------- routers/web/repo/pull_review.go | 8 +++---- services/forms/repo_form.go | 1 + templates/repo/diff/comment_form.tmpl | 1 + .../repo/diff/comment_form_datahandler.tmpl | 2 +- templates/repo/diff/conversation.tmpl | 2 +- templates/repo/diff/section_split.tmpl | 8 +++---- templates/repo/diff/section_unified.tmpl | 2 +- .../repo/issue/view_content/conversation.tmpl | 2 +- web_src/js/features/repo-diff.js | 3 ++- web_src/js/features/repo-issue.js | 2 ++ 12 files changed, 44 insertions(+), 22 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index c6c5dc24321d1..233f4b0f98aba 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -10,6 +10,7 @@ import ( "fmt" "html/template" "strconv" + "strings" "unicode/utf8" "code.gitea.io/gitea/models/db" @@ -783,6 +784,15 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) { return err } +func (c *Comment) GetLineContent() string { + // always show the comment at the last line + patchLines := strings.Split(c.Patch, "\n") + if len(patchLines) == 0 { + return "" + } + return patchLines[len(patchLines)-1] +} + // CreateComment creates comment with context func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) { ctx, committer, err := db.TxContext(ctx) @@ -1033,6 +1043,7 @@ type FindCommentsOptions struct { Since int64 Before int64 Line int64 + LineContent string TreePath string Type CommentType IssueIDs []int64 diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index a18e8b880617a..fd183582b776d 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -5,7 +5,6 @@ package issues import ( "context" - "strings" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" @@ -43,9 +42,7 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u if pathToLineToComment[comment.TreePath] == nil { pathToLineToComment[comment.TreePath] = make(map[string][]*Comment) } - // always show the comment at the last line - patchLines := strings.Split(comment.Patch, "\n") - lineContent := patchLines[len(patchLines)-1] + lineContent := comment.GetLineContent() pathToLineToComment[comment.TreePath][lineContent] = append(pathToLineToComment[comment.TreePath][lineContent], comment) } return pathToLineToComment, nil @@ -70,6 +67,14 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu return nil, err } + if opts.LineContent != "" { + for index, comment := range comments { + if comment.GetLineContent() != opts.LineContent { + comments = append(comments[:index], comments[index+1:]...) + } + } + } + if err := issue.LoadRepo(ctx); err != nil { return nil, err } @@ -131,12 +136,13 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu } // FetchCodeCommentsByLine fetches the code comments for a given treePath and line number -func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) (CommentList, error) { +func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, lineContent string, showOutdatedComments bool) (CommentList, error) { opts := FindCommentsOptions{ - Type: CommentTypeCode, - IssueID: issue.ID, - TreePath: treePath, - Line: line, + Type: CommentTypeCode, + IssueID: issue.ID, + TreePath: treePath, + Line: line, + LineContent: lineContent, } return findCodeComments(ctx, opts, issue, currentUser, nil, showOutdatedComments) } diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 62f6d71c5e5cf..1fb0d7cdb7804 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -110,7 +110,7 @@ func CreateCodeComment(ctx *context.Context) { log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID) - renderConversation(ctx, comment, form.Origin) + renderConversation(ctx, comment, form.Origin, form.LineContent) } // UpdateResolveConversation add or remove an Conversation resolved mark @@ -161,14 +161,14 @@ func UpdateResolveConversation(ctx *context.Context) { return } - renderConversation(ctx, comment, origin) + renderConversation(ctx, comment, origin, ctx.FormString("line_content")) } -func renderConversation(ctx *context.Context, comment *issues_model.Comment, origin string) { +func renderConversation(ctx *context.Context, comment *issues_model.Comment, origin, lineContent string) { ctx.Data["PageIsPullFiles"] = origin == "diff" showOutdatedComments := origin == "timeline" || ctx.Data["ShowOutdatedComments"].(bool) - comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, showOutdatedComments) + comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, lineContent, showOutdatedComments) if err != nil { ctx.ServerError("FetchCodeCommentsByLine", err) return diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index 32d96abf4d272..fde0b1145a46c 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -587,6 +587,7 @@ type CodeCommentForm struct { Content string `binding:"Required"` Side string `binding:"Required;In(previous,proposed)"` Line int64 + LineContent string `binding:"Required"` TreePath string `form:"path" binding:"Required"` SingleReview bool `form:"single_review"` Reply int64 `form:"reply"` diff --git a/templates/repo/diff/comment_form.tmpl b/templates/repo/diff/comment_form.tmpl index 856b3da01a345..9abcc2121a5f4 100644 --- a/templates/repo/diff/comment_form.tmpl +++ b/templates/repo/diff/comment_form.tmpl @@ -5,6 +5,7 @@ + diff --git a/templates/repo/diff/comment_form_datahandler.tmpl b/templates/repo/diff/comment_form_datahandler.tmpl index d0e493488df0a..88e2a8a9793db 100644 --- a/templates/repo/diff/comment_form_datahandler.tmpl +++ b/templates/repo/diff/comment_form_datahandler.tmpl @@ -1,5 +1,5 @@ {{if $.comment}} - {{template "repo/diff/comment_form" dict "root" $.root "hidden" $.hidden "reply" $.reply "Line" $.comment.UnsignedLine "File" $.comment.TreePath "Side" $.comment.DiffSide "HasComments" true}} + {{template "repo/diff/comment_form" dict "root" $.root "hidden" $.hidden "reply" $.reply "Line" $.comment.UnsignedLine "LineContent" $.comment.GetLineContent "File" $.comment.TreePath "Side" $.comment.DiffSide "HasComments" true}} {{else if $.root}} {{template "repo/diff/comment_form" $}} {{else}} diff --git a/templates/repo/diff/conversation.tmpl b/templates/repo/diff/conversation.tmpl index 08f60644b3b52..4eee1490eb152 100644 --- a/templates/repo/diff/conversation.tmpl +++ b/templates/repo/diff/conversation.tmpl @@ -50,7 +50,7 @@ {{if and $.CanMarkConversation $hasReview (not $isReviewPending)}} - + {{if $resolved}} {{ctx.Locale.Tr "repo.issues.review.un_resolve_conversation"}} {{else}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 37b42bcb376ab..7fab9c64a3437 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -49,7 +49,7 @@ {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -64,7 +64,7 @@ {{if $match.RightIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -81,7 +81,7 @@ {{if $line.LeftIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -96,7 +96,7 @@ {{if $line.RightIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index 708b333291641..dffa4eda45543 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -54,7 +54,7 @@ {{else}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index ccea9b690d7e8..648e2fe932a45 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -120,7 +120,7 @@ {{if and $.CanMarkConversation $hasReview (not $isReviewPending)}} - + {{if $resolved}} {{ctx.Locale.Tr "repo.issues.review.un_resolve_conversation"}} {{else}} diff --git a/web_src/js/features/repo-diff.js b/web_src/js/features/repo-diff.js index 00f74515df6a8..ba526d3691551 100644 --- a/web_src/js/features/repo-diff.js +++ b/web_src/js/features/repo-diff.js @@ -100,10 +100,11 @@ function initRepoDiffConversationForm() { const comment_id = $(this).data('comment-id'); const origin = $(this).data('origin'); const action = $(this).data('action'); + const line_content = $(this).data('line-content'); const url = $(this).data('update-url'); try { - const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})}); + const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id, line_content})}); const data = await response.text(); if ($(this).closest('.conversation-holder').length) { diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index 95910e34bc9af..e35c125c8397a 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -524,6 +524,7 @@ export function initRepoPullRequestReview() { const isSplit = this.closest('.code-diff')?.classList.contains('code-diff-split'); const side = this.getAttribute('data-side'); const idx = this.getAttribute('data-idx'); + const content = this.getAttribute('data-content'); const path = this.closest('[data-path]')?.getAttribute('data-path'); const tr = this.closest('tr'); const lineType = tr.getAttribute('data-line-type'); @@ -551,6 +552,7 @@ export function initRepoPullRequestReview() { const html = await response.text(); $td.html(html); $td.find("input[name='line']").val(idx); + $td.find("input[name='line_content']").val(content); $td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed'); $td.find("input[name='path']").val(path); From 866036584d3718433345bb1d27323b739db265c9 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 02:08:18 +0000 Subject: [PATCH 03/11] fix bug --- models/issues/comment_code.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index fd183582b776d..6e9d2357cbbaf 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -68,11 +69,9 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu } if opts.LineContent != "" { - for index, comment := range comments { - if comment.GetLineContent() != opts.LineContent { - comments = append(comments[:index], comments[index+1:]...) - } - } + comments = container.FilterSlice(comments, func(c *Comment) (*Comment, bool) { + return c, c.GetLineContent() == opts.LineContent + }) } if err := issue.LoadRepo(ctx); err != nil { From 2a233703710dbd59516d8f3e7f8bdc53360a5d37 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 02:41:02 +0000 Subject: [PATCH 04/11] fix tests --- models/fixtures/comment.yml | 6 +++--- models/issues/comment_test.go | 11 ++++++++--- models/issues/review_test.go | 3 ++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/models/fixtures/comment.yml b/models/fixtures/comment.yml index 74fc716180d1d..bdbc9b852cd03 100644 --- a/models/fixtures/comment.yml +++ b/models/fixtures/comment.yml @@ -29,8 +29,8 @@ issue_id: 2 content: "meh..." review_id: 4 - line: 4 tree_path: "README.md" + patch: "+ # repo1" created_unix: 946684812 invalidated: false - @@ -39,7 +39,7 @@ poster_id: 1 issue_id: 2 content: "meh..." - line: -4 + patch: "- " tree_path: "README.md" created_unix: 946684812 invalidated: false @@ -50,7 +50,7 @@ poster_id: 1 issue_id: 2 content: "it's already invalidated. boring..." - line: -4 + patch: "- " tree_path: "README.md" created_unix: 946684812 invalidated: true diff --git a/models/issues/comment_test.go b/models/issues/comment_test.go index c5bbfdedc28ec..1844aae0739fd 100644 --- a/models/issues/comment_test.go +++ b/models/issues/comment_test.go @@ -51,11 +51,16 @@ func TestFetchCodeComments(t *testing.T) { issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false) + lineContent := "+ # repo1" assert.NoError(t, err) assert.Contains(t, res, "README.md") - assert.Contains(t, res["README.md"], int64(4)) - assert.Len(t, res["README.md"][4], 1) - assert.Equal(t, int64(4), res["README.md"][4][0].ID) + assert.Contains(t, res["README.md"], lineContent) + assert.Len(t, res["README.md"][lineContent], 1) + assert.Equal(t, int64(4), res["README.md"][lineContent][0].ID) + lineContent = "- " + assert.Contains(t, res["README.md"], lineContent) + assert.Len(t, res["README.md"][lineContent], 1) + assert.Equal(t, int64(5), res["README.md"][lineContent][0].ID) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false) diff --git a/models/issues/review_test.go b/models/issues/review_test.go index ac1b84adebcc2..cfeed0e704834 100644 --- a/models/issues/review_test.go +++ b/models/issues/review_test.go @@ -48,7 +48,8 @@ func TestReview_LoadCodeComments(t *testing.T) { assert.NoError(t, review.LoadAttributes(db.DefaultContext)) assert.NoError(t, review.LoadCodeComments(db.DefaultContext)) assert.Len(t, review.CodeComments, 1) - assert.Equal(t, int64(4), review.CodeComments["README.md"][int64(4)][0].Line) + lineContent := "+ # repo1" + assert.Equal(t, int64(4), review.CodeComments["README.md"][lineContent][0].ID) } func TestReviewType_Icon(t *testing.T) { From 12be5c532c9c3a764948cc0454edf90de85898d7 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 02:55:40 +0000 Subject: [PATCH 05/11] fix test --- routers/web/repo/pull_review_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/pull_review_test.go b/routers/web/repo/pull_review_test.go index 8344ff409113f..464b56498cf64 100644 --- a/routers/web/repo/pull_review_test.go +++ b/routers/web/repo/pull_review_test.go @@ -56,29 +56,29 @@ func TestRenderConversation(t *testing.T) { } run("diff with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) { ctx.Data["ShowOutdatedComments"] = true - renderConversation(ctx, preparedComment, "diff") + renderConversation(ctx, preparedComment, "diff", preparedComment.GetLineContent()) assert.Contains(t, resp.Body.String(), ` Date: Mon, 10 Jun 2024 05:19:30 +0000 Subject: [PATCH 06/11] fix logic --- models/issues/comment_code.go | 7 +++-- routers/web/repo/issue.go | 28 ++++++++++---------- services/convert/pull_review.go | 45 ++++++++++++++++----------------- services/feed/action.go | 26 +++++++++---------- services/gitdiff/gitdiff.go | 34 ++++++++++++++++++++----- services/mailer/mail.go | 4 +-- services/pull/review.go | 12 ++++----- 7 files changed, 84 insertions(+), 72 deletions(-) diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index 6e9d2357cbbaf..d186e8943df12 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -16,7 +16,7 @@ import ( ) // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS -type CodeComments map[string]map[string][]*Comment +type CodeComments map[string][]*Comment // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) { @@ -41,10 +41,9 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u for _, comment := range comments { if pathToLineToComment[comment.TreePath] == nil { - pathToLineToComment[comment.TreePath] = make(map[string][]*Comment) + pathToLineToComment[comment.TreePath] = make([]*Comment, 0) } - lineContent := comment.GetLineContent() - pathToLineToComment[comment.TreePath][lineContent] = append(pathToLineToComment[comment.TreePath][lineContent], comment) + pathToLineToComment[comment.TreePath] = append(pathToLineToComment[comment.TreePath], comment) } return pathToLineToComment, nil } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index e7ad02c0c25ec..26dac7940736f 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1728,23 +1728,21 @@ func ViewIssue(ctx *context.Context) { return } for _, codeComments := range comment.Review.CodeComments { - for _, lineComments := range codeComments { - for _, c := range lineComments { - // Check tag. - role, ok = marked[c.PosterID] - if ok { - c.ShowRole = role - continue - } + for _, c := range codeComments { + // Check tag. + role, ok = marked[c.PosterID] + if ok { + c.ShowRole = role + continue + } - c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, c.HasOriginalAuthor()) - if err != nil { - ctx.ServerError("roleDescriptor", err) - return - } - marked[c.PosterID] = c.ShowRole - participants = addParticipant(c.Poster, participants) + c.ShowRole, err = roleDescriptor(ctx, repo, c.Poster, issue, c.HasOriginalAuthor()) + if err != nil { + ctx.ServerError("roleDescriptor", err) + return } + marked[c.PosterID] = c.ShowRole + participants = addParticipant(c.Poster, participants) } } if err = comment.LoadResolveDoer(ctx); err != nil { diff --git a/services/convert/pull_review.go b/services/convert/pull_review.go index 29a5ab7466b13..7b56fd39f2443 100644 --- a/services/convert/pull_review.go +++ b/services/convert/pull_review.go @@ -90,31 +90,30 @@ func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, d apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments)) for _, lines := range review.CodeComments { - for _, comments := range lines { - for _, comment := range comments { - apiComment := &api.PullReviewComment{ - ID: comment.ID, - Body: comment.Content, - Poster: ToUser(ctx, comment.Poster, doer), - Resolver: ToUser(ctx, comment.ResolveDoer, doer), - ReviewID: review.ID, - Created: comment.CreatedUnix.AsTime(), - Updated: comment.UpdatedUnix.AsTime(), - Path: comment.TreePath, - CommitID: comment.CommitSHA, - OrigCommitID: comment.OldRef, - DiffHunk: patch2diff(comment.Patch), - HTMLURL: comment.HTMLURL(ctx), - HTMLPullURL: review.Issue.HTMLURL(), - } + for _, comment := range lines { + apiComment := &api.PullReviewComment{ + ID: comment.ID, + Body: comment.Content, + Poster: ToUser(ctx, comment.Poster, doer), + Resolver: ToUser(ctx, comment.ResolveDoer, doer), + ReviewID: review.ID, + Created: comment.CreatedUnix.AsTime(), + Updated: comment.UpdatedUnix.AsTime(), + Path: comment.TreePath, + CommitID: comment.CommitSHA, + OrigCommitID: comment.OldRef, + DiffHunk: patch2diff(comment.Patch), + HTMLURL: comment.HTMLURL(ctx), + HTMLPullURL: review.Issue.HTMLURL(), + } - if comment.Line < 0 { - apiComment.OldLineNum = comment.UnsignedLine() - } else { - apiComment.LineNum = comment.UnsignedLine() - } - apiComments = append(apiComments, apiComment) + if comment.Line < 0 { + apiComment.OldLineNum = comment.UnsignedLine() + } else { + apiComment.LineNum = comment.UnsignedLine() } + apiComments = append(apiComments, apiComment) + } } return apiComments, nil diff --git a/services/feed/action.go b/services/feed/action.go index 83daaa1438fd5..4c3187ae031d7 100644 --- a/services/feed/action.go +++ b/services/feed/action.go @@ -224,20 +224,18 @@ func (a *actionNotifier) PullRequestReview(ctx context.Context, pr *issues_model actions := make([]*activities_model.Action, 0, 10) for _, lines := range review.CodeComments { - for _, comments := range lines { - for _, comm := range comments { - actions = append(actions, &activities_model.Action{ - ActUserID: review.Reviewer.ID, - ActUser: review.Reviewer, - Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comm.Content, "\n")[0]), - OpType: activities_model.ActionCommentPull, - RepoID: review.Issue.RepoID, - Repo: review.Issue.Repo, - IsPrivate: review.Issue.Repo.IsPrivate, - Comment: comm, - CommentID: comm.ID, - }) - } + for _, comment := range lines { + actions = append(actions, &activities_model.Action{ + ActUserID: review.Reviewer.ID, + ActUser: review.Reviewer, + Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comment.Content, "\n")[0]), + OpType: activities_model.ActionCommentPull, + RepoID: review.Issue.RepoID, + Repo: review.Issue.Repo, + IsPrivate: review.Issue.Repo.IsPrivate, + Comment: comment, + CommentID: comment.ID, + }) } } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 824aa022b9b43..4be7adb351fcf 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -472,15 +472,35 @@ func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, c return err } for _, file := range diff.Files { - if lineCommits, ok := allComments[file.Name]; ok { - for _, section := range file.Sections { - for _, line := range section.Lines { - if comments, ok := lineCommits[line.Content]; ok { - line.Comments = append(line.Comments, comments...) + + if comments, ok := allComments[file.Name]; ok { + for _, comment := range comments { + lineContent := comment.GetLineContent() + + for _, section := range file.Sections { + diffLineMap := make(map[string][]*DiffLine, len(section.Lines)) + for _, line := range section.Lines { + if _, ok := diffLineMap[line.Content]; !ok { + diffLineMap[line.Content] = make([]*DiffLine, 0) + } + diffLineMap[line.Content] = append(diffLineMap[line.Content], line) } - if comments, ok := lineCommits[""]; ok { - line.Comments = append(line.Comments, comments...) + + if diffLines, ok := diffLineMap[lineContent]; ok { + if len(diffLines) > 1 { + for _, diffLine := range diffLines { + if comment.Line == int64(diffLine.LeftIdx*-1) || comment.Line == int64(diffLine.RightIdx) { + diffLine.Comments = append(diffLine.Comments, comment) + } + } + } else if len(diffLines) == 1 { + diffLines[0].Comments = append(diffLines[0].Comments, comment) + } } + } + } + for _, section := range file.Sections { + for _, line := range section.Lines { sort.SliceStable(line.Comments, func(i, j int) bool { return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix }) diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 000cc835c8b77..83e3ca1729f7a 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -242,8 +242,8 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient if ctx.Comment != nil && ctx.Comment.Review != nil { reviewComments = make([]*issues_model.Comment, 0, 10) for _, lines := range ctx.Comment.Review.CodeComments { - for _, comments := range lines { - reviewComments = append(reviewComments, comments...) + for _, comment := range lines { + reviewComments = append(reviewComments, comment) } } } diff --git a/services/pull/review.go b/services/pull/review.go index 3d5eca779f8e9..b510a7b5bf112 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -329,14 +329,12 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos notify_service.PullRequestReview(ctx, pr, review, comm, mentions) for _, lines := range review.CodeComments { - for _, comments := range lines { - for _, codeComment := range comments { - mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, doer, codeComment.Content) - if err != nil { - return nil, nil, err - } - notify_service.PullRequestCodeComment(ctx, pr, codeComment, mentions) + for _, comment := range lines { + mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, doer, comment.Content) + if err != nil { + return nil, nil, err } + notify_service.PullRequestCodeComment(ctx, pr, comment, mentions) } } From 03e6cb988a9cc1680043e4c78491203993f1f33e Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 07:05:54 +0000 Subject: [PATCH 07/11] improve logic --- models/issues/comment_code.go | 7 --- routers/web/repo/pull.go | 2 +- services/gitdiff/gitdiff.go | 54 ++++++++++++------- .../repo/issue/view_content/comments.tmpl | 6 +-- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index d186e8943df12..d0c1e14f04b97 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -8,7 +8,6 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -67,12 +66,6 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu return nil, err } - if opts.LineContent != "" { - comments = container.FilterSlice(comments, func(c *Comment) (*Comment, bool) { - return c, c.GetLineContent() == opts.LineContent - }) - } - if err := issue.LoadRepo(ctx); err != nil { return nil, err } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 92e0a1674e080..e85636e9f0dea 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -752,7 +752,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi "numberOfViewedFiles": diff.NumViewedFiles, } - if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil { + if err = diff.LoadComments(ctx, issue, gitRepo, diffOptions, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil { ctx.ServerError("LoadComments", err) return } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 4be7adb351fcf..a7a5402ea9bc1 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -466,35 +466,53 @@ type Diff struct { } // LoadComments loads comments into each line -func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error { +func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, gitRepo *git.Repository, diffOptions *DiffOptions, currentUser *user_model.User, showOutdatedComments bool) error { allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments) if err != nil { return err } for _, file := range diff.Files { - if comments, ok := allComments[file.Name]; ok { for _, comment := range comments { - lineContent := comment.GetLineContent() + diffOptions.BeforeCommitID = comment.CommitSHA + diffOptions.MaxFiles = 1 + d, err := GetDiff(ctx, gitRepo, diffOptions, file.Name) + if err != nil { + return err + } - for _, section := range file.Sections { - diffLineMap := make(map[string][]*DiffLine, len(section.Lines)) - for _, line := range section.Lines { - if _, ok := diffLineMap[line.Content]; !ok { - diffLineMap[line.Content] = make([]*DiffLine, 0) + deleted := false + done := false + if len(d.Files) > 0 { + f := d.Files[0] + for _, s := range f.Sections { + for _, l := range s.Lines { + if l.LeftIdx == int(comment.Line) { + if l.Type == DiffLineDel { + // this line has been deleted so skip it + deleted = true + } else { + // update new line + comment.Line = int64(l.RightIdx) + } + done = true + break + } + } + if done { + break } - diffLineMap[line.Content] = append(diffLineMap[line.Content], line) } + } - if diffLines, ok := diffLineMap[lineContent]; ok { - if len(diffLines) > 1 { - for _, diffLine := range diffLines { - if comment.Line == int64(diffLine.LeftIdx*-1) || comment.Line == int64(diffLine.RightIdx) { - diffLine.Comments = append(diffLine.Comments, comment) - } - } - } else if len(diffLines) == 1 { - diffLines[0].Comments = append(diffLines[0].Comments, comment) + if deleted { + continue + } + + for _, section := range file.Sections { + for _, line := range section.Lines { + if comment.Line == int64(line.RightIdx) { + line.Comments = append(line.Comments, comment) } } } diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 3da2f3815ee3f..9196b6a1dc05e 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -456,10 +456,8 @@ {{if and .Review .Review.CodeComments}} - {{range $filename, $lines := .Review.CodeComments}} - {{range $line, $comms := $lines}} - {{template "repo/issue/view_content/conversation" dict "." $ "comments" $comms}} - {{end}} + {{range $filename, $comms := .Review.CodeComments}} + {{template "repo/issue/view_content/conversation" dict "." $ "comments" $comms}} {{end}} {{end}} From df4e76ba3507836e305353ed9587e8dc279e3c2f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 07:27:50 +0000 Subject: [PATCH 08/11] revert unnecessary changes --- models/fixtures/comment.yml | 6 +++--- models/issues/comment.go | 11 ----------- models/issues/comment_code.go | 11 +++++------ routers/web/repo/pull_review.go | 8 ++++---- services/forms/repo_form.go | 1 - templates/repo/diff/comment_form.tmpl | 1 - templates/repo/diff/comment_form_datahandler.tmpl | 2 +- templates/repo/diff/conversation.tmpl | 2 +- templates/repo/issue/view_content/conversation.tmpl | 2 +- 9 files changed, 15 insertions(+), 29 deletions(-) diff --git a/models/fixtures/comment.yml b/models/fixtures/comment.yml index bdbc9b852cd03..74fc716180d1d 100644 --- a/models/fixtures/comment.yml +++ b/models/fixtures/comment.yml @@ -29,8 +29,8 @@ issue_id: 2 content: "meh..." review_id: 4 + line: 4 tree_path: "README.md" - patch: "+ # repo1" created_unix: 946684812 invalidated: false - @@ -39,7 +39,7 @@ poster_id: 1 issue_id: 2 content: "meh..." - patch: "- " + line: -4 tree_path: "README.md" created_unix: 946684812 invalidated: false @@ -50,7 +50,7 @@ poster_id: 1 issue_id: 2 content: "it's already invalidated. boring..." - patch: "- " + line: -4 tree_path: "README.md" created_unix: 946684812 invalidated: true diff --git a/models/issues/comment.go b/models/issues/comment.go index 233f4b0f98aba..c6c5dc24321d1 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -10,7 +10,6 @@ import ( "fmt" "html/template" "strconv" - "strings" "unicode/utf8" "code.gitea.io/gitea/models/db" @@ -784,15 +783,6 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) { return err } -func (c *Comment) GetLineContent() string { - // always show the comment at the last line - patchLines := strings.Split(c.Patch, "\n") - if len(patchLines) == 0 { - return "" - } - return patchLines[len(patchLines)-1] -} - // CreateComment creates comment with context func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) { ctx, committer, err := db.TxContext(ctx) @@ -1043,7 +1033,6 @@ type FindCommentsOptions struct { Since int64 Before int64 Line int64 - LineContent string TreePath string Type CommentType IssueIDs []int64 diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index d0c1e14f04b97..63564f4d5ac6d 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -127,13 +127,12 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu } // FetchCodeCommentsByLine fetches the code comments for a given treePath and line number -func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, lineContent string, showOutdatedComments bool) (CommentList, error) { +func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) (CommentList, error) { opts := FindCommentsOptions{ - Type: CommentTypeCode, - IssueID: issue.ID, - TreePath: treePath, - Line: line, - LineContent: lineContent, + Type: CommentTypeCode, + IssueID: issue.ID, + TreePath: treePath, + Line: line, } return findCodeComments(ctx, opts, issue, currentUser, nil, showOutdatedComments) } diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 1fb0d7cdb7804..62f6d71c5e5cf 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -110,7 +110,7 @@ func CreateCodeComment(ctx *context.Context) { log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID) - renderConversation(ctx, comment, form.Origin, form.LineContent) + renderConversation(ctx, comment, form.Origin) } // UpdateResolveConversation add or remove an Conversation resolved mark @@ -161,14 +161,14 @@ func UpdateResolveConversation(ctx *context.Context) { return } - renderConversation(ctx, comment, origin, ctx.FormString("line_content")) + renderConversation(ctx, comment, origin) } -func renderConversation(ctx *context.Context, comment *issues_model.Comment, origin, lineContent string) { +func renderConversation(ctx *context.Context, comment *issues_model.Comment, origin string) { ctx.Data["PageIsPullFiles"] = origin == "diff" showOutdatedComments := origin == "timeline" || ctx.Data["ShowOutdatedComments"].(bool) - comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, lineContent, showOutdatedComments) + comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, showOutdatedComments) if err != nil { ctx.ServerError("FetchCodeCommentsByLine", err) return diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index fde0b1145a46c..32d96abf4d272 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -587,7 +587,6 @@ type CodeCommentForm struct { Content string `binding:"Required"` Side string `binding:"Required;In(previous,proposed)"` Line int64 - LineContent string `binding:"Required"` TreePath string `form:"path" binding:"Required"` SingleReview bool `form:"single_review"` Reply int64 `form:"reply"` diff --git a/templates/repo/diff/comment_form.tmpl b/templates/repo/diff/comment_form.tmpl index 9abcc2121a5f4..856b3da01a345 100644 --- a/templates/repo/diff/comment_form.tmpl +++ b/templates/repo/diff/comment_form.tmpl @@ -5,7 +5,6 @@ - diff --git a/templates/repo/diff/comment_form_datahandler.tmpl b/templates/repo/diff/comment_form_datahandler.tmpl index 88e2a8a9793db..d0e493488df0a 100644 --- a/templates/repo/diff/comment_form_datahandler.tmpl +++ b/templates/repo/diff/comment_form_datahandler.tmpl @@ -1,5 +1,5 @@ {{if $.comment}} - {{template "repo/diff/comment_form" dict "root" $.root "hidden" $.hidden "reply" $.reply "Line" $.comment.UnsignedLine "LineContent" $.comment.GetLineContent "File" $.comment.TreePath "Side" $.comment.DiffSide "HasComments" true}} + {{template "repo/diff/comment_form" dict "root" $.root "hidden" $.hidden "reply" $.reply "Line" $.comment.UnsignedLine "File" $.comment.TreePath "Side" $.comment.DiffSide "HasComments" true}} {{else if $.root}} {{template "repo/diff/comment_form" $}} {{else}} diff --git a/templates/repo/diff/conversation.tmpl b/templates/repo/diff/conversation.tmpl index 4eee1490eb152..08f60644b3b52 100644 --- a/templates/repo/diff/conversation.tmpl +++ b/templates/repo/diff/conversation.tmpl @@ -50,7 +50,7 @@ {{if and $.CanMarkConversation $hasReview (not $isReviewPending)}} - + {{if $resolved}} {{ctx.Locale.Tr "repo.issues.review.un_resolve_conversation"}} {{else}} diff --git a/templates/repo/issue/view_content/conversation.tmpl b/templates/repo/issue/view_content/conversation.tmpl index 648e2fe932a45..ccea9b690d7e8 100644 --- a/templates/repo/issue/view_content/conversation.tmpl +++ b/templates/repo/issue/view_content/conversation.tmpl @@ -120,7 +120,7 @@ {{if and $.CanMarkConversation $hasReview (not $isReviewPending)}} - + {{if $resolved}} {{ctx.Locale.Tr "repo.issues.review.un_resolve_conversation"}} {{else}} From 27fd7f49edc48ba3588675bc1eb94f5b63e1b014 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 07:30:05 +0000 Subject: [PATCH 09/11] revert --- templates/repo/diff/section_split.tmpl | 8 ++++---- templates/repo/diff/section_unified.tmpl | 2 +- web_src/js/features/repo-diff.js | 3 +-- web_src/js/features/repo-issue.js | 2 -- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 7fab9c64a3437..37b42bcb376ab 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -49,7 +49,7 @@ {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -64,7 +64,7 @@ {{if $match.RightIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -81,7 +81,7 @@ {{if $line.LeftIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* @@ -96,7 +96,7 @@ {{if $line.RightIdx}}{{end}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index dffa4eda45543..708b333291641 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -54,7 +54,7 @@ {{else}} {{/* */}}{{if and $.root.SignedUserID $.root.PageIsPullFiles}}{{/* - */}}{{/* + */}}{{/* */}}{{svg "octicon-plus"}}{{/* */}}{{/* */}}{{end}}{{/* diff --git a/web_src/js/features/repo-diff.js b/web_src/js/features/repo-diff.js index ba526d3691551..00f74515df6a8 100644 --- a/web_src/js/features/repo-diff.js +++ b/web_src/js/features/repo-diff.js @@ -100,11 +100,10 @@ function initRepoDiffConversationForm() { const comment_id = $(this).data('comment-id'); const origin = $(this).data('origin'); const action = $(this).data('action'); - const line_content = $(this).data('line-content'); const url = $(this).data('update-url'); try { - const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id, line_content})}); + const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})}); const data = await response.text(); if ($(this).closest('.conversation-holder').length) { diff --git a/web_src/js/features/repo-issue.js b/web_src/js/features/repo-issue.js index e35c125c8397a..95910e34bc9af 100644 --- a/web_src/js/features/repo-issue.js +++ b/web_src/js/features/repo-issue.js @@ -524,7 +524,6 @@ export function initRepoPullRequestReview() { const isSplit = this.closest('.code-diff')?.classList.contains('code-diff-split'); const side = this.getAttribute('data-side'); const idx = this.getAttribute('data-idx'); - const content = this.getAttribute('data-content'); const path = this.closest('[data-path]')?.getAttribute('data-path'); const tr = this.closest('tr'); const lineType = tr.getAttribute('data-line-type'); @@ -552,7 +551,6 @@ export function initRepoPullRequestReview() { const html = await response.text(); $td.html(html); $td.find("input[name='line']").val(idx); - $td.find("input[name='line_content']").val(content); $td.find("input[name='side']").val(side === 'left' ? 'previous' : 'proposed'); $td.find("input[name='path']").val(path); From 9ab94b0627684806e0ba79be5e93e48d33da7f7c Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 07:50:17 +0000 Subject: [PATCH 10/11] fix unit test --- models/issues/comment_test.go | 11 +++-------- models/issues/review_test.go | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/models/issues/comment_test.go b/models/issues/comment_test.go index 1844aae0739fd..c32278add0077 100644 --- a/models/issues/comment_test.go +++ b/models/issues/comment_test.go @@ -51,16 +51,11 @@ func TestFetchCodeComments(t *testing.T) { issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false) - lineContent := "+ # repo1" assert.NoError(t, err) assert.Contains(t, res, "README.md") - assert.Contains(t, res["README.md"], lineContent) - assert.Len(t, res["README.md"][lineContent], 1) - assert.Equal(t, int64(4), res["README.md"][lineContent][0].ID) - lineContent = "- " - assert.Contains(t, res["README.md"], lineContent) - assert.Len(t, res["README.md"][lineContent], 1) - assert.Equal(t, int64(5), res["README.md"][lineContent][0].ID) + assert.Len(t, res["README.md"], 2) + assert.Equal(t, res["README.md"][0].ID, int64(4)) + assert.Equal(t, res["README.md"][1].ID, int64(5)) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false) diff --git a/models/issues/review_test.go b/models/issues/review_test.go index cfeed0e704834..ab9f56a734c18 100644 --- a/models/issues/review_test.go +++ b/models/issues/review_test.go @@ -48,8 +48,7 @@ func TestReview_LoadCodeComments(t *testing.T) { assert.NoError(t, review.LoadAttributes(db.DefaultContext)) assert.NoError(t, review.LoadCodeComments(db.DefaultContext)) assert.Len(t, review.CodeComments, 1) - lineContent := "+ # repo1" - assert.Equal(t, int64(4), review.CodeComments["README.md"][lineContent][0].ID) + assert.Equal(t, int64(4), review.CodeComments["README.md"][0].Line) } func TestReviewType_Icon(t *testing.T) { From 293da205e6324953b0f6c94f985b00d042b5cc80 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jun 2024 07:51:21 +0000 Subject: [PATCH 11/11] revert changes in tests --- routers/web/repo/pull_review_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/pull_review_test.go b/routers/web/repo/pull_review_test.go index 464b56498cf64..8344ff409113f 100644 --- a/routers/web/repo/pull_review_test.go +++ b/routers/web/repo/pull_review_test.go @@ -56,29 +56,29 @@ func TestRenderConversation(t *testing.T) { } run("diff with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) { ctx.Data["ShowOutdatedComments"] = true - renderConversation(ctx, preparedComment, "diff", preparedComment.GetLineContent()) + renderConversation(ctx, preparedComment, "diff") assert.Contains(t, resp.Body.String(), `