Display partial diff for commit comments

This commit is contained in:
Naoki Takezoe
2018-04-27 12:04:23 +09:00
parent efe891a348
commit dd1dbd429c
3 changed files with 47 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package gitbucket.core.view
import java.text.SimpleDateFormat
import java.util.{Date, Locale, TimeZone}
import com.nimbusds.jose.util.JSONObjectUtils
import gitbucket.core.controller.Context
import gitbucket.core.model.CommitState
import gitbucket.core.plugin.{PluginRegistry, RenderRequest}
@@ -462,4 +463,44 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
*/
def readableSize(size: Option[Long]): String = FileUtil.readableSize(size.getOrElse(0))
/**
* Make HTML fragment of the partial diff for a comment on a line of diff.
*
* @param jsonString JSON string which is stored in COMMIT_COMMENT table.
* @return HTML fragment of diff
*/
def diff(jsonString: String): Html = {
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats = DefaultFormats
val diff = parse(jsonString).extract[Seq[CommentDiffLine]]
val sb = new StringBuilder()
sb.append("<table class=\"diff inlinediff\">")
diff.foreach { line =>
sb.append("<tr>")
sb.append(s"""<th class="line-num oldline ${line.`type`}">""")
line.oldLine.foreach { oldLine =>
sb.append(oldLine)
}
sb.append("</th>")
sb.append(s"""<th class="line-num newline ${line.`type`}">""")
line.newLine.foreach { newLine =>
sb.append(newLine)
}
sb.append("</th>")
sb.append(s"""<td class="body ${line.`type`}">""")
sb.append(StringUtil.escapeHtml(line.text))
sb.append("</td>")
sb.append("</tr>")
}
sb.append("</table>")
Html(sb.toString())
}
case class CommentDiffLine(newLine: Option[String], oldLine: Option[String], `type`: String, text: String)
}

View File

@@ -10,6 +10,11 @@
<a href="@helpers.url(repository)/commit/@comments.comments.head.commitId" class="monospace">@comments.comments.head.commitId.substring(0, 7)</a>
</span>
</div>
@comments.comments.headOption.map { comment =>
@comment.diff.map { diff =>
@helpers.diff(diff)
}
}
<div class="panel-body">
@comments.comments.map { comment =>
@gitbucket.core.helper.html.commitcomment(comment, hasWritePermission, repository, latestCommitId)

View File

@@ -15,6 +15,7 @@
flash: Map[String, String])(implicit context: gitbucket.core.controller.Context)
@import gitbucket.core.view.helpers
@gitbucket.core.pulls.html.menu("conversation", issue, pullreq, commits, comments, isManageable, repository, flash){
<link href="@helpers.assets("/vendors/jsdifflib/diffview.css")" type="text/css" rel="stylesheet" />
<div class="col-md-9">
<div id="comment-list">
@gitbucket.core.issues.html.commentlist(Some(issue), comments.toList, isManageable, repository, Some(pullreq))