1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Improved comment rendering in "Files" view by adding Comments to DiffLine

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz
2018-05-12 19:09:42 +02:00
parent 58fb672d0d
commit 066086c390
9 changed files with 112 additions and 26 deletions

View File

@@ -30,3 +30,12 @@
line: 4
tree_path: "README.md"
created_unix: 946684812
-
id: 5
type: 19 # code comment
poster_id: 1
issue_id: 2
content: "meh..."
line: -4
tree_path: "README.md"
created_unix: 946684812

View File

@@ -14,6 +14,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"sort"
"strconv"
"strings"
@@ -57,6 +58,7 @@ type DiffLine struct {
RightIdx int
Type DiffLineType
Content string
Comments []*Comment
}
// GetType returns the type of a DiffLine.
@@ -225,6 +227,32 @@ type Diff struct {
IsIncomplete bool
}
// LoadComments loads comments into each line
func (diff *Diff) LoadComments(issue *Issue, currentUser *User) error {
allComments, err := FetchCodeComments(issue, currentUser)
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 {
if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok {
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
})
}
}
}
}
return nil
}
// NumFiles returns number of files changes in a diff.
func (diff *Diff) NumFiles() int {
return len(diff.Files)

View File

@@ -5,6 +5,7 @@ import (
"testing"
dmp "github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
)
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
@@ -34,3 +35,28 @@ func TestDiffToHTML(t *testing.T) {
{Type: dmp.DiffEqual, Text: " biz"},
}, DiffLineDel))
}
func TestDiff_LoadComments(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
diff := &Diff{
Files: []*DiffFile{
{
Name: "README.md",
Sections: []*DiffSection{
{
Lines: []*DiffLine{
{
LeftIdx: 4,
RightIdx: 4,
},
},
},
},
},
},
}
assert.NoError(t, diff.LoadComments(issue, user))
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
}