From dc78dc9b0dbc27418c42d2347681c22430c3ebfe Mon Sep 17 00:00:00 2001 From: odz Date: Sat, 30 Nov 2013 18:57:19 +0900 Subject: [PATCH 1/2] Close issues via commit messages --- src/main/scala/app/PullRequestsController.scala | 13 +++++++++++++ src/main/scala/service/IssuesService.scala | 12 ++++++++++++ src/main/scala/servlet/GitRepositoryServlet.scala | 8 ++++++++ 3 files changed, 33 insertions(+) diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index 365fb9270..e91ff786e 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -163,6 +163,19 @@ trait PullRequestsControllerBase extends ControllerBase { } } + // close issue by content of pull request + val defaultBranch = getRepository(owner, name, baseUrl).get.repository.defaultBranch + if(pullreq.branch == defaultBranch){ + commits.flatten.foreach { commit => + closeIssuesFromMessage(commit.fullMessage, loginAccount.userName, owner, name) + } + issue.content match { + case Some(content) => closeIssuesFromMessage(content, loginAccount.userName, owner, name) + case _ => + } + closeIssuesFromMessage(form.message, loginAccount.userName, owner, name) + } + // notifications Notifier().toNotify(repository, issueId, "merge"){ Notifier.msgStatus(s"${baseUrl}/${owner}/${name}/pull/${issueId}") diff --git a/src/main/scala/service/IssuesService.scala b/src/main/scala/service/IssuesService.scala index 493692e4d..ec6f93619 100644 --- a/src/main/scala/service/IssuesService.scala +++ b/src/main/scala/service/IssuesService.scala @@ -314,6 +314,18 @@ trait IssuesService { }.toList } + def closeIssuesFromMessage(message: String, userName: String, owner: String, repository: String) = { + val regex = "(?i)(? + getIssue(owner, repository, issueId) match { + case Some(issue) if !issue.closed => { + createComment(owner, repository, userName, issue.issueId, "Close", "close") + updateClosed(owner, repository, issue.issueId, true) + } + case _ => + } + } + } } object IssuesService { diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index a00a92811..4c02070de 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -140,6 +140,14 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL } } + // close issues + val defaultBranch = getRepository(owner, repository, baseURL).get.repository.defaultBranch + if(refName(1) == "heads" && branchName == defaultBranch && command.getType == ReceiveCommand.Type.UPDATE){ + git.log.addRange(command.getOldId, command.getNewId).call.asScala.foreach { commit => + closeIssuesFromMessage(commit.getFullMessage, userName, owner, repository) + } + } + // call web hook val webHookURLs = getWebHookURLs(owner, repository) if(webHookURLs.nonEmpty){ From b031103df8d7109f727a1dfc903f4c6caf93e011 Mon Sep 17 00:00:00 2001 From: takezoe Date: Sun, 2 Mar 2014 04:49:14 +0900 Subject: [PATCH 2/2] (refs #218)Separate StringUtil#extractCloseId() to add unit test. --- .../scala/app/PullRequestsController.scala | 2 +- src/main/scala/service/IssuesService.scala | 13 +++++-------- .../scala/servlet/GitRepositoryServlet.scala | 2 +- src/main/scala/util/StringUtil.scala | 15 ++++++++++++--- src/test/scala/util/StringUtilSpec.scala | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index 5a3317dc2..bf3bea268 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -79,7 +79,7 @@ trait PullRequestsControllerBase extends ControllerBase { pulls.html.pullreq( issue, pullreq, getComments(owner, name, issueId), - getIssueLabels(owner, name, issueId.toInt), + getIssueLabels(owner, name, issueId), (getCollaborators(owner, name) ::: (if(getAccountByUserName(owner).get.isGroupAccount) Nil else List(owner))).sorted, getMilestonesWithIssueCount(owner, name), getLabels(owner, name), diff --git a/src/main/scala/service/IssuesService.scala b/src/main/scala/service/IssuesService.scala index 81a9bf85e..b1215995f 100644 --- a/src/main/scala/service/IssuesService.scala +++ b/src/main/scala/service/IssuesService.scala @@ -8,6 +8,7 @@ import Q.interpolation import model._ import util.Implicits._ import util.StringUtil._ +import util.StringUtil trait IssuesService { import IssuesService._ @@ -315,14 +316,10 @@ trait IssuesService { } def closeIssuesFromMessage(message: String, userName: String, owner: String, repository: String) = { - val regex = "(?i)(? - getIssue(owner, repository, issueId) match { - case Some(issue) if !issue.closed => { - createComment(owner, repository, userName, issue.issueId, "Close", "close") - updateClosed(owner, repository, issue.issueId, true) - } - case _ => + StringUtil.extractCloseId(message).foreach { issueId => + for(issue <- getIssue(owner, repository, issueId) if !issue.closed){ + createComment(owner, repository, userName, issue.issueId, "Close", "close") + updateClosed(owner, repository, issue.issueId, true) } } } diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index e1a00d783..9efefd35f 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -142,7 +142,7 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: } // close issues - val defaultBranch = getRepository(owner, repository, baseURL).get.repository.defaultBranch + val defaultBranch = getRepository(owner, repository, baseUrl).get.repository.defaultBranch if(refName(1) == "heads" && branchName == defaultBranch && command.getType == ReceiveCommand.Type.UPDATE){ git.log.addRange(command.getOldId, command.getNewId).call.asScala.foreach { commit => closeIssuesFromMessage(commit.getFullMessage, pusher, owner, repository) diff --git a/src/main/scala/util/StringUtil.scala b/src/main/scala/util/StringUtil.scala index 7de2a62d6..54da0294d 100644 --- a/src/main/scala/util/StringUtil.scala +++ b/src/main/scala/util/StringUtil.scala @@ -31,7 +31,7 @@ object StringUtil { /** * Make string from byte array. Character encoding is detected automatically by [[util.StringUtil.detectEncoding]]. - * And if given bytes contains UTF-8 BOM, it's removed from returned string.. + * And if given bytes contains UTF-8 BOM, it's removed from returned string. */ def convertFromByteArray(content: Array[Byte]): String = IOUtils.toString(new BOMInputStream(new java.io.ByteArrayInputStream(content)), detectEncoding(content)) @@ -47,12 +47,21 @@ object StringUtil { } /** - * Extract issue id like ````#issueId``` from the given message. + * 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) } + "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(message).matchData.map(_.group(2)) + + /** + * Extract close issue id like ```close #issueId ``` from the given message. + * + * @param message the message which may contains close command + * @return the iterator of issue id + */ + def extractCloseId(message: String): Iterator[String] = + "(?i)(?