Skip to content

Commit eadbbbe

Browse files
committed
extract duplicate code
FetchCodeCommentsByLine was initially more or less copied from fetchCodeCommentsByReview. Now they both use a common findCodeComments function instead
1 parent 1f96c0e commit eadbbbe

File tree

1 file changed

+35
-70
lines changed

1 file changed

+35
-70
lines changed

models/issue_comment.go

+35-70
Original file line numberDiff line numberDiff line change
@@ -1079,67 +1079,18 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
10791079
if review == nil {
10801080
review = &Review{ID: 0}
10811081
}
1082-
//Find comments
10831082
opts := FindCommentsOptions{
10841083
Type: CommentTypeCode,
10851084
IssueID: issue.ID,
10861085
ReviewID: review.ID,
10871086
}
1088-
conds := opts.toConds()
1089-
if review.ID == 0 {
1090-
conds = conds.And(builder.Eq{"invalidated": false})
1091-
}
1092-
1093-
var comments []*Comment
1094-
if err := e.Where(conds).
1095-
Asc("comment.created_unix").
1096-
Asc("comment.id").
1097-
Find(&comments); err != nil {
1098-
return nil, err
1099-
}
11001087

1101-
if err := issue.loadRepo(e); err != nil {
1102-
return nil, err
1103-
}
1104-
1105-
if err := CommentList(comments).loadPosters(e); err != nil {
1106-
return nil, err
1107-
}
1108-
1109-
// Find all reviews by ReviewID
1110-
reviews := make(map[int64]*Review)
1111-
var ids = make([]int64, 0, len(comments))
1112-
for _, comment := range comments {
1113-
if comment.ReviewID != 0 {
1114-
ids = append(ids, comment.ReviewID)
1115-
}
1116-
}
1117-
if err := e.In("id", ids).Find(&reviews); err != nil {
1088+
comments, err := findCodeComments(e, opts, issue, currentUser, review)
1089+
if err != nil {
11181090
return nil, err
11191091
}
11201092

11211093
for _, comment := range comments {
1122-
if err := comment.LoadResolveDoer(); err != nil {
1123-
return nil, err
1124-
}
1125-
1126-
if err := comment.LoadReactions(issue.Repo); err != nil {
1127-
return nil, err
1128-
}
1129-
1130-
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1131-
// If the review is pending only the author can see the comments (except the review is set)
1132-
if review.ID == 0 {
1133-
if re.Type == ReviewTypePending &&
1134-
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1135-
continue
1136-
}
1137-
}
1138-
comment.Review = re
1139-
}
1140-
1141-
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
1142-
issue.Repo.ComposeMetas()))
11431094
if pathToLineToComment[comment.TreePath] == nil {
11441095
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
11451096
}
@@ -1148,16 +1099,16 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11481099
return pathToLineToComment, nil
11491100
}
11501101

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-
}
1102+
func findCodeComments(e Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
11591103
var comments []*Comment
1160-
if err := x.Where(opts.toConds()).
1104+
if review == nil {
1105+
review = &Review{ID: 0}
1106+
}
1107+
conds := opts.toConds()
1108+
if review.ID == 0 {
1109+
conds = conds.And(builder.Eq{"invalidated": false})
1110+
}
1111+
if err := x.Where(conds).
11611112
Asc("comment.created_unix").
11621113
Asc("comment.id").
11631114
Find(&comments); err != nil {
@@ -1184,7 +1135,19 @@ func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, l
11841135
return nil, err
11851136
}
11861137

1138+
n := 0
11871139
for _, comment := range comments {
1140+
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1141+
// If the review is pending only the author can see the comments (except if the review is set)
1142+
if review.ID == 0 && re.Type == ReviewTypePending &&
1143+
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1144+
continue
1145+
}
1146+
comment.Review = re
1147+
}
1148+
comments[n] = comment
1149+
n++
1150+
11881151
if err := comment.LoadResolveDoer(); err != nil {
11891152
return nil, err
11901153
}
@@ -1193,19 +1156,21 @@ func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, l
11931156
return nil, err
11941157
}
11951158

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-
12051159
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
12061160
issue.Repo.ComposeMetas()))
12071161
}
1208-
return comments, nil
1162+
return comments[:n], nil
1163+
}
1164+
1165+
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
1166+
func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, line int64) ([]*Comment, error) {
1167+
opts := FindCommentsOptions{
1168+
Type: CommentTypeCode,
1169+
IssueID: issue.ID,
1170+
TreePath: treePath,
1171+
Line: line,
1172+
}
1173+
return findCodeComments(x, opts, issue, currentUser, nil)
12091174
}
12101175

12111176
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line

0 commit comments

Comments
 (0)