Skip to content

Commit 30a7838

Browse files
Show outdated comments in files changed tab (#24936) (#25428)
Backport #24936 If enabled show a clickable label in the comment. A click on the label opens the Conversation tab with the comment focussed - there you're able to view the old diff (or original diff the comment was created on). **Screenshots** ![image](https://github.com/go-gitea/gitea/assets/1135157/63ab9571-a9ee-4900-9f02-94ab0095f9e7) ![image](https://github.com/go-gitea/gitea/assets/1135157/78f7c225-8d76-46f5-acfd-9b8aab988a6c) When resolved and outdated: ![image](https://github.com/go-gitea/gitea/assets/1135157/6ece9ebd-c792-4aa5-9c35-628694e9d093) Option to enable/disable this (stored in user settings - default is disabled): ![image](https://github.com/go-gitea/gitea/assets/1135157/ed99dfe4-76dc-4c12-bd96-e7e62da50ab5) ![image](https://github.com/go-gitea/gitea/assets/1135157/e837a052-e92e-4a28-906d-9db5bacf93a6) fixes #24913 Co-authored-by: silverwind <[email protected]>
1 parent cb3173a commit 30a7838

File tree

18 files changed

+115
-38
lines changed

18 files changed

+115
-38
lines changed

models/issues/comment_code.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
type CodeComments map[string]map[int64][]*Comment
1919

2020
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
21-
func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User) (CodeComments, error) {
22-
return fetchCodeCommentsByReview(ctx, issue, currentUser, nil)
21+
func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) {
22+
return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments)
2323
}
2424

25-
func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review) (CodeComments, error) {
25+
func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) (CodeComments, error) {
2626
pathToLineToComment := make(CodeComments)
2727
if review == nil {
2828
review = &Review{ID: 0}
@@ -33,7 +33,7 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u
3333
ReviewID: review.ID,
3434
}
3535

36-
comments, err := findCodeComments(ctx, opts, issue, currentUser, review)
36+
comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments)
3737
if err != nil {
3838
return nil, err
3939
}
@@ -47,15 +47,17 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u
4747
return pathToLineToComment, nil
4848
}
4949

50-
func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review) ([]*Comment, error) {
50+
func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) ([]*Comment, error) {
5151
var comments CommentList
5252
if review == nil {
5353
review = &Review{ID: 0}
5454
}
5555
conds := opts.ToConds()
56-
if review.ID == 0 {
56+
57+
if !showOutdatedComments && review.ID == 0 {
5758
conds = conds.And(builder.Eq{"invalidated": false})
5859
}
60+
5961
e := db.GetEngine(ctx)
6062
if err := e.Where(conds).
6163
Asc("comment.created_unix").
@@ -118,12 +120,12 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
118120
}
119121

120122
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
121-
func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64) ([]*Comment, error) {
123+
func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) ([]*Comment, error) {
122124
opts := FindCommentsOptions{
123125
Type: CommentTypeCode,
124126
IssueID: issue.ID,
125127
TreePath: treePath,
126128
Line: line,
127129
}
128-
return findCodeComments(ctx, opts, issue, currentUser, nil)
130+
return findCodeComments(ctx, opts, issue, currentUser, nil, showOutdatedComments)
129131
}

models/issues/comment_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func TestFetchCodeComments(t *testing.T) {
5050

5151
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
5252
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
53-
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user)
53+
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false)
5454
assert.NoError(t, err)
5555
assert.Contains(t, res, "README.md")
5656
assert.Contains(t, res["README.md"], int64(4))
5757
assert.Len(t, res["README.md"][4], 1)
5858
assert.Equal(t, int64(4), res["README.md"][4][0].ID)
5959

6060
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
61-
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2)
61+
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false)
6262
assert.NoError(t, err)
6363
assert.Len(t, res, 1)
6464
}

models/issues/review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) {
141141
if err = r.loadIssue(ctx); err != nil {
142142
return
143143
}
144-
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r)
144+
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false)
145145
return err
146146
}
147147

models/user/setting_keys.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const (
88
SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types"
99
// SettingsKeyDiffWhitespaceBehavior is the setting key for whitespace behavior of diff
1010
SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour"
11+
// SettingsKeyShowOutdatedComments is the setting key wether or not to show outdated comments in PRs
12+
SettingsKeyShowOutdatedComments = "comment_code.show_outdated"
1113
// UserActivityPubPrivPem is user's private key
1214
UserActivityPubPrivPem = "activitypub.priv_pem"
1315
// UserActivityPubPubPem is user's public key

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,9 @@ issues.review.pending.tooltip = This comment is not currently visible to other u
16061606
issues.review.review = Review
16071607
issues.review.reviewers = Reviewers
16081608
issues.review.outdated = Outdated
1609+
issues.review.outdated_description = Content has changed since this comment was made
1610+
issues.review.option.show_outdated_comments = Show outdated comments
1611+
issues.review.option.hide_outdated_comments = Hide outdated comments
16091612
issues.review.show_outdated = Show outdated
16101613
issues.review.hide_outdated = Hide outdated
16111614
issues.review.show_resolved = Show resolved

routers/web/repo/middlewares.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package repo
55

66
import (
77
"fmt"
8+
"strconv"
89

910
system_model "code.gitea.io/gitea/models/system"
1011
user_model "code.gitea.io/gitea/models/user"
@@ -88,3 +89,27 @@ func SetWhitespaceBehavior(ctx *context.Context) {
8889
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
8990
}
9091
}
92+
93+
// SetShowOutdatedComments set the show outdated comments option as context variable
94+
func SetShowOutdatedComments(ctx *context.Context) {
95+
showOutdatedCommentsValue := ctx.FormString("show-outdated")
96+
// var showOutdatedCommentsValue string
97+
98+
if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
99+
// invalid or no value for this form string -> use default or stored user setting
100+
if ctx.IsSigned {
101+
showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, "false")
102+
} else {
103+
// not logged in user -> use the default value
104+
showOutdatedCommentsValue = "false"
105+
}
106+
} else {
107+
// valid value -> update user setting if user is logged in
108+
if ctx.IsSigned {
109+
_ = user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
110+
}
111+
}
112+
113+
showOutdatedComments, _ := strconv.ParseBool(showOutdatedCommentsValue)
114+
ctx.Data["ShowOutdatedComments"] = showOutdatedComments
115+
}

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func ViewPullFiles(ctx *context.Context) {
762762
"numberOfViewedFiles": diff.NumViewedFiles,
763763
}
764764

765-
if err = diff.LoadComments(ctx, issue, ctx.Doer); err != nil {
765+
if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil {
766766
ctx.ServerError("LoadComments", err)
767767
return
768768
}

routers/web/repo/pull_review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func UpdateResolveConversation(ctx *context.Context) {
159159
}
160160

161161
func renderConversation(ctx *context.Context, comment *issues_model.Comment) {
162-
comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line)
162+
comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, ctx.Data["ShowOutdatedComments"].(bool))
163163
if err != nil {
164164
ctx.ServerError("FetchCodeCommentsByLine", err)
165165
return

routers/web/web.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ func registerRoutes(m *web.Route) {
10291029
m.Post("/request_review", reqRepoIssuesOrPullsReader, repo.UpdatePullReviewRequest)
10301030
m.Post("/dismiss_review", reqRepoAdmin, web.Bind(forms.DismissReviewForm{}), repo.DismissReview)
10311031
m.Post("/status", reqRepoIssuesOrPullsWriter, repo.UpdateIssueStatus)
1032-
m.Post("/resolve_conversation", reqRepoIssuesOrPullsReader, repo.UpdateResolveConversation)
1032+
m.Post("/resolve_conversation", reqRepoIssuesOrPullsReader, repo.SetShowOutdatedComments, repo.UpdateResolveConversation)
10331033
m.Post("/attachments", repo.UploadIssueAttachment)
10341034
m.Post("/attachments/remove", repo.DeleteAttachment)
10351035
m.Delete("/unpin/{index}", reqRepoAdmin, repo.IssueUnpin)
@@ -1277,10 +1277,10 @@ func registerRoutes(m *web.Route) {
12771277
m.Post("/set_allow_maintainer_edit", web.Bind(forms.UpdateAllowEditsForm{}), repo.SetAllowEdits)
12781278
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
12791279
m.Group("/files", func() {
1280-
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
1280+
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFiles)
12811281
m.Group("/reviews", func() {
12821282
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
1283-
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.CreateCodeComment)
1283+
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.SetShowOutdatedComments, repo.CreateCodeComment)
12841284
m.Post("/submit", web.Bind(forms.SubmitReviewForm{}), repo.SubmitReview)
12851285
}, context.RepoMustNotBeArchived())
12861286
})

services/gitdiff/gitdiff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ type Diff struct {
450450
}
451451

452452
// LoadComments loads comments into each line
453-
func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User) error {
454-
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser)
453+
func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error {
454+
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments)
455455
if err != nil {
456456
return err
457457
}

0 commit comments

Comments
 (0)