diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index 304ac4569f..d447d7542c 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -18,11 +18,11 @@ import ( type CodeComments map[string]map[int64][]*Comment // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line -func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User) (CodeComments, error) { - return fetchCodeCommentsByReview(ctx, issue, currentUser, nil) +func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) { + return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments) } -func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review) (CodeComments, error) { +func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) (CodeComments, error) { pathToLineToComment := make(CodeComments) if review == nil { review = &Review{ID: 0} @@ -33,7 +33,7 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u ReviewID: review.ID, } - comments, err := findCodeComments(ctx, opts, issue, currentUser, review) + comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments) if err != nil { return nil, err } @@ -47,15 +47,17 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u return pathToLineToComment, nil } -func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review) ([]*Comment, error) { +func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) ([]*Comment, error) { var comments CommentList if review == nil { review = &Review{ID: 0} } conds := opts.ToConds() - if review.ID == 0 { + + if !showOutdatedComments && review.ID == 0 { conds = conds.And(builder.Eq{"invalidated": false}) } + e := db.GetEngine(ctx) if err := e.Where(conds). Asc("comment.created_unix"). @@ -118,12 +120,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) ([]*Comment, error) { +func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) ([]*Comment, error) { opts := FindCommentsOptions{ Type: CommentTypeCode, IssueID: issue.ID, TreePath: treePath, Line: line, } - return findCodeComments(ctx, opts, issue, currentUser, nil) + return findCodeComments(ctx, opts, issue, currentUser, nil, showOutdatedComments) } diff --git a/models/issues/comment_test.go b/models/issues/comment_test.go index 43bad1660f..d766625be3 100644 --- a/models/issues/comment_test.go +++ b/models/issues/comment_test.go @@ -50,7 +50,7 @@ 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) + res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false) assert.NoError(t, err) assert.Contains(t, res, "README.md") assert.Contains(t, res["README.md"], int64(4)) @@ -58,7 +58,7 @@ func TestFetchCodeComments(t *testing.T) { assert.Equal(t, int64(4), res["README.md"][4][0].ID) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2) + res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false) assert.NoError(t, err) assert.Len(t, res, 1) } diff --git a/models/issues/review.go b/models/issues/review.go index 3a1ab7468a..3685c65ce5 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -141,7 +141,7 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) { if err = r.loadIssue(ctx); err != nil { return } - r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r) + r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false) return err } diff --git a/models/user/setting_keys.go b/models/user/setting_keys.go index 10255735b3..72b3974eee 100644 --- a/models/user/setting_keys.go +++ b/models/user/setting_keys.go @@ -8,6 +8,8 @@ const ( SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types" // SettingsKeyDiffWhitespaceBehavior is the setting key for whitespace behavior of diff SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour" + // SettingsKeyShowOutdatedComments is the setting key wether or not to show outdated comments in PRs + SettingsKeyShowOutdatedComments = "comment_code.show_outdated" // UserActivityPubPrivPem is user's private key UserActivityPubPrivPem = "activitypub.priv_pem" // UserActivityPubPubPem is user's public key diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a2c67fbf55..70802fc4ca 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1611,6 +1611,9 @@ issues.review.pending.tooltip = This comment is not currently visible to other u issues.review.review = Review issues.review.reviewers = Reviewers issues.review.outdated = Outdated +issues.review.outdated_description = Content has changed since this comment was made +issues.review.option.show_outdated_comments = Show outdated comments +issues.review.option.hide_outdated_comments = Hide outdated comments issues.review.show_outdated = Show outdated issues.review.hide_outdated = Hide outdated issues.review.show_resolved = Show resolved diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go index 5c38b31154..216550ca99 100644 --- a/routers/web/repo/middlewares.go +++ b/routers/web/repo/middlewares.go @@ -5,6 +5,7 @@ package repo import ( "fmt" + "strconv" system_model "code.gitea.io/gitea/models/system" user_model "code.gitea.io/gitea/models/user" @@ -88,3 +89,27 @@ func SetWhitespaceBehavior(ctx *context.Context) { ctx.Data["WhitespaceBehavior"] = whitespaceBehavior } } + +// SetShowOutdatedComments set the show outdated comments option as context variable +func SetShowOutdatedComments(ctx *context.Context) { + showOutdatedCommentsValue := ctx.FormString("show-outdated") + // var showOutdatedCommentsValue string + + if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" { + // invalid or no value for this form string -> use default or stored user setting + if ctx.IsSigned { + showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, "false") + } else { + // not logged in user -> use the default value + showOutdatedCommentsValue = "false" + } + } else { + // valid value -> update user setting if user is logged in + if ctx.IsSigned { + _ = user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue) + } + } + + showOutdatedComments, _ := strconv.ParseBool(showOutdatedCommentsValue) + ctx.Data["ShowOutdatedComments"] = showOutdatedComments +} diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index ef9d5856da..f2a58a35a7 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -761,7 +761,7 @@ func ViewPullFiles(ctx *context.Context) { "numberOfViewedFiles": diff.NumViewedFiles, } - if err = diff.LoadComments(ctx, issue, ctx.Doer); err != nil { + if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil { ctx.ServerError("LoadComments", err) return } diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 69d36ff4a4..5aa5811367 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -159,7 +159,7 @@ func UpdateResolveConversation(ctx *context.Context) { } func renderConversation(ctx *context.Context, comment *issues_model.Comment) { - comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line) + comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, ctx.Data["ShowOutdatedComments"].(bool)) if err != nil { ctx.ServerError("FetchCodeCommentsByLine", err) return diff --git a/routers/web/web.go b/routers/web/web.go index a7573b38f5..26ad2d54c3 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1037,7 +1037,7 @@ func registerRoutes(m *web.Route) { m.Post("/dismiss_review", reqRepoAdmin, web.Bind(forms.DismissReviewForm{}), repo.DismissReview) m.Post("/status", reqRepoIssuesOrPullsWriter, repo.UpdateIssueStatus) m.Post("/delete", reqRepoAdmin, repo.BatchDeleteIssues) - m.Post("/resolve_conversation", reqRepoIssuesOrPullsReader, repo.UpdateResolveConversation) + m.Post("/resolve_conversation", reqRepoIssuesOrPullsReader, repo.SetShowOutdatedComments, repo.UpdateResolveConversation) m.Post("/attachments", repo.UploadIssueAttachment) m.Post("/attachments/remove", repo.DeleteAttachment) m.Delete("/unpin/{index}", reqRepoAdmin, repo.IssueUnpin) @@ -1285,10 +1285,10 @@ func registerRoutes(m *web.Route) { m.Post("/set_allow_maintainer_edit", web.Bind(forms.UpdateAllowEditsForm{}), repo.SetAllowEdits) m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest) m.Group("/files", func() { - m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles) + m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles) m.Group("/reviews", func() { m.Get("/new_comment", repo.RenderNewCodeCommentForm) - m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.CreateCodeComment) + m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.SetShowOutdatedComments, repo.CreateCodeComment) m.Post("/submit", web.Bind(forms.SubmitReviewForm{}), repo.SubmitReview) }, context.RepoMustNotBeArchived()) }) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index b6a75f6098..9adf3b9400 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -450,8 +450,8 @@ type Diff struct { } // LoadComments loads comments into each line -func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User) error { - allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser) +func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error { + allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments) if err != nil { return err } diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 389f787dfc..e270e46fd4 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -594,16 +594,26 @@ func setupDefaultDiff() *Diff { } } -func TestDiff_LoadComments(t *testing.T) { +func TestDiff_LoadCommentsNoOutdated(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) diff := setupDefaultDiff() - assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user)) + assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user, false)) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) } +func TestDiff_LoadCommentsWithOutdated(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}) + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + diff := setupDefaultDiff() + assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user, true)) + assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 3) +} + func TestDiffLine_CanComment(t *testing.T) { assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment()) assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*issues_model.Comment{{Content: "bla"}}}).CanComment()) diff --git a/templates/repo/diff/comments.tmpl b/templates/repo/diff/comments.tmpl index 5b0b32cc97..d19ac1f047 100644 --- a/templates/repo/diff/comments.tmpl +++ b/templates/repo/diff/comments.tmpl @@ -31,6 +31,12 @@ {{end}}
+ {{if .Invalidated}} + {{$referenceUrl := printf "%s#%s" $.root.Issue.Link .HashTag}} + + {{$.root.locale.Tr "repo.issues.review.outdated"}} + + {{end}} {{if and .Review}} {{if eq .Review.Type 0}}
diff --git a/templates/repo/diff/conversation.tmpl b/templates/repo/diff/conversation.tmpl index 8d4064967e..ce344794ea 100644 --- a/templates/repo/diff/conversation.tmpl +++ b/templates/repo/diff/conversation.tmpl @@ -1,14 +1,25 @@ {{$resolved := (index .comments 0).IsResolved}} +{{$invalid := (index .comments 0).Invalidated}} {{$resolveDoer := (index .comments 0).ResolveDoer}} {{$isNotPending := (not (eq (index .comments 0).Review.Type 0))}} +{{$referenceUrl := printf "%s#%s" $.Issue.Link (index .comments 0).HashTag}}
{{if $resolved}}
-
+
{{svg "octicon-check" 16 "icon gt-mr-2"}} {{$resolveDoer.Name}} {{$.locale.Tr "repo.issues.review.resolved_by"}} + {{if $invalid}} + + + {{$.locale.Tr "repo.issues.review.outdated"}} + + {{end}}
-
+
diff --git a/templates/repo/diff/whitespace_dropdown.tmpl b/templates/repo/diff/whitespace_dropdown.tmpl index 5a5b8c3dbd..a1830cb288 100644 --- a/templates/repo/diff/whitespace_dropdown.tmpl +++ b/templates/repo/diff/whitespace_dropdown.tmpl @@ -1,25 +1,25 @@ -{{if .IsSplitStyle}}{{svg "gitea-join"}}{{else}}{{svg "gitea-split"}}{{end}} +{{if .IsSplitStyle}}{{svg "gitea-join"}}{{else}}{{svg "gitea-split"}}{{end}} diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index bfcd292f44..9097a180e3 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -488,7 +488,7 @@
{{$filename}} {{if $invalid}} - + {{$.locale.Tr "repo.issues.review.outdated"}} {{end}} diff --git a/web_src/css/repo.css b/web_src/css/repo.css index fa97393421..c84b9e0f35 100644 --- a/web_src/css/repo.css +++ b/web_src/css/repo.css @@ -1702,6 +1702,10 @@ .repository .diff-box .resolved-placeholder { display: flex; align-items: center; + font-size: 14px !important; + height: 36px; + padding-top: 0; + padding-bottom: 0; } .repository .diff-box .resolved-placeholder .button { @@ -1728,10 +1732,6 @@ text-align: center; } -.repository .diff-file-box .code-diff { - font-size: 12px; -} - .repository .diff-file-box .code-diff td { padding: 0 0 0 10px !important; border-top: 0; @@ -2495,14 +2495,17 @@ left: 7px; } -.comment-header .actions a { - margin-right: 0 !important; +.comment-header .actions a:not(.label) { padding: 0.5rem !important; } -.comment-header-left > * + *, -.comment-header-right > * + * { - margin-left: 0.25rem; +.comment-header .actions .label { + margin: 0 !important; +} + +.comment-header-left, +.comment-header-right { + gap: 4px; } .comment-body { diff --git a/web_src/css/review.css b/web_src/css/review.css index dce23bcd74..263f821f38 100644 --- a/web_src/css/review.css +++ b/web_src/css/review.css @@ -128,7 +128,7 @@ } .comment-code-cloud .attached.header { - padding: 0.1rem 1rem; + padding: 1px 8px 1px 12px; } .comment-code-cloud .attached.header .text {