From dd1dbd429cc4597688829ed3e012850f6da19f74 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Fri, 27 Apr 2018 12:04:23 +0900 Subject: [PATCH] Display partial diff for commit comments --- .../scala/gitbucket/core/view/helpers.scala | 41 +++++++++++++++++++ .../core/helper/commitcomments.scala.html | 5 +++ .../core/pulls/conversation.scala.html | 1 + 3 files changed, 47 insertions(+) diff --git a/src/main/scala/gitbucket/core/view/helpers.scala b/src/main/scala/gitbucket/core/view/helpers.scala index c5f1a4012..848981a9e 100644 --- a/src/main/scala/gitbucket/core/view/helpers.scala +++ b/src/main/scala/gitbucket/core/view/helpers.scala @@ -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("") + diff.foreach { line => + sb.append("") + sb.append(s"""") + sb.append(s"""") + + sb.append(s"""") + sb.append("") + } + sb.append("
""") + line.oldLine.foreach { oldLine => + sb.append(oldLine) + } + sb.append("""") + line.newLine.foreach { newLine => + sb.append(newLine) + } + sb.append("""") + sb.append(StringUtil.escapeHtml(line.text)) + sb.append("
") + + Html(sb.toString()) + } + + case class CommentDiffLine(newLine: Option[String], oldLine: Option[String], `type`: String, text: String) + } diff --git a/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html b/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html index 41e9affb8..60f7b8137 100644 --- a/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html +++ b/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html @@ -10,6 +10,11 @@ @comments.comments.head.commitId.substring(0, 7) + @comments.comments.headOption.map { comment => + @comment.diff.map { diff => + @helpers.diff(diff) + } + }
@comments.comments.map { comment => @gitbucket.core.helper.html.commitcomment(comment, hasWritePermission, repository, latestCommitId) diff --git a/src/main/twirl/gitbucket/core/pulls/conversation.scala.html b/src/main/twirl/gitbucket/core/pulls/conversation.scala.html index 254a5f32e..a30834c0e 100644 --- a/src/main/twirl/gitbucket/core/pulls/conversation.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/conversation.scala.html @@ -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){ +
@gitbucket.core.issues.html.commentlist(Some(issue), comments.toList, isManageable, repository, Some(pullreq))