diff --git a/src/main/scala/gitbucket/core/service/PullRequestService.scala b/src/main/scala/gitbucket/core/service/PullRequestService.scala index 0f7f50c..c6cde8e 100644 --- a/src/main/scala/gitbucket/core/service/PullRequestService.scala +++ b/src/main/scala/gitbucket/core/service/PullRequestService.scala @@ -116,6 +116,24 @@ .map { case (t1, t2) => t1 } .list + def getPullRequestsByBranch(userName: String, repositoryName: String, branch: String, closed: Option[Boolean])( + implicit s: Session + ): List[PullRequest] = + PullRequests + .join(Issues) + .on { (t1, t2) => + t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) + } + .filter { + case (t1, t2) => + (t1.requestUserName === userName.bind) && + (t1.requestRepositoryName === repositoryName.bind) && + (t1.branch === branch.bind) && + (t2.closed === closed.get.bind, closed.isDefined) + } + .map { case (t1, t2) => t1 } + .list + /** * for repository viewer. * 1. find pull request from `branch` to other branch on same repository @@ -358,6 +376,14 @@ .sortWith(_.registeredDate before _.registeredDate) } + def markMergeAndClosePullRequest(userName: String, owner: String, repository: String, pull: PullRequest)( + implicit s: Session + ): Unit = { + createComment(owner, repository, userName, pull.issueId, "Merged by user", "merge") + createComment(owner, repository, userName, pull.issueId, "Close", "close") + updateClosed(owner, repository, pull.issueId, true) + } + } object PullRequestService { diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index 0a8d956..3ae5257 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -303,6 +303,19 @@ } else None } + // set PR as merged + val pulls = getPullRequestsByBranch(owner, repository, branchName, Some(false)) + pulls.foreach { pull => + if (commits.find { c => + c.id == pull.commitIdTo + }.isDefined) { + markMergeAndClosePullRequest(pusher, owner, repository, pull) + getAccountByUserName(pusher).map { pusherAccount => + callPullRequestWebHook("closed", repositoryInfo, pull.issueId, baseUrl, pusherAccount) + } + } + } + // record activity if (refName(1) == "heads") { command.getType match {