Skip to content

Commit 96849dd

Browse files
chore: handle feedback
1 parent 780d483 commit 96849dd

File tree

7 files changed

+56
-65
lines changed

7 files changed

+56
-65
lines changed

models/issues/comment_code.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
type CodeComments map[string]map[int64][]*Comment
1919

2020
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
21-
func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) {
22-
return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments)
21+
func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool, filePath *string) (CodeComments, error) {
22+
return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments, filePath)
2323
}
2424

25-
func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) (CodeComments, error) {
25+
func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool, filePath *string) (CodeComments, error) {
2626
pathToLineToComment := make(CodeComments)
2727
if review == nil {
2828
review = &Review{ID: 0}
@@ -33,6 +33,15 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u
3333
ReviewID: review.ID,
3434
}
3535

36+
if filePath != nil {
37+
opts = FindCommentsOptions{
38+
Type: CommentTypeCode,
39+
IssueID: issue.ID,
40+
ReviewID: review.ID,
41+
TreePath: *filePath,
42+
}
43+
}
44+
3645
comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments)
3746
if err != nil {
3847
return nil, err

models/issues/comment_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func TestFetchCodeComments(t *testing.T) {
5050

5151
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
5252
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
53-
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false)
53+
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false, nil)
5454
assert.NoError(t, err)
5555
assert.Contains(t, res, "README.md")
5656
assert.Contains(t, res["README.md"], int64(4))
5757
assert.Len(t, res["README.md"][4], 1)
5858
assert.Equal(t, int64(4), res["README.md"][4][0].ID)
5959

6060
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
61-
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false)
61+
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false, nil)
6262
assert.NoError(t, err)
6363
assert.Len(t, res, 1)
6464
}

models/issues/review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) {
158158
if err = r.LoadIssue(ctx); err != nil {
159159
return err
160160
}
161-
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false)
161+
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false, nil)
162162
return err
163163
}
164164

routers/web/repo/compare.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -924,16 +924,14 @@ func ExcerptBlob(ctx *context.Context) {
924924
Type: gitdiff.DiffLineSection,
925925
Content: lineText,
926926
SectionInfo: &gitdiff.DiffLineSectionInfo{
927-
Path: filePath,
928-
LastLeftIdx: lastLeft,
929-
LastRightIdx: lastRight,
930-
LeftIdx: idxLeft,
931-
RightIdx: idxRight,
932-
LeftHunkSize: leftHunkSize,
933-
RightHunkSize: rightHunkSize,
934-
HasComments: false,
935-
LastRightCommentIdx: 0,
936-
RightCommentIdx: 0,
927+
Path: filePath,
928+
LastLeftIdx: lastLeft,
929+
LastRightIdx: lastRight,
930+
LeftIdx: idxLeft,
931+
RightIdx: idxRight,
932+
LeftHunkSize: leftHunkSize,
933+
RightHunkSize: rightHunkSize,
934+
HasComments: false,
937935
},
938936
Comments: nil,
939937
}
@@ -946,11 +944,11 @@ func ExcerptBlob(ctx *context.Context) {
946944
issueIndex := ctx.FormInt64("issue_index")
947945
if ctx.FormBool("pull") && issueIndex > 0 {
948946
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex)
949-
if issue == nil {
947+
if err != nil {
950948
ctx.ServerError("GetIssueByIndex", err)
951949
return
952950
}
953-
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, false)
951+
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, false, &filePath)
954952
if err != nil {
955953
ctx.ServerError("FetchCodeComments", err)
956954
return

routers/web/web.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,10 +1520,6 @@ func registerRoutes(m *web.Router) {
15201520
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
15211521
}, func(ctx *context.Context) gocontext.CancelFunc {
15221522
// FIXME: refactor this function, use separate routes for wiki/code
1523-
if ctx.FormBool("pull") {
1524-
ctx.Data["PageIsPullFiles"] = true
1525-
}
1526-
15271523
if ctx.FormBool("wiki") {
15281524
ctx.Data["PageIsWiki"] = true
15291525
repo.MustEnableWiki(ctx)

services/gitdiff/gitdiff.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ type DiffLine struct {
8686

8787
// DiffLineSectionInfo represents diff line section meta data
8888
type DiffLineSectionInfo struct {
89-
Path string
90-
LastLeftIdx int
91-
LastRightIdx int
92-
LeftIdx int
93-
RightIdx int
94-
LeftHunkSize int
95-
RightHunkSize int
96-
HasComments bool
97-
LastRightCommentIdx int
98-
RightCommentIdx int
89+
Path string
90+
LastLeftIdx int
91+
LastRightIdx int
92+
LeftIdx int
93+
RightIdx int
94+
LeftHunkSize int
95+
RightHunkSize int
96+
HasComments bool
9997
}
10098

10199
// BlobExcerptChunkSize represent max lines of excerpt
@@ -146,12 +144,10 @@ func (d *DiffLine) GetBlobExcerptQuery() string {
146144
"last_left=%d&last_right=%d&"+
147145
"left=%d&right=%d&"+
148146
"left_hunk_size=%d&right_hunk_size=%d&"+
149-
"last_rightt_comment_idx=%d&right_comment_idx=%d&"+
150147
"path=%s",
151148
d.SectionInfo.LastLeftIdx, d.SectionInfo.LastRightIdx,
152149
d.SectionInfo.LeftIdx, d.SectionInfo.RightIdx,
153150
d.SectionInfo.LeftHunkSize, d.SectionInfo.RightHunkSize,
154-
d.SectionInfo.LastRightCommentIdx, d.SectionInfo.RightCommentIdx,
155151
url.QueryEscape(d.SectionInfo.Path))
156152
return query
157153
}
@@ -175,16 +171,14 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int
175171
leftLine, leftHunk, rightLine, righHunk := git.ParseDiffHunkString(line)
176172

177173
return &DiffLineSectionInfo{
178-
Path: treePath,
179-
LastLeftIdx: lastLeftIdx,
180-
LastRightIdx: lastRightIdx,
181-
LeftIdx: leftLine,
182-
RightIdx: rightLine,
183-
LeftHunkSize: leftHunk,
184-
RightHunkSize: righHunk,
185-
HasComments: false,
186-
LastRightCommentIdx: 0,
187-
RightCommentIdx: 0,
174+
Path: treePath,
175+
LastLeftIdx: lastLeftIdx,
176+
LastRightIdx: lastRightIdx,
177+
LeftIdx: leftLine,
178+
RightIdx: rightLine,
179+
LeftHunkSize: leftHunk,
180+
RightHunkSize: righHunk,
181+
HasComments: false,
188182
}
189183
}
190184

@@ -404,14 +398,12 @@ func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommit, ri
404398
Type: DiffLineSection,
405399
Content: " ",
406400
SectionInfo: &DiffLineSectionInfo{
407-
Path: diffFile.Name,
408-
LastLeftIdx: lastLine.LeftIdx,
409-
LastRightIdx: lastLine.RightIdx,
410-
LeftIdx: leftLineCount,
411-
RightIdx: rightLineCount,
412-
HasComments: false,
413-
LastRightCommentIdx: 0,
414-
RightCommentIdx: 0,
401+
Path: diffFile.Name,
402+
LastLeftIdx: lastLine.LeftIdx,
403+
LastRightIdx: lastLine.RightIdx,
404+
LeftIdx: leftLineCount,
405+
RightIdx: rightLineCount,
406+
HasComments: false,
415407
},
416408
}
417409
tailSection := &DiffSection{FileName: diffFile.Name, Lines: []*DiffLine{tailDiffLine}}
@@ -469,11 +461,9 @@ type Diff struct {
469461
NumViewedFiles int // user-specific
470462
}
471463

472-
// function (section *DiffSection) GetType() int {
473-
474464
// LoadComments loads comments into each line
475465
func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error {
476-
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments)
466+
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments, nil)
477467
if err != nil {
478468
return err
479469
}

services/repository/files/diff_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,14 @@ func TestGetDiffPreview(t *testing.T) {
5959
Content: "@@ -1,3 +1,4 @@",
6060
Comments: nil,
6161
SectionInfo: &gitdiff.DiffLineSectionInfo{
62-
Path: "README.md",
63-
LastLeftIdx: 0,
64-
LastRightIdx: 0,
65-
LeftIdx: 1,
66-
RightIdx: 1,
67-
LeftHunkSize: 3,
68-
RightHunkSize: 4,
69-
HasComments: false,
70-
LastRightCommentIdx: 0,
71-
RightCommentIdx: 0,
62+
Path: "README.md",
63+
LastLeftIdx: 0,
64+
LastRightIdx: 0,
65+
LeftIdx: 1,
66+
RightIdx: 1,
67+
LeftHunkSize: 3,
68+
RightHunkSize: 4,
69+
HasComments: false,
7270
},
7371
},
7472
{

0 commit comments

Comments
 (0)