summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorJimmy Praet <jimmy.praet@telenet.be>2021-01-08 22:49:55 +0100
committerGitHub <noreply@github.com>2021-01-08 23:49:55 +0200
commitbcb7f352215be12368de937fc0faa91cdb3d639a (patch)
treefa981e5c0110df8a094d7f3e3ba0ffdf2469c194 /models
parentce43d38b4ffa40255cc8f859c5b31f59351f827c (diff)
downloadgitea-bcb7f352215be12368de937fc0faa91cdb3d639a.tar.gz
gitea-bcb7f352215be12368de937fc0faa91cdb3d639a.zip
Do not reload page after adding comments in Pull Request reviews (#13877)
Fixed #8861 * use ajax on PR review page * handle review comments * extract duplicate code FetchCodeCommentsByLine was initially more or less copied from fetchCodeCommentsByReview. Now they both use a common findCodeComments function instead * use the Engine that was passed into the method Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models')
-rw-r--r--models/issue_comment.go65
1 files changed, 45 insertions, 20 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index a5cb91ac75..dd979edcda 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -979,7 +979,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
if opts.Type != CommentTypeUnknown {
cond = cond.And(builder.Eq{"comment.type": opts.Type})
}
- if opts.Line > 0 {
+ if opts.Line != 0 {
cond = cond.And(builder.Eq{"comment.line": opts.Line})
}
if len(opts.TreePath) > 0 {
@@ -1078,18 +1078,35 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
if review == nil {
review = &Review{ID: 0}
}
- //Find comments
opts := FindCommentsOptions{
Type: CommentTypeCode,
IssueID: issue.ID,
ReviewID: review.ID,
}
+
+ comments, err := findCodeComments(e, opts, issue, currentUser, review)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, comment := range comments {
+ if pathToLineToComment[comment.TreePath] == nil {
+ pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
+ }
+ pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
+ }
+ return pathToLineToComment, nil
+}
+
+func findCodeComments(e Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
+ var comments []*Comment
+ if review == nil {
+ review = &Review{ID: 0}
+ }
conds := opts.toConds()
if review.ID == 0 {
conds = conds.And(builder.Eq{"invalidated": false})
}
-
- var comments []*Comment
if err := e.Where(conds).
Asc("comment.created_unix").
Asc("comment.id").
@@ -1117,7 +1134,19 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
return nil, err
}
+ n := 0
for _, comment := range comments {
+ if re, ok := reviews[comment.ReviewID]; ok && re != nil {
+ // If the review is pending only the author can see the comments (except if the review is set)
+ if review.ID == 0 && re.Type == ReviewTypePending &&
+ (currentUser == nil || currentUser.ID != re.ReviewerID) {
+ continue
+ }
+ comment.Review = re
+ }
+ comments[n] = comment
+ n++
+
if err := comment.LoadResolveDoer(); err != nil {
return nil, err
}
@@ -1126,25 +1155,21 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
return nil, err
}
- if re, ok := reviews[comment.ReviewID]; ok && re != nil {
- // If the review is pending only the author can see the comments (except the review is set)
- if review.ID == 0 {
- if re.Type == ReviewTypePending &&
- (currentUser == nil || currentUser.ID != re.ReviewerID) {
- continue
- }
- }
- comment.Review = re
- }
-
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
issue.Repo.ComposeMetas()))
- if pathToLineToComment[comment.TreePath] == nil {
- pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
- }
- pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
}
- return pathToLineToComment, nil
+ return comments[:n], nil
+}
+
+// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
+func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, line int64) ([]*Comment, error) {
+ opts := FindCommentsOptions{
+ Type: CommentTypeCode,
+ IssueID: issue.ID,
+ TreePath: treePath,
+ Line: line,
+ }
+ return findCodeComments(x, opts, issue, currentUser, nil)
}
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line