Skip to content

Commit 9cf249b

Browse files
committed
#8861 use ajax on PR review page
1 parent 94415f7 commit 9cf249b

File tree

12 files changed

+248
-77
lines changed

12 files changed

+248
-77
lines changed

models/issue_comment.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
980980
if opts.Type != CommentTypeUnknown {
981981
cond = cond.And(builder.Eq{"comment.type": opts.Type})
982982
}
983-
if opts.Line > 0 {
983+
if opts.Line != 0 {
984984
cond = cond.And(builder.Eq{"comment.line": opts.Line})
985985
}
986986
if len(opts.TreePath) > 0 {
@@ -1148,6 +1148,66 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11481148
return pathToLineToComment, nil
11491149
}
11501150

1151+
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
1152+
func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, line int64) ([]*Comment, error) {
1153+
opts := FindCommentsOptions{
1154+
Type: CommentTypeCode,
1155+
IssueID: issue.ID,
1156+
TreePath: treePath,
1157+
Line: line,
1158+
}
1159+
var comments []*Comment
1160+
if err := x.Where(opts.toConds()).
1161+
Asc("comment.created_unix").
1162+
Asc("comment.id").
1163+
Find(&comments); err != nil {
1164+
return nil, err
1165+
}
1166+
1167+
if err := issue.loadRepo(x); err != nil {
1168+
return nil, err
1169+
}
1170+
1171+
if err := CommentList(comments).loadPosters(x); err != nil {
1172+
return nil, err
1173+
}
1174+
1175+
// Find all reviews by ReviewID
1176+
reviews := make(map[int64]*Review)
1177+
var ids = make([]int64, 0, len(comments))
1178+
for _, comment := range comments {
1179+
if comment.ReviewID != 0 {
1180+
ids = append(ids, comment.ReviewID)
1181+
}
1182+
}
1183+
if err := x.In("id", ids).Find(&reviews); err != nil {
1184+
return nil, err
1185+
}
1186+
1187+
for _, comment := range comments {
1188+
if err := comment.LoadResolveDoer(); err != nil {
1189+
return nil, err
1190+
}
1191+
1192+
if err := comment.LoadReactions(issue.Repo); err != nil {
1193+
return nil, err
1194+
}
1195+
1196+
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1197+
// If the review is pending only the author can see the comments (except the review is set)
1198+
if re.Type == ReviewTypePending &&
1199+
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1200+
continue
1201+
}
1202+
comment.Review = re
1203+
}
1204+
1205+
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
1206+
issue.Repo.ComposeMetas()))
1207+
}
1208+
return comments, nil
1209+
}
1210+
11511211
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
11521212
func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) {
11531213
return fetchCodeComments(x, issue, currentUser)

modules/auth/repo_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ func (f *MergePullRequestForm) Validate(ctx *macaron.Context, errs binding.Error
547547

548548
// CodeCommentForm form for adding code comments for PRs
549549
type CodeCommentForm struct {
550+
Origin string `binding:"Required;In(timeline,diff)"`
550551
Content string `binding:"Required"`
551552
Side string `binding:"Required;In(previous,proposed)"`
552553
Line int64

routers/repo/pull_review.go

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,40 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/auth"
12+
"code.gitea.io/gitea/modules/base"
1213
"code.gitea.io/gitea/modules/context"
1314
"code.gitea.io/gitea/modules/log"
1415
pull_service "code.gitea.io/gitea/services/pull"
1516
)
1617

18+
const (
19+
tplConversation base.TplName = "repo/diff/conversation"
20+
tplNewComment base.TplName = "repo/diff/new_comment"
21+
)
22+
23+
// RenderNewCodeCommentForm will render the form for creating a new review comment
24+
func RenderNewCodeCommentForm(ctx *context.Context) {
25+
issue := GetActionIssue(ctx)
26+
if !issue.IsPull {
27+
return
28+
}
29+
currentReview, err := models.GetCurrentReview(ctx.User, issue)
30+
if err != nil && !models.IsErrReviewNotExist(err) {
31+
ctx.ServerError("GetCurrentReview", err)
32+
return
33+
}
34+
ctx.Data["PageIsPullFiles"] = true
35+
ctx.Data["Issue"] = issue
36+
ctx.Data["CurrentReview"] = currentReview
37+
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(issue.PullRequest.GetGitRefName())
38+
if err != nil {
39+
ctx.ServerError("GetRefCommitID", err)
40+
return
41+
}
42+
ctx.Data["AfterCommitID"] = pullHeadCommitID
43+
ctx.HTML(200, tplNewComment)
44+
}
45+
1746
// CreateCodeComment will create a code comment including an pending review if required
1847
func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
1948
issue := GetActionIssue(ctx)
@@ -58,11 +87,32 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
5887
}
5988

6089
log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID)
61-
ctx.Redirect(comment.HTMLURL())
90+
91+
if form.Origin == "diff" {
92+
comments, err := models.FetchCodeCommentsByLine(issue, ctx.User, comment.TreePath, comment.Line)
93+
if err != nil {
94+
ctx.ServerError("FetchCodeCommentsByLine", err)
95+
return
96+
}
97+
ctx.Data["PageIsPullFiles"] = true
98+
ctx.Data["comments"] = comments
99+
ctx.Data["CanMarkConversation"] = true
100+
ctx.Data["Issue"] = issue
101+
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(issue.PullRequest.GetGitRefName())
102+
if err != nil {
103+
ctx.ServerError("GetRefCommitID", err)
104+
return
105+
}
106+
ctx.Data["AfterCommitID"] = pullHeadCommitID
107+
ctx.HTML(200, tplConversation)
108+
} else {
109+
ctx.Redirect(comment.HTMLURL())
110+
}
62111
}
63112

64113
// UpdateResolveConversation add or remove an Conversation resolved mark
65114
func UpdateResolveConversation(ctx *context.Context) {
115+
origin := ctx.Query("origin")
66116
action := ctx.Query("action")
67117
commentID := ctx.QueryInt64("comment_id")
68118

@@ -103,9 +153,32 @@ func UpdateResolveConversation(ctx *context.Context) {
103153
return
104154
}
105155

106-
ctx.JSON(200, map[string]interface{}{
107-
"ok": true,
108-
})
156+
if origin == "diff" {
157+
comments, err := models.FetchCodeCommentsByLine(comment.Issue, ctx.User, comment.TreePath, comment.Line)
158+
if err != nil {
159+
ctx.ServerError("FetchCodeCommentsByLine", err)
160+
return
161+
}
162+
ctx.Data["PageIsPullFiles"] = true
163+
ctx.Data["comments"] = comments
164+
ctx.Data["CanMarkConversation"] = true
165+
ctx.Data["Issue"] = comment.Issue
166+
if err = comment.Issue.LoadPullRequest(); err != nil {
167+
ctx.ServerError("comment.Issue.LoadPullReqiest", err)
168+
return
169+
}
170+
pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(comment.Issue.PullRequest.GetGitRefName())
171+
if err != nil {
172+
ctx.ServerError("GetRefCommitID", err)
173+
return
174+
}
175+
ctx.Data["AfterCommitID"] = pullHeadCommitID
176+
ctx.HTML(200, tplConversation)
177+
} else {
178+
ctx.JSON(200, map[string]interface{}{
179+
"ok": true,
180+
})
181+
}
109182
}
110183

111184
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist

routers/routes/macaron.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
854854
m.Group("/files", func() {
855855
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
856856
m.Group("/reviews", func() {
857+
m.Post("/new_comment", repo.RenderNewCodeCommentForm)
857858
m.Post("/comments", bindIgnErr(auth.CodeCommentForm{}), repo.CreateCodeComment)
858859
m.Post("/submit", bindIgnErr(auth.SubmitReviewForm{}), repo.SubmitReview)
859860
}, context.RepoMustNotBeArchived())

templates/repo/diff/box.tmpl

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,25 @@
142142
{{end}}
143143

144144
{{if not $.Repository.IsArchived}}
145-
<div id="pull_review_add_comment" class="hide">
146-
{{template "repo/diff/new_comment" dict "root" .}}
145+
<div class="hide" id="edit-content-form">
146+
<div class="ui comment form">
147+
<div class="ui top attached tabular menu">
148+
<a class="active write item">{{$.i18n.Tr "write"}}</a>
149+
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
147150
</div>
148-
<div class="hide" id="edit-content-form">
149-
<div class="ui comment form">
150-
<div class="ui top attached tabular menu">
151-
<a class="active write item">{{$.i18n.Tr "write"}}</a>
152-
<a class="preview item" data-url="{{$.Repository.APIURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
153-
</div>
154-
<div class="ui bottom attached active write tab segment">
155-
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
156-
</div>
157-
<div class="ui bottom attached tab preview segment markdown">
158-
{{$.i18n.Tr "loading"}}
159-
</div>
160-
<div class="text right edit buttons">
161-
<div class="ui basic blue cancel button" tabindex="3">{{.i18n.Tr "repo.issues.cancel"}}</div>
162-
<div class="ui green save button" tabindex="2">{{.i18n.Tr "repo.issues.save"}}</div>
163-
</div>
164-
</div>
151+
<div class="ui bottom attached active write tab segment">
152+
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
153+
</div>
154+
<div class="ui bottom attached tab preview segment markdown">
155+
{{$.i18n.Tr "loading"}}
156+
</div>
157+
<div class="text right edit buttons">
158+
<div class="ui basic blue cancel button" tabindex="3">{{.i18n.Tr "repo.issues.cancel"}}</div>
159+
<div class="ui green save button" tabindex="2">{{.i18n.Tr "repo.issues.save"}}</div>
165160
</div>
166-
{{end}}
161+
</div>
162+
</div>
163+
{{end}}
167164

168165
{{if .IsSplitStyle}}
169166
<script>

templates/repo/diff/comment_form.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{end}}
77
<form class="ui form {{if $.hidden}}hide comment-form comment-form-reply{{end}}" action="{{$.root.Issue.HTMLURL}}/files/reviews/comments" method="post">
88
{{$.root.CsrfTokenHtml}}
9+
<input type="hidden" name="origin" value="{{if $.root.PageIsPullFiles}}diff{{else}}timeline{{end}}">
910
<input type="hidden" name="latest_commit_id" value="{{$.root.AfterCommitID}}"/>
1011
<input type="hidden" name="side" value="{{if $.Side}}{{$.Side}}{{end}}">
1112
<input type="hidden" name="line" value="{{if $.Line}}{{$.Line}}{{end}}">
@@ -29,7 +30,7 @@
2930
<span class="markdown-info">{{svg "octicon-markdown"}} {{$.root.i18n.Tr "repo.diff.comment.markdown_info"}}</span>
3031
<div class="ui right">
3132
{{if $.reply}}
32-
<button class="ui submit green tiny button btn-reply" onclick="window.submitReply(this);">{{$.root.i18n.Tr "repo.diff.comment.reply"}}</button>
33+
<button class="ui submit green tiny button btn-reply" type="submit">{{$.root.i18n.Tr "repo.diff.comment.reply"}}</button>
3334
<input type="hidden" name="reply" value="{{$.reply}}">
3435
{{else}}
3536
{{if $.root.CurrentReview}}

templates/repo/diff/conversation.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{$resolved := (index .comments 0).IsResolved}}
22
{{$resolveDoer := (index .comments 0).ResolveDoer}}
33
{{$isNotPending := (not (eq (index .comments 0).Review.Type 0))}}
4-
<div class="conversation-holder">
4+
<div class="conversation-holder" data-path="{{(index .comments 0).TreePath}}" data-side="{{if lt (index .comments 0).Line 0}}left{{else}}right{{end}}" data-idx="{{(index .comments 0).UnsignedLine}}">
55
{{if $resolved}}
66
<div class="ui attached header resolved-placeholder">
77
<span class="ui grey text left"><b>{{$resolveDoer.Name}}</b> {{$.i18n.Tr "repo.issues.review.resolved_by"}}</span>
@@ -23,7 +23,7 @@
2323
</div>
2424
{{template "repo/diff/comment_form_datahandler" dict "hidden" true "reply" (index .comments 0).ReviewID "root" $ "comment" (index .comments 0)}}
2525
{{if and $.CanMarkConversation $isNotPending}}
26-
<button class="ui icon tiny button resolve-conversation" data-action="{{if not $resolved}}Resolve{{else}}UnResolve{{end}}" data-comment-id="{{(index .comments 0).ID}}" data-update-url="{{$.RepoLink}}/issues/resolve_conversation" >
26+
<button class="ui icon tiny button resolve-conversation" data-origin="diff" data-action="{{if not $resolved}}Resolve{{else}}UnResolve{{end}}" data-comment-id="{{(index .comments 0).ID}}" data-update-url="{{$.RepoLink}}/issues/resolve_conversation" >
2727
{{if $resolved}}
2828
{{$.i18n.Tr "repo.issues.review.un_resolve_conversation"}}
2929
{{else}}

templates/repo/diff/new_comment.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
<div class="field comment-code-cloud">
2-
{{template "repo/diff/comment_form_datahandler" .}}
1+
<div class="conversation-holder">
2+
<div class="field comment-code-cloud">
3+
{{template "repo/diff/comment_form_datahandler" .}}
4+
</div>
35
</div>

templates/repo/diff/section_split.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{$file := .file}}
22
{{range $j, $section := $file.Sections}}
33
{{range $k, $line := $section.Lines}}
4-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
4+
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
55
{{if eq .GetType 4}}
66
<td class="lines-num lines-num-old">
77
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }}
@@ -24,14 +24,14 @@
2424
{{else}}
2525
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}"></span></td>
2626
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
27-
<td class="lines-code lines-code-old halfwidth">{{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui primary button add-code-comment add-code-comment-left" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
27+
<td class="lines-code lines-code-old halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui primary button add-code-comment add-code-comment-left{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
2828
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}"></span></td>
2929
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
30-
<td class="lines-code lines-code-new halfwidth">{{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui primary button add-code-comment add-code-comment-right" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
30+
<td class="lines-code lines-code-new halfwidth">{{if and $.root.SignedUserID $.root.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui primary button add-code-comment add-code-comment-right{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></td>
3131
{{end}}
3232
</tr>
3333
{{if gt (len $line.Comments) 0}}
34-
<tr class="add-code-comment">
34+
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
3535
<td class="lines-num"></td>
3636
<td class="lines-type-marker"></td>
3737
<td class="add-comment-left">

templates/repo/diff/section_unified.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{range $j, $section := $file.Sections}}
33
{{range $k, $line := $section.Lines}}
44
{{if or $.root.AfterCommitID (ne .GetType 4)}}
5-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
5+
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
66
{{if eq .GetType 4}}
77
<td colspan="2" class="lines-num">
88
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5) }}
@@ -29,11 +29,11 @@
2929
{{if eq .GetType 4}}
3030
<td class="chroma lines-code blob-hunk"><code class="code-inner">{{$section.GetComputedInlineDiffFor $line}}</code></td>
3131
{{else}}
32-
<td class="chroma lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">{{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles}}<a class="ui primary button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}" data-path="{{$file.Name}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{$section.GetComputedInlineDiffFor $line}}</code></td>
32+
<td class="chroma lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">{{if and $.root.SignedUserID $.root.PageIsPullFiles}}<a class="ui primary button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}{{if (not $line.CanComment)}} invisible{{end}}" data-path="{{$file.Name}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}" data-new-comment-url="{{$.root.Issue.HTMLURL}}/files/reviews/new_comment">{{svg "octicon-plus"}}</a>{{end}}<code class="code-inner">{{$section.GetComputedInlineDiffFor $line}}</code></td>
3333
{{end}}
3434
</tr>
3535
{{if gt (len $line.Comments) 0}}
36-
<tr>
36+
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
3737
<td colspan="2" class="lines-num"></td>
3838
<td class="add-comment-left add-comment-right" colspan="2">
3939
{{template "repo/diff/conversation" mergeinto $.root "comments" $line.Comments}}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@
538538
{{template "repo/diff/comment_form_datahandler" dict "hidden" true "reply" (index $comms 0).ReviewID "root" $ "comment" (index $comms 0)}}
539539

540540
{{if and $.CanMarkConversation $isNotPending}}
541-
<button class="ui tiny button resolve-conversation" data-action="{{if not $resolved}}Resolve{{else}}UnResolve{{end}}" data-comment-id="{{(index $comms 0).ID}}" data-update-url="{{$.RepoLink}}/issues/resolve_conversation" >
541+
<button class="ui tiny button resolve-conversation" data-origin="timeline" data-action="{{if not $resolved}}Resolve{{else}}UnResolve{{end}}" data-comment-id="{{(index $comms 0).ID}}" data-update-url="{{$.RepoLink}}/issues/resolve_conversation" >
542542
{{if $resolved}}
543543
{{$.i18n.Tr "repo.issues.review.un_resolve_conversation"}}
544544
{{else}}

0 commit comments

Comments
 (0)