Add support for showing code comments of reviews in conversation

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz 2018-05-15 12:52:28 +02:00
parent 6ae32b27e3
commit 4ea74e5327
No known key found for this signature in database
GPG Key ID: 506AEEBE80BEDECD
5 changed files with 196 additions and 10 deletions

View File

@ -839,12 +839,19 @@ func DeleteComment(comment *Comment) error {
}
func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) {
pathToLineToComment := make(map[string]map[int64][]*Comment)
return fetchCodeCommentsByReview(e, issue, currentUser, nil)
}
func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review *Review) (map[string]map[int64][]*Comment, error) {
pathToLineToComment := make(map[string]map[int64][]*Comment)
if review == nil {
review = &Review{ID: 0}
}
//Find comments
opts := FindCommentsOptions{
Type: CommentTypeCode,
IssueID: issue.ID,
Type: CommentTypeCode,
IssueID: issue.ID,
ReviewID: review.ID,
}
var comments []*Comment
if err := e.Where(opts.toConds().And(builder.Eq{"invalidated": false})).
@ -870,10 +877,12 @@ func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]ma
}
for _, comment := range comments {
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
// If the review is pending only the author can see the comments
if re.Type == ReviewTypePending &&
(currentUser == nil || currentUser.ID != re.ReviewerID) {
continue
// If the review is pending only the author can see the comments (except the review is set)
if review.ID == 0 {
if re.Type == ReviewTypePending &&
(currentUser == nil || currentUser.ID != re.ReviewerID) {
continue
}
}
comment.Review = re
}

View File

@ -56,11 +56,11 @@ type Review struct {
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
// CodeComments are the initial code comments of the review
CodeComments []*Comment `xorm:"-"`
CodeComments map[string]map[int64][]*Comment `xorm:"-"`
}
func (r *Review) loadCodeComments(e Engine) (err error) {
r.CodeComments, err = findComments(e, FindCommentsOptions{IssueID: r.IssueID, ReviewID: r.ID, Type: CommentTypeCode})
r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r)
return
}
@ -75,6 +75,9 @@ func (r *Review) loadIssue(e Engine) (err error) {
}
func (r *Review) loadReviewer(e Engine) (err error) {
if r.ReviewerID == 0 {
return nil
}
r.Reviewer, err = getUserByID(e, r.ReviewerID)
return
}

View File

@ -37,9 +37,10 @@ func TestReview_LoadCodeComments(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review)
assert.NoError(t, review.LoadAttributes())
assert.NoError(t, review.LoadCodeComments())
assert.Len(t, review.CodeComments, 1)
assert.Equal(t, int64(4), review.CodeComments[0].Line)
assert.Equal(t, int64(4), review.CodeComments["README.md"][int64(4)][0].Line)
}
func TestReviewType_Icon(t *testing.T) {

View File

@ -726,6 +726,17 @@ func ViewIssue(ctx *context.Context) {
ctx.ServerError("LoadReview", err)
return
}
if comment.Review == nil {
continue
}
if err = comment.Review.LoadAttributes(); err != nil {
ctx.ServerError("Review.LoadAttributes", err)
return
}
if err = comment.Review.LoadCodeComments(); err != nil {
ctx.ServerError("Review.LoadCodeComments", err)
return
}
}
}

View File

@ -256,6 +256,168 @@
<span class="text grey">{{.Content}}</span>
</div>
{{end}}
{{ range $filename, $lines := .Review.CodeComments}}
{{range $line, $comms := $lines}}
<div class="ui segments">
<div class="ui segment">
{{if (index $comms 0).Invalidated}}
<button class="ui icon button">
<i class="indend icon"></i>
</button>
{{end}}
<code>{{$filename}}</code>
</div>
<div class="ui table segment">
<div class="diff-file-box diff-box file-content tab-size-8">
<div class="file-body file-code code-view code-diff code-diff-unified">
<table>
<tbody>
<tr class="tag-code nl-0 ol-0">
<td colspan="2" class="lines-num">
</td>
<td class="lines-code lines-code-old">
<pre><code class="wrap language-json hljs">@@ <span class="hljs-number">-27</span>,<span class="hljs-number">5</span> +<span class="hljs-number">27</span>,<span class="hljs-number">5</span> @@</code></pre>
</td>
</tr>
<tr class="same-code nl-1 ol-1">
<td class="lines-num lines-num-old">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0L27">27</span>
</td>
<td class="lines-num lines-num-new">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0R27">27</span>
<a class="ui green button add-code-comment add-code-comment-right" data-path="vendor/vendor.json" data-side="right" data-idx="27">+</a>
</td>
<td class="lines-code ">
<pre><code class="wrap language-json hljs"> <span class="hljs-string">"revisionTime"</span>: <span class="hljs-string">"2016-08-23T15:25:51Z"</span></code></pre>
</td>
</tr>
<tr class="same-code nl-2 ol-2">
<td class="lines-num lines-num-old">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0L28">28</span>
</td>
<td class="lines-num lines-num-new">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0R28">28</span>
<a class="ui green button add-code-comment add-code-comment-right" data-path="vendor/vendor.json" data-side="right" data-idx="28">+</a>
</td>
<td class="lines-code ">
<pre><code class="wrap language-json hljs"> }</code></pre>
</td>
</tr>
<tr class="same-code nl-3 ol-3">
<td class="lines-num lines-num-old">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0L29">29</span>
</td>
<td class="lines-num lines-num-new">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0R29">29</span>
<a class="ui green button add-code-comment add-code-comment-right" data-path="vendor/vendor.json" data-side="right" data-idx="29">+</a>
</td>
<td class="lines-code ">
<pre><code class="wrap language-json hljs"> ],</code></pre>
</td>
</tr>
<tr class="del-code nl-4 ol-4">
<td class="lines-num lines-num-old">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0L30">30</span>
</td>
<td class="lines-num lines-num-new">
<span rel=""></span>
<a class="ui green button add-code-comment add-code-comment-left" data-path="vendor/vendor.json" data-side="left" data-idx="30">+</a>
</td>
<td class="lines-code lines-code-old">
<pre><code class="wrap language-json hljs">- <span class="hljs-string">"rootPath"</span>: <span class="hljs-string">"</span><span class="removed-code"><span class="hljs-string">github.com</span></span><span class="hljs-string">/JonasFranzDEV/drone-gitea-release"</span></code></pre>
</td>
</tr>
<tr class="add-code nl-5 ol-5">
<td class="lines-num lines-num-old">
<span rel=""></span>
</td>
<td class="lines-num lines-num-new">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0R30">30</span>
<a class="ui green button add-code-comment add-code-comment-right" data-path="vendor/vendor.json" data-side="right" data-idx="30">+</a>
</td>
<td class="lines-code ">
<pre><code class="wrap language-json hljs">+ <span class="hljs-string">"rootPath"</span>: <span class="hljs-string">"</span><span class="added-code"><span class="hljs-string">localhost:3000</span></span><span class="hljs-string">/JonasFranzDEV/drone-gitea-release"</span></code></pre>
</td>
</tr>
<tr class="same-code nl-6 ol-6">
<td class="lines-num lines-num-old">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0L31">31</span>
</td>
<td class="lines-num lines-num-new">
<span rel="diff-d9bff1704388955a5d24ad2abc8aea5a312994d0R31">31</span>
<a class="ui green button add-code-comment add-code-comment-right" data-path="vendor/vendor.json" data-side="right" data-idx="31">+</a>
</td>
<td class="lines-code ">
<pre><code class="wrap language-json hljs"> }</code></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="ui segment">
<div class="ui comments">
{{range $comms}}
{{ $createdSubStr:= TimeSinceUnix .CreatedUnix $.Lang }}
<div class="comment">
<a class="avatar">
<img src="{{.Poster.RelAvatarLink}}">
</a>
<div class="content">
<a class="author" {{if gt .Poster.ID 0}}href="{{.Poster.HomeLink}}"{{end}}>{{.Poster.Name}}</a>
<div class="metadata">
<span class="date">{{$.i18n.Tr "repo.issues.commented_at" .HashTag $createdSubStr | Safe}}</span>
</div>
<div class="text">
<div class="render-content markdown has-emoji">
{{if .RenderedContent}}
{{.RenderedContent|Str2html}}
{{else}}
<span class="no-content">{{$.i18n.Tr "repo.issues.no_content"}}</span>
{{end}}
</div>
<div class="raw-content hide">{{.Content}}</div>
</div>
</div>
</div>
{{end}}
</div>
</div>
</div>
{{end}}
{{end}}
</div>
{{end}}
{{end}}