diff --git a/src/main/scala/gitbucket/core/controller/DashboardController.scala b/src/main/scala/gitbucket/core/controller/DashboardController.scala index 3dc8d1a..a67e294 100644 --- a/src/main/scala/gitbucket/core/controller/DashboardController.scala +++ b/src/main/scala/gitbucket/core/controller/DashboardController.scala @@ -9,6 +9,7 @@ class DashboardController extends DashboardControllerBase with IssuesService + with MergeService with PullRequestService with RepositoryService with AccountService diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index 2c2fa68..efc0acb 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -24,6 +24,7 @@ with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator + with MergeService with PullRequestService with WebHookIssueCommentService with WebHookPullRequestReviewCommentService diff --git a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala index 022acf4..8509115 100644 --- a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala +++ b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala @@ -534,15 +534,15 @@ ) createPullRequest( - originUserName = repository.owner, - originRepositoryName = repository.name, + originRepository = repository, issueId = issueId, originBranch = form.targetBranch, requestUserName = form.requestUserName, requestRepositoryName = form.requestRepositoryName, requestBranch = form.requestBranch, commitIdFrom = form.commitIdFrom, - commitIdTo = form.commitIdTo + commitIdTo = form.commitIdTo, + loginAccount = context.loginAccount.get ) // insert labels @@ -557,29 +557,6 @@ } } - // fetch requested branch - fetchAsPullRequest(owner, name, form.requestUserName, form.requestRepositoryName, form.requestBranch, issueId) - - // record activity - recordPullRequestActivity(owner, name, loginUserName, issueId, form.title) - - // call web hook - callPullRequestWebHook("opened", repository, issueId, context.loginAccount.get) - - getIssue(owner, name, issueId.toString) foreach { issue => - // extract references and create refer comment - createReferComment( - owner, - name, - issue, - form.title + " " + form.content.getOrElse(""), - context.loginAccount.get - ) - - // call hooks - PluginRegistry().getPullRequestHooks.foreach(_.created(issue, repository)) - } - redirect(s"/${owner}/${name}/pull/${issueId}") } }) diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index c842b22..5fca3c7 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -50,6 +50,7 @@ with ReadableUsersAuthenticator with ReferrerAuthenticator with WritableUsersAuthenticator + with MergeService with PullRequestService with CommitStatusService with WebHookPullRequestService diff --git a/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala index 13cc75d..14aaccb 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala @@ -106,15 +106,15 @@ ) createPullRequest( - originUserName = repository.owner, - originRepositoryName = repository.name, + originRepository = repository, issueId = issueId, originBranch = createPullReq.base, requestUserName = reqOwner, requestRepositoryName = repository.name, requestBranch = reqBranch, commitIdFrom = commitIdFrom.getName, - commitIdTo = commitIdTo.getName + commitIdTo = commitIdTo.getName, + loginAccount = context.loginAccount.get ) getApiPullRequest(repository, issueId).map(JsonFormat(_)) case _ => @@ -133,15 +133,15 @@ case (Some(commitIdFrom), Some(commitIdTo)) => changeIssueToPullRequest(repository.owner, repository.name, createPullReqAlt.issue) createPullRequest( - originUserName = repository.owner, - originRepositoryName = repository.name, + originRepository = repository, issueId = createPullReqAlt.issue, originBranch = createPullReqAlt.base, requestUserName = reqOwner, requestRepositoryName = repository.name, requestBranch = reqBranch, commitIdFrom = commitIdFrom.getName, - commitIdTo = commitIdTo.getName + commitIdTo = commitIdTo.getName, + loginAccount = context.loginAccount.get ) getApiPullRequest(repository, createPullReqAlt.issue).map(JsonFormat(_)) case _ => diff --git a/src/main/scala/gitbucket/core/service/PullRequestService.scala b/src/main/scala/gitbucket/core/service/PullRequestService.scala index 185955c..2a3cd80 100644 --- a/src/main/scala/gitbucket/core/service/PullRequestService.scala +++ b/src/main/scala/gitbucket/core/service/PullRequestService.scala @@ -6,6 +6,8 @@ import difflib.{Delta, DiffUtils} import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.api.JsonFormat +import gitbucket.core.controller.Context +import gitbucket.core.plugin.PluginRegistry import gitbucket.core.util.SyntaxSugars._ import gitbucket.core.util.Directory._ import gitbucket.core.util.Implicits._ @@ -20,7 +22,13 @@ import scala.collection.JavaConverters._ trait PullRequestService { - self: IssuesService with CommitsService with WebHookService with WebHookPullRequestService with RepositoryService => + self: IssuesService + with CommitsService + with WebHookService + with WebHookPullRequestService + with RepositoryService + with MergeService + with ActivityService => import PullRequestService._ def getPullRequest(owner: String, repository: String, issueId: Int)( @@ -81,27 +89,66 @@ // .map { x => PullRequestCount(x._1, x._2) } def createPullRequest( - originUserName: String, - originRepositoryName: String, + originRepository: RepositoryInfo, issueId: Int, originBranch: String, requestUserName: String, requestRepositoryName: String, requestBranch: String, commitIdFrom: String, - commitIdTo: String - )(implicit s: Session): Unit = - PullRequests insert PullRequest( - originUserName, - originRepositoryName, - issueId, - originBranch, - requestUserName, - requestRepositoryName, - requestBranch, - commitIdFrom, - commitIdTo - ) + commitIdTo: String, + loginAccount: Account + )(implicit s: Session, context: Context): Unit = { + getIssue(originRepository.owner, originRepository.name, issueId.toString).foreach { baseIssue => + PullRequests insert PullRequest( + originRepository.owner, + originRepository.name, + issueId, + originBranch, + requestUserName, + requestRepositoryName, + requestBranch, + commitIdFrom, + commitIdTo + ) + + // fetch requested branch + fetchAsPullRequest( + originRepository.owner, + originRepository.name, + requestUserName, + requestRepositoryName, + requestBranch, + issueId + ) + + // record activity + recordPullRequestActivity( + originRepository.owner, + originRepository.name, + loginAccount.userName, + issueId, + baseIssue.title + ) + + // call web hook + callPullRequestWebHook("opened", originRepository, issueId, loginAccount) + + getIssue(originRepository.owner, originRepository.name, issueId.toString) foreach { issue => + // extract references and create refer comment + createReferComment( + originRepository.owner, + originRepository.name, + issue, + baseIssue.title + " " + baseIssue.content, + loginAccount + ) + + // call hooks + PluginRegistry().getPullRequestHooks.foreach(_.created(issue, originRepository)) + } + } + } def getPullRequestsByRequest(userName: String, repositoryName: String, branch: String, closed: Option[Boolean])( implicit s: Session diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index 46dbcc4..68df7da 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -215,6 +215,7 @@ with AccountService with IssuesService with ActivityService + with MergeService with PullRequestService with WebHookService with LabelsService diff --git a/src/test/scala/gitbucket/core/service/PullRequestServiceSpec.scala b/src/test/scala/gitbucket/core/service/PullRequestServiceSpec.scala index bba472c..2f1db6d 100644 --- a/src/test/scala/gitbucket/core/service/PullRequestServiceSpec.scala +++ b/src/test/scala/gitbucket/core/service/PullRequestServiceSpec.scala @@ -6,6 +6,7 @@ class PullRequestServiceSpec extends FunSpec with ServiceSpecBase + with MergeService with PullRequestService with IssuesService with AccountService diff --git a/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala b/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala index 86b3f26..4842d66 100644 --- a/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala +++ b/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala @@ -10,12 +10,12 @@ import gitbucket.core.model.Profile._ import gitbucket.core.model.Profile.profile._ import gitbucket.core.model.Profile.profile.blockingApi._ - import org.apache.commons.io.FileUtils - import java.sql.DriverManager import java.io.File +import gitbucket.core.controller.Context + import scala.util.Random trait ServiceSpecBase { @@ -44,8 +44,8 @@ def user(name: String)(implicit s: Session): Account = AccountService.getAccountByUserName(name).get lazy val dummyService = new RepositoryService with AccountService with ActivityService with IssuesService - with PullRequestService with CommitsService with CommitStatusService with LabelsService with MilestonesService - with PrioritiesService with WebHookService with WebHookPullRequestService + with MergeService with PullRequestService with CommitsService with CommitStatusService with LabelsService + with MilestonesService with PrioritiesService with WebHookService with WebHookPullRequestService with WebHookPullRequestReviewCommentService {} def generateNewUserWithDBRepository(userName: String, repositoryName: String)(implicit s: Session): Account = { @@ -76,16 +76,18 @@ val Array(baseUserName, baseRepositoryName, baesBranch) = base.split("/") val Array(requestUserName, requestRepositoryName, requestBranch) = request.split("/") val issueId = generateNewIssue(baseUserName, baseRepositoryName, Option(loginUser).getOrElse(requestUserName)) + val baseRepository = dummyService.getRepository(baseUserName, baseRepositoryName) + val loginAccount = dummyService.getAccountByUserName(loginUser) dummyService.createPullRequest( - originUserName = baseUserName, - originRepositoryName = baseRepositoryName, + originRepository = baseRepository.get, issueId = issueId, originBranch = baesBranch, requestUserName = requestUserName, requestRepositoryName = requestRepositoryName, requestBranch = requestBranch, commitIdFrom = baesBranch, - commitIdTo = requestBranch + commitIdTo = requestBranch, + loginAccount = loginAccount.get ) dummyService.getPullRequest(baseUserName, baseRepositoryName, issueId).get } diff --git a/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala b/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala index 957aa93..92a4aa3 100644 --- a/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala +++ b/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala @@ -6,8 +6,8 @@ class WebHookServiceSpec extends FunSuite with ServiceSpecBase { lazy val service = new WebHookPullRequestService with AccountService with ActivityService with RepositoryService - with PullRequestService with IssuesService with CommitsService with LabelsService with MilestonesService - with PrioritiesService with WebHookPullRequestReviewCommentService + with MergeService with PullRequestService with IssuesService with CommitsService with LabelsService + with MilestonesService with PrioritiesService with WebHookPullRequestReviewCommentService test("WebHookPullRequestService.getPullRequestsByRequestForWebhook") { withTestDB { implicit session =>