Skip to content

Commit c9b2aae

Browse files
GiteaBotwxiaoguang
andauthored
Improve user experience for outdated comments (#29050) (#29086)
Backport #29050 by wxiaoguang Try to improve #28949 1. Make `ctx.Data["ShowOutdatedComments"] = true` by default: it brings consistent user experience, and sometimes the "outdated (source changed)" comments are still valuable. 2. Show a friendly message if the comment won't show, then the end users won't fell that "the comment disappears" (it is the special case when `ShowOutdatedComments = false`) Co-authored-by: wxiaoguang <[email protected]>
1 parent 19a08c7 commit c9b2aae

File tree

5 files changed

+100
-17
lines changed

5 files changed

+100
-17
lines changed

modules/contexttest/context_tests.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@ func mockRequest(t *testing.T, reqPath string) *http.Request {
4040
return req
4141
}
4242

43+
type MockContextOption struct {
44+
Render context.Render
45+
}
46+
4347
// MockContext mock context for unit tests
44-
func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.ResponseRecorder) {
48+
func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*context.Context, *httptest.ResponseRecorder) {
49+
var opt MockContextOption
50+
if len(opts) > 0 {
51+
opt = opts[0]
52+
}
53+
if opt.Render == nil {
54+
opt.Render = &MockRender{}
55+
}
4556
resp := httptest.NewRecorder()
4657
req := mockRequest(t, reqPath)
4758
base, baseCleanUp := context.NewBaseContext(resp, req)
4859
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
4960
base.Data = middleware.GetContextData(req.Context())
5061
base.Locale = &translation.MockLocale{}
5162

52-
ctx := context.NewWebContext(base, &MockRender{}, nil)
63+
ctx := context.NewWebContext(base, opt.Render, nil)
5364

5465
chiCtx := chi.NewRouteContext()
5566
ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx)

routers/web/repo/middlewares.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,15 @@ func SetWhitespaceBehavior(ctx *context.Context) {
9292
// SetShowOutdatedComments set the show outdated comments option as context variable
9393
func SetShowOutdatedComments(ctx *context.Context) {
9494
showOutdatedCommentsValue := ctx.FormString("show-outdated")
95-
// var showOutdatedCommentsValue string
96-
9795
if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
9896
// invalid or no value for this form string -> use default or stored user setting
97+
showOutdatedCommentsValue = "true"
9998
if ctx.IsSigned {
100-
showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, "false")
101-
} else {
102-
// not logged in user -> use the default value
103-
showOutdatedCommentsValue = "false"
99+
showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
104100
}
105-
} else {
101+
} else if ctx.IsSigned {
106102
// valid value -> update user setting if user is logged in
107-
if ctx.IsSigned {
108-
_ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
109-
}
103+
_ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
110104
}
111-
112-
showOutdatedComments, _ := strconv.ParseBool(showOutdatedCommentsValue)
113-
ctx.Data["ShowOutdatedComments"] = showOutdatedComments
105+
ctx.Data["ShowOutdatedComments"], _ = strconv.ParseBool(showOutdatedCommentsValue)
114106
}

routers/web/repo/pull_review.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
const (
2424
tplDiffConversation base.TplName = "repo/diff/conversation"
25+
tplConversationOutdated base.TplName = "repo/diff/conversation_outdated"
2526
tplTimelineConversation base.TplName = "repo/issue/view_content/conversation"
2627
tplNewComment base.TplName = "repo/diff/new_comment"
2728
)
@@ -161,8 +162,8 @@ func renderConversation(ctx *context.Context, comment *issues_model.Comment, ori
161162
return
162163
}
163164
if len(comments) == 0 {
164-
// if the comments are empty (deleted, outdated, etc), it doesn't need to render anything, just return an empty body to replace "conversation-holder" on the page
165-
ctx.Resp.WriteHeader(http.StatusOK)
165+
// if the comments are empty (deleted, outdated, etc), it's better to tell the users that it is outdated
166+
ctx.HTML(http.StatusOK, tplConversationOutdated)
166167
return
167168
}
168169

routers/web/repo/pull_review_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
"net/http/httptest"
8+
"testing"
9+
10+
"code.gitea.io/gitea/models/db"
11+
issues_model "code.gitea.io/gitea/models/issues"
12+
"code.gitea.io/gitea/models/unittest"
13+
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/contexttest"
15+
"code.gitea.io/gitea/modules/templates"
16+
"code.gitea.io/gitea/services/pull"
17+
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func TestRenderConversation(t *testing.T) {
22+
unittest.PrepareTestEnv(t)
23+
24+
pr, _ := issues_model.GetPullRequestByID(db.DefaultContext, 2)
25+
_ = pr.LoadIssue(db.DefaultContext)
26+
_ = pr.Issue.LoadPoster(db.DefaultContext)
27+
_ = pr.Issue.LoadRepo(db.DefaultContext)
28+
29+
run := func(name string, cb func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder)) {
30+
t.Run(name, func(t *testing.T) {
31+
ctx, resp := contexttest.MockContext(t, "/", contexttest.MockContextOption{Render: templates.HTMLRenderer()})
32+
contexttest.LoadUser(t, ctx, pr.Issue.PosterID)
33+
contexttest.LoadRepo(t, ctx, pr.BaseRepoID)
34+
contexttest.LoadGitRepo(t, ctx)
35+
defer ctx.Repo.GitRepo.Close()
36+
cb(t, ctx, resp)
37+
})
38+
}
39+
40+
var preparedComment *issues_model.Comment
41+
run("prepare", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
42+
comment, err := pull.CreateCodeComment(ctx, pr.Issue.Poster, ctx.Repo.GitRepo, pr.Issue, 1, "content", "", false, 0, pr.HeadCommitID)
43+
if !assert.NoError(t, err) {
44+
return
45+
}
46+
comment.Invalidated = true
47+
err = issues_model.UpdateCommentInvalidate(ctx, comment)
48+
if !assert.NoError(t, err) {
49+
return
50+
}
51+
preparedComment = comment
52+
})
53+
if !assert.NotNil(t, preparedComment) {
54+
return
55+
}
56+
run("diff with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
57+
ctx.Data["ShowOutdatedComments"] = true
58+
renderConversation(ctx, preparedComment, "diff")
59+
assert.Contains(t, resp.Body.String(), `<div class="content comment-container"`)
60+
})
61+
run("diff without outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
62+
ctx.Data["ShowOutdatedComments"] = false
63+
renderConversation(ctx, preparedComment, "diff")
64+
assert.Contains(t, resp.Body.String(), `conversation-not-existing`)
65+
})
66+
run("timeline with outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
67+
ctx.Data["ShowOutdatedComments"] = true
68+
renderConversation(ctx, preparedComment, "timeline")
69+
assert.Contains(t, resp.Body.String(), `<div id="code-comments-`)
70+
})
71+
run("timeline without outdated", func(t *testing.T, ctx *context.Context, resp *httptest.ResponseRecorder) {
72+
ctx.Data["ShowOutdatedComments"] = false
73+
renderConversation(ctx, preparedComment, "timeline")
74+
assert.Contains(t, resp.Body.String(), `conversation-not-existing`)
75+
})
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="ui segment conversation-holder conversation-not-existing">
2+
{{ctx.Locale.Tr "repo.issues.review.outdated_description"}}
3+
</div>

0 commit comments

Comments
 (0)