Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: phase 1 changes: add comments feat to unchanged lines in the pullrequest #32471

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
274ed54
feat: phase 1 changes: add comments feat to unchanged lines in the pu…
rajesh-jonnalagadda Nov 11, 2024
914bed8
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 11, 2024
abd1d2f
chore: fix lint issues
rajesh-jonnalagadda Nov 11, 2024
f03359e
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 11, 2024
a03abb5
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 11, 2024
ccd797a
chore: fixed the tmpl lint issues
rajesh-jonnalagadda Nov 11, 2024
efba6c4
chore: remove issue index and minro template changes
rajesh-jonnalagadda Nov 11, 2024
28c512d
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 12, 2024
e41d7f8
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 12, 2024
d9aa349
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 13, 2024
b387e41
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 14, 2024
f5a83a7
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 18, 2024
a8ace20
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 22, 2024
7f0c5e4
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 23, 2024
2db63b1
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 24, 2024
fa4c61e
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 26, 2024
40826dd
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 26, 2024
7d0c8e3
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 27, 2024
0f6708c
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 27, 2024
ea285c2
chore: fix the extra comment icon at the end and change the icon pointer
rajesh-jonnalagadda Nov 27, 2024
d36fece
Merge branch 'feat-32257-add-comments-unchanged-lines-and-show' of gi…
rajesh-jonnalagadda Nov 27, 2024
819dd08
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 28, 2024
5d9651b
chore: fix the comment context menu drop down
rajesh-jonnalagadda Nov 28, 2024
17deb2c
Merge branch 'feat-32257-add-comments-unchanged-lines-and-show' of gi…
rajesh-jonnalagadda Nov 28, 2024
65ef174
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 28, 2024
728fc12
chore: fix backedn-lint issues
rajesh-jonnalagadda Nov 28, 2024
24da8ef
Merge branch 'feat-32257-add-comments-unchanged-lines-and-show' of gi…
rajesh-jonnalagadda Nov 28, 2024
bf3535a
chore: fix lint issue
rajesh-jonnalagadda Nov 28, 2024
904fb52
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Nov 29, 2024
e6e314b
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 2, 2024
bcaf4aa
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 2, 2024
c1e4d57
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 4, 2024
5289da2
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 5, 2024
510d8ba
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 5, 2024
649201d
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 6, 2024
50bab1b
chore: remove the add comment on the line section. fix the hsaComment…
rajesh-jonnalagadda Dec 6, 2024
780d483
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 6, 2024
f8197d7
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 6, 2024
959e149
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 7, 2024
b5531f8
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 9, 2024
67343d6
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 9, 2024
96849dd
chore: handle feedback
rajesh-jonnalagadda Dec 10, 2024
2413513
Merge branch 'feat-32257-add-comments-unchanged-lines-and-show' of gi…
rajesh-jonnalagadda Dec 10, 2024
5dd1004
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 10, 2024
51a70e3
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 11, 2024
8d6b3c3
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-…
rajesh-jonnalagadda Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions models/issues/comment_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
type CodeComments map[string]map[int64][]*Comment

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

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

if filePath != nil {
opts = FindCommentsOptions{
Type: CommentTypeCode,
IssueID: issue.ID,
ReviewID: review.ID,
TreePath: *filePath,
}
}

comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions models/issues/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func TestFetchCodeComments(t *testing.T) {

issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false)
res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false, nil)
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 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false)
res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false, nil)
assert.NoError(t, err)
assert.Len(t, res, 1)
}
Expand Down
2 changes: 1 addition & 1 deletion models/issues/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) {
if err = r.LoadIssue(ctx); err != nil {
return err
}
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false)
r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false, nil)
return err
}

Expand Down
69 changes: 69 additions & 0 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"net/url"
"path/filepath"
"sort"
"strings"

"code.gitea.io/gitea/models/db"
Expand All @@ -39,6 +40,7 @@ import (
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/context/upload"
"code.gitea.io/gitea/services/gitdiff"
user_service "code.gitea.io/gitea/services/user"
)

const (
Expand Down Expand Up @@ -863,6 +865,10 @@ func ExcerptBlob(ctx *context.Context) {
direction := ctx.FormString("direction")
filePath := ctx.FormString("path")
gitRepo := ctx.Repo.GitRepo
if ctx.FormBool("pull") {
ctx.Data["PageIsPullFiles"] = true
}

if ctx.FormBool("wiki") {
var err error
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
Expand All @@ -881,6 +887,7 @@ func ExcerptBlob(ctx *context.Context) {
section := &gitdiff.DiffSection{
FileName: filePath,
Name: filePath,
Lines: []*gitdiff.DiffLine{},
}
if direction == "up" && (idxLeft-lastLeft) > chunkSize {
idxLeft -= chunkSize
Expand Down Expand Up @@ -924,18 +931,79 @@ func ExcerptBlob(ctx *context.Context) {
RightIdx: idxRight,
LeftHunkSize: leftHunkSize,
RightHunkSize: rightHunkSize,
HasComments: false,
},
Comments: nil,
}
if direction == "up" {
section.Lines = append([]*gitdiff.DiffLine{lineSection}, section.Lines...)
} else if direction == "down" {
section.Lines = append(section.Lines, lineSection)
}
}
issueIndex := ctx.FormInt64("issue_index")
if ctx.FormBool("pull") && issueIndex > 0 {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex)
if err != nil {
ctx.ServerError("GetIssueByIndex", err)
return
}
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, false, &filePath)
if err != nil {
ctx.ServerError("FetchCodeComments", err)
return
}
lineCommits := allComments[filePath]
for index, line := range section.Lines {
if line.SectionInfo != nil && line.Type == 4 && !(line.SectionInfo.LastRightIdx == 0 && index+1 == len(section.Lines)) {
start := int64(line.SectionInfo.LastRightIdx + 1)
end := int64(line.SectionInfo.RightIdx - 1)
for start <= end {
if _, ok := lineCommits[start]; ok {
if !line.SectionInfo.HasComments {
line.SectionInfo.HasComments = true
break
}
}
start++
}
}
if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok {
line.Comments = append(line.Comments, comments...)
}
if comments, ok := lineCommits[int64(line.RightIdx)]; ok {
line.Comments = append(line.Comments, comments...)
}

sort.SliceStable(line.Comments, func(i, j int) bool {
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
})
}
for _, line := range section.Lines {
for _, comment := range line.Comments {
if err := comment.LoadAttachments(ctx); err != nil {
ctx.ServerError("LoadAttachments", err)
return
}
}
}
ctx.Data["Issue"] = issue
ctx.Data["IssueIndex"] = issue.Index
}
ctx.Data["section"] = section
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
ctx.Data["AfterCommitID"] = commitID
ctx.Data["Anchor"] = anchor
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
}
if ctx.Data["SignedUserID"] == nil {
ctx.Data["SignedUserID"] = ctx.Doer.ID
}
ctx.Data["SignedUser"] = ctx.Doer
ctx.Data["IsSigned"] = ctx.Doer != nil
ctx.Data["Repository"] = ctx.Repo.Repository
ctx.Data["Permission"] = &ctx.Repo.Permission
ctx.HTML(http.StatusOK, tplBlobExcerpt)
}

Expand Down Expand Up @@ -964,6 +1032,7 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
RightIdx: line + 1,
Type: gitdiff.DiffLinePlain,
Content: " " + lineText,
Comments: []*issues_model.Comment{},
}
diffLines = append(diffLines, diffLine)
}
Expand Down
1 change: 1 addition & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func getPullInfo(ctx *context.Context) (issue *issues_model.Issue, ok bool) {
}
ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, emoji.ReplaceAliases(issue.Title))
ctx.Data["Issue"] = issue
ctx.Data["IssueIndex"] = issue.Index

if !issue.IsPull {
ctx.NotFound("ViewPullCommits", nil)
Expand Down
21 changes: 19 additions & 2 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type DiffLineSectionInfo struct {
RightIdx int
LeftHunkSize int
RightHunkSize int
HasComments bool
}

// BlobExcerptChunkSize represent max lines of excerpt
Expand Down Expand Up @@ -177,6 +178,7 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int
RightIdx: rightLine,
LeftHunkSize: leftHunk,
RightHunkSize: righHunk,
HasComments: false,
}
}

Expand Down Expand Up @@ -401,6 +403,7 @@ func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommit, ri
LastRightIdx: lastLine.RightIdx,
LeftIdx: leftLineCount,
RightIdx: rightLineCount,
HasComments: false,
},
}
tailSection := &DiffSection{FileName: diffFile.Name, Lines: []*DiffLine{tailDiffLine}}
Expand Down Expand Up @@ -460,14 +463,28 @@ type Diff struct {

// LoadComments loads comments into each line
func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error {
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments)
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments, nil)
if err != nil {
return err
}

for _, file := range diff.Files {
if lineCommits, ok := allComments[file.Name]; ok {
for _, section := range file.Sections {
for _, line := range section.Lines {
for index, line := range section.Lines {
if line.SectionInfo != nil && line.Type == 4 && !(line.SectionInfo.LastRightIdx == 0 && index+1 == len(section.Lines)) {
start := int64(line.SectionInfo.LastRightIdx + 1)
end := int64(line.SectionInfo.RightIdx - 1)
for start <= end {
if _, ok := lineCommits[start]; ok {
if !line.SectionInfo.HasComments {
line.SectionInfo.HasComments = true
break
}
}
start++
}
}
if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok {
line.Comments = append(line.Comments, comments...)
}
Expand Down
1 change: 1 addition & 0 deletions services/repository/files/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestGetDiffPreview(t *testing.T) {
RightIdx: 1,
LeftHunkSize: 3,
RightHunkSize: 4,
HasComments: false,
},
},
{
Expand Down
Loading
Loading