Fetch all review ids at once

Add unit tests

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz 2018-05-11 20:28:44 +02:00
parent 9c6bb4b1e9
commit bc9359222a
No known key found for this signature in database
GPG Key ID: 506AEEBE80BEDECD
4 changed files with 38 additions and 7 deletions

View File

@ -26,7 +26,7 @@
poster_id: 1 poster_id: 1
issue_id: 2 issue_id: 2
content: "meh..." content: "meh..."
review_id: 1 review_id: 4
line: 4 line: 4
tree_path: "README.md" tree_path: "README.md"
created_unix: 946684812 created_unix: 946684812

View File

@ -802,17 +802,30 @@ func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]ma
if err = issue.loadRepo(e); err != nil { if err = issue.loadRepo(e); err != nil {
return nil, err return nil, err
} }
// Find all reviews by ReviewID
reviews := make(map[int64]*Review)
var ids = make([]int64, 0, len(comments))
for _, comment := range comments { for _, comment := range comments {
if err = comment.loadReview(e); err != nil && !IsErrReviewNotExist(err) { if comment.ReviewID != 0 {
return nil, err ids = append(ids, comment.ReviewID)
} }
if comment.Review != nil && comment.Review.Type == ReviewTypePending { }
if currentUser == nil || currentUser.ID != comment.Review.ReviewerID { if err = e.In("id", ids).Find(&reviews); err != nil {
continue return nil, err
}
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
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(), comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
issue.Repo.ComposeMetas())) issue.Repo.ComposeMetas()))
if pathToLineToComment[comment.TreePath] == nil { if pathToLineToComment[comment.TreePath] == nil {
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment) pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
} }

View File

@ -39,3 +39,21 @@ func TestCreateComment(t *testing.T) {
updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
} }
func TestFetchCodeComments(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
res, err := FetchCodeComments(issue, user)
assert.NoError(t, err)
assert.Contains(t, res, "README.md")
assert.Contains(t, res["README.md"], int64(4))
assert.Len(t, res["README.md"][4], 1)
assert.Equal(t, int64(4), res["README.md"][4][0].ID)
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
res, err = FetchCodeComments(issue, user2)
assert.NoError(t, err)
assert.Empty(t, res)
}

View File

@ -36,7 +36,7 @@ func TestReview_LoadAttributes(t *testing.T) {
func TestReview_LoadCodeComments(t *testing.T) { func TestReview_LoadCodeComments(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) review := AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review)
assert.NoError(t, review.LoadCodeComments()) assert.NoError(t, review.LoadCodeComments())
assert.Len(t, review.CodeComments, 1) assert.Len(t, review.CodeComments, 1)
assert.Equal(t, int64(4), review.CodeComments[0].Line) assert.Equal(t, int64(4), review.CodeComments[0].Line)