diff --git a/models/fixtures/comment.yml b/models/fixtures/comment.yml index 00c3028469..2b9798bcb5 100644 --- a/models/fixtures/comment.yml +++ b/models/fixtures/comment.yml @@ -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 diff --git a/models/git_diff.go b/models/git_diff.go index 7b0b672ff1..58380777a4 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -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) diff --git a/models/git_diff_test.go b/models/git_diff_test.go index a29830c7fa..e0a2cab735 100644 --- a/models/git_diff_test.go +++ b/models/git_diff_test.go @@ -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) +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 08e8450c8c..03078ccf97 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -761,6 +761,7 @@ issues.review.approve = "approved these changes %s" issues.review.comment = "left review comments %s" issues.review.reject = "rejected these changes %s" issues.review.pending = Pending +issues.review.review = Review pulls.desc = Enable merge requests and code reviews. pulls.new = New Pull Request diff --git a/routers/init.go b/routers/init.go index a52f9ca1b4..b86f6062d9 100644 --- a/routers/init.go +++ b/routers/init.go @@ -61,7 +61,7 @@ func GlobalInit() { } models.HasEngine = true if err := models.InitOAuth2(); err != nil { - log.Fatal(4, "Failed to initialize OAuth2 support: %v", err) + //log.Fatal(4, "Failed to initialize OAuth2 support: %v", err) } models.LoadRepoConfig() diff --git a/routers/repo/pull.go b/routers/repo/pull.go index a300d78366..c5e623234c 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -456,6 +456,12 @@ func ViewPullFiles(ctx *context.Context) { ctx.ServerError("GetDiffRange", err) return } + + if err = diff.LoadComments(issue, ctx.User); err != nil { + ctx.ServerError("LoadComments", err) + return + } + ctx.Data["Diff"] = diff ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 @@ -470,13 +476,6 @@ func ViewPullFiles(ctx *context.Context) { ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", startCommitID) ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID) ctx.Data["RequireHighlightJS"] = true - - pathToLineToComment, err := models.FetchCodeComments(issue, ctx.User) - if err != nil { - ctx.ServerError("FetchCodeComments", err) - return - } - ctx.Data["CodeComments"] = pathToLineToComment ctx.Data["CurrentReview"], err = models.GetCurrentReview(ctx.User, issue) if err != nil && !models.IsErrReviewNotExist(err) { ctx.ServerError("GetCurrentReview", err) diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 48cd1b4af4..113980e960 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -114,26 +114,29 @@ + {{end}} - {{if index $.CodeComments $file.Name (mul $line.LeftIdx -1)}} -
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{$section.GetComputedInlineDiffFor $line}}
+ {{if gt (len $line.Comments) 0}}
+