diff --git a/src/main/scala/app/IssuesController.scala b/src/main/scala/app/IssuesController.scala index 22544e7..be564ee 100644 --- a/src/main/scala/app/IssuesController.scala +++ b/src/main/scala/app/IssuesController.scala @@ -4,10 +4,11 @@ import service._ import IssuesService._ -import util.{CollaboratorsAuthenticator, ReferrerAuthenticator, ReadableUsersAuthenticator, Notifier, Keys} +import util._ import util.Implicits._ import util.ControlUtil._ import org.scalatra.Ok +import model.Issue class IssuesController extends IssuesControllerBase with IssuesService with RepositoryService with AccountService with LabelsService with MilestonesService with ActivityService @@ -110,6 +111,11 @@ // record activity recordCreateIssueActivity(owner, name, userName, issueId, form.title) + // extract references and create refer comment + getIssue(owner, name, issueId.toString).foreach { issue => + createReferComment(owner, name, issue, form.title + " " + form.content.getOrElse("")) + } + // notifications Notifier().toNotify(repository, issueId, form.content.getOrElse("")){ Notifier.msgIssue(s"${baseUrl}/${owner}/${name}/issues/${issueId}") @@ -123,7 +129,11 @@ defining(repository.owner, repository.name){ case (owner, name) => getIssue(owner, name, params("id")).map { issue => if(isEditable(owner, name, issue.openedUserName)){ + // update issue updateIssue(owner, name, issue.issueId, form.title, form.content) + // extract references and create refer comment + createReferComment(owner, name, issue, form.title + " " + form.content.getOrElse("")) + redirect(s"/${owner}/${name}/issues/_data/${issue.issueId}") } else Unauthorized } getOrElse NotFound @@ -274,6 +284,15 @@ redirect(s"/${repository.owner}/${repository.name}/issues") } + private def createReferComment(owner: String, repository: String, fromIssue: Issue, message: String) = { + StringUtil.extractIssueId(message).foreach { issueId => + if(getIssue(owner, repository, issueId).isDefined){ + createComment(owner, repository, context.loginAccount.get.userName, issueId.toInt, + fromIssue.issueId + ":" + fromIssue.title, "refer") + } + } + } + /** * @see [[https://github.com/takezoe/gitbucket/wiki/CommentAction]] */ @@ -313,6 +332,11 @@ } recordActivity foreach ( _ (owner, name, userName, issueId, issue.title) ) + // extract references and create refer comment + content.map { content => + createReferComment(owner, name, issue, content) + } + // notifications Notifier() match { case f => diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index 712cd81..7eedd21 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -9,7 +9,7 @@ import javax.servlet.ServletConfig import javax.servlet.ServletContext import javax.servlet.http.HttpServletRequest -import util.{Keys, JGitUtil, Directory} +import util.{StringUtil, Keys, JGitUtil, Directory} import util.ControlUtil._ import util.Implicits._ import service._ @@ -167,8 +167,7 @@ } private def createIssueComment(commit: CommitInfo) = { - "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(commit.fullMessage).matchData.foreach { matchData => - val issueId = matchData.group(2) + StringUtil.extractIssueId(commit.fullMessage).foreach { issueId => if(getIssue(owner, repository, issueId).isDefined){ getAccountByMailAddress(commit.mailAddress).foreach { account => createComment(owner, repository, account.userName, issueId.toInt, commit.fullMessage + " " + commit.id, "commit") diff --git a/src/main/scala/util/StringUtil.scala b/src/main/scala/util/StringUtil.scala index 55c923a..7de2a62 100644 --- a/src/main/scala/util/StringUtil.scala +++ b/src/main/scala/util/StringUtil.scala @@ -45,4 +45,14 @@ case e => e } } + + /** + * Extract issue id like ````#issueId``` from the given message. + * + *@param message the message which may contains issue id + * @return the iterator of issue id + */ + def extractIssueId(message: String): Iterator[String] = + "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(message).matchData.map { matchData => matchData.group(2) } + } diff --git a/src/main/twirl/issues/commentlist.scala.html b/src/main/twirl/issues/commentlist.scala.html index 7bc168f..92a82f7 100644 --- a/src/main/twirl/issues/commentlist.scala.html +++ b/src/main/twirl/issues/commentlist.scala.html @@ -11,10 +11,16 @@
- @user(comment.commentedUserName, styleClass="username strong") commented + @user(comment.commentedUserName, styleClass="username strong") + @if(comment.action == "comment"){ + commented + } else { + @if(pullreq.isEmpty){ referenced the issue } else { referenced the pull request } + } @datetime(comment.registeredDate) - @if(comment.action != "commit" && comment.action != "merge" && (hasWritePermission || loginAccount.map(_.userName == comment.commentedUserName).getOrElse(false))){ + @if(comment.action != "commit" && comment.action != "merge" && comment.action != "refer" && + (hasWritePermission || loginAccount.map(_.userName == comment.commentedUserName).getOrElse(false))){   } @@ -27,7 +33,13 @@ @markdown(comment.content.substring(0, comment.content.length - 41), repository, false, true) } } else { - @markdown(comment.content, repository, false, true) + @if(comment.action == "refer"){ + @defining(comment.content.split(":")){ case Array(issueId, rest @ _*) => + Issue #@issueId: @rest.mkString(":") + } + } else { + @markdown(comment.content, repository, false, true) + } }