diff --git a/src/main/scala/gitbucket/core/api/ApiPullRequest.scala b/src/main/scala/gitbucket/core/api/ApiPullRequest.scala index 52bb9a7..8efa944 100644 --- a/src/main/scala/gitbucket/core/api/ApiPullRequest.scala +++ b/src/main/scala/gitbucket/core/api/ApiPullRequest.scala @@ -15,6 +15,8 @@ head: ApiPullRequest.Commit, base: ApiPullRequest.Commit, mergeable: Option[Boolean], + merged: Boolean, + merged_at: Option[Date], title: String, body: String, user: ApiUser) { @@ -31,7 +33,8 @@ } object ApiPullRequest{ - def apply(issue: Issue, pullRequest: PullRequest, headRepo: ApiRepository, baseRepo: ApiRepository, user: ApiUser): ApiPullRequest = + def apply(issue: Issue, pullRequest: PullRequest, headRepo: ApiRepository, baseRepo: ApiRepository, user: ApiUser, + /*mergeable: Boolean,*/ merged: Boolean, mergedAt: Option[Date]): ApiPullRequest = ApiPullRequest( number = issue.issueId, updated_at = issue.updatedDate, @@ -45,6 +48,8 @@ ref = pullRequest.branch, repo = baseRepo)(issue.userName), mergeable = None, // TODO: need check mergeable. + merged = merged, + merged_at = mergedAt, title = issue.title, body = issue.content.getOrElse(""), user = user diff --git a/src/main/scala/gitbucket/core/controller/ApiController.scala b/src/main/scala/gitbucket/core/controller/ApiController.scala index e5229c3..918bf9c 100644 --- a/src/main/scala/gitbucket/core/controller/ApiController.scala +++ b/src/main/scala/gitbucket/core/controller/ApiController.scala @@ -405,13 +405,18 @@ repos = repository.owner -> repository.name ) + JsonFormat(issues.map { case (issue, issueUser, commentCount, pullRequest, headRepo, headOwner) => + val comments = getCommentsForApi(repository.owner, repository.name, issue.issueId) + ApiPullRequest( - issue, - pullRequest, - ApiRepository(headRepo, ApiUser(headOwner)), - ApiRepository(repository, ApiUser(baseOwner)), - ApiUser(issueUser) + issue = issue, + pullRequest = pullRequest, + headRepo = ApiRepository(headRepo, ApiUser(headOwner)), + baseRepo = ApiRepository(repository, ApiUser(baseOwner)), + user = ApiUser(issueUser), + merged = comments.exists { case (comment, _, _) => comment.action == "merged" }, + mergedAt = comments.collectFirst { case (comment, _, _) if(comment.action == "merged") => comment.registeredDate } ) }) }) @@ -429,12 +434,17 @@ issueUser <- users.get(issue.openedUserName) headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) } yield { + val comments = getCommentsForApi(repository.owner, repository.name, issueId) + JsonFormat(ApiPullRequest( - issue, - pullRequest, - ApiRepository(headRepo, ApiUser(headOwner)), - ApiRepository(repository, ApiUser(baseOwner)), - ApiUser(issueUser))) + issue = issue, + pullRequest = pullRequest, + headRepo = ApiRepository(headRepo, ApiUser(headOwner)), + baseRepo = ApiRepository(repository, ApiUser(baseOwner)), + user = ApiUser(issueUser), + merged = comments.exists { case (comment, _, _) => comment.action == "merged" }, + mergedAt = comments.collectFirst { case (comment, _, _) if(comment.action == "merged") => comment.registeredDate } + )) }) getOrElse NotFound() }) diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index d50f9e7..86ba402 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -1,9 +1,11 @@ package gitbucket.core.service +import java.util.Date + import fr.brouillard.oss.security.xhub.XHub -import fr.brouillard.oss.security.xhub.XHub.{XHubDigest, XHubConverter} +import fr.brouillard.oss.security.xhub.XHub.{XHubConverter, XHubDigest} import gitbucket.core.api._ -import gitbucket.core.model.{WebHook, Account, Issue, PullRequest, IssueComment, WebHookEvent, CommitComment} +import gitbucket.core.model.{Account, CommitComment, Issue, IssueComment, PullRequest, WebHook, WebHookEvent} import gitbucket.core.model.Profile._ import org.apache.http.client.utils.URLEncodedUtils import profile.simple._ @@ -16,6 +18,7 @@ import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.ObjectId import org.slf4j.LoggerFactory + import scala.concurrent._ import org.apache.http.HttpRequest import org.apache.http.HttpResponse @@ -189,6 +192,8 @@ issueUser <- users.get(issue.openedUserName) headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) } yield { + val comments = getCommentsForApi(repository.owner, repository.name, issueId) + WebHookPullRequestPayload( action = action, issue = issue, @@ -198,7 +203,10 @@ headOwner = headOwner, baseRepository = repository, baseOwner = baseOwner, - sender = sender) + sender = sender, + merged = comments.exists { case (comment, _, _) => comment.action == "merged" }, + mergedAt = comments.collectFirst { case (comment, _, _) if(comment.action == "merged") => comment.registeredDate } + ) } } } @@ -228,6 +236,8 @@ ((issue, issueUser, pullRequest, baseOwner, headOwner), webHooks) <- getPullRequestsByRequestForWebhook(requestRepository.owner, requestRepository.name, requestBranch) baseRepo <- getRepository(pullRequest.userName, pullRequest.repositoryName) } yield { + val comments = getCommentsForApi(baseRepo.owner, baseRepo.name, issue.issueId) + val payload = WebHookPullRequestPayload( action = action, issue = issue, @@ -237,7 +247,11 @@ headOwner = headOwner, baseRepository = baseRepo, baseOwner = baseOwner, - sender = sender) + sender = sender, + merged = comments.exists { case (comment, _, _) => comment.action == "merged" }, + mergedAt = comments.collectFirst { case (comment, _, _) if(comment.action == "merged") => comment.registeredDate } + ) + callWebHook(WebHook.PullRequest, webHooks, payload) } } @@ -257,6 +271,8 @@ issueUser <- users.get(issue.openedUserName) headRepo <- getRepository(pullRequest.requestUserName, pullRequest.requestRepositoryName) } yield { + val comments = getCommentsForApi(repository.owner, repository.name, issue.issueId) + WebHookPullRequestReviewCommentPayload( action = action, comment = comment, @@ -267,7 +283,10 @@ headOwner = headOwner, baseRepository = repository, baseOwner = baseOwner, - sender = sender) + sender = sender, + merged = comments.exists { case (comment, _, _) => comment.action == "merged" }, + mergedAt = comments.collectFirst { case (comment, _, _) if(comment.action == "merged") => comment.registeredDate } + ) } } } @@ -365,11 +384,14 @@ headOwner: Account, baseRepository: RepositoryInfo, baseOwner: Account, - sender: Account): WebHookPullRequestPayload = { + sender: Account, + merged: Boolean, + mergedAt: Option[Date]): WebHookPullRequestPayload = { val headRepoPayload = ApiRepository(headRepository, headOwner) val baseRepoPayload = ApiRepository(baseRepository, baseOwner) val senderPayload = ApiUser(sender) - val pr = ApiPullRequest(issue, pullRequest, headRepoPayload, baseRepoPayload, ApiUser(issueUser)) + val pr = ApiPullRequest(issue, pullRequest, headRepoPayload, baseRepoPayload, ApiUser(issueUser), merged, mergedAt) + WebHookPullRequestPayload( action = action, number = issue.issueId, @@ -389,7 +411,7 @@ sender: ApiUser ) extends WebHookPayload - object WebHookIssueCommentPayload{ + object WebHookIssueCommentPayload { def apply( issue: Issue, issueUser: Account, @@ -415,28 +437,44 @@ sender: ApiUser ) extends WebHookPayload - object WebHookPullRequestReviewCommentPayload{ + object WebHookPullRequestReviewCommentPayload { def apply( - action: String, - comment: CommitComment, - issue: Issue, - issueUser: Account, - pullRequest: PullRequest, - headRepository: RepositoryInfo, - headOwner: Account, - baseRepository: RepositoryInfo, - baseOwner: Account, - sender: Account - ) : WebHookPullRequestReviewCommentPayload = { + action: String, + comment: CommitComment, + issue: Issue, + issueUser: Account, + pullRequest: PullRequest, + headRepository: RepositoryInfo, + headOwner: Account, + baseRepository: RepositoryInfo, + baseOwner: Account, + sender: Account, + merged: Boolean, + mergedAt: Option[Date] + ) : WebHookPullRequestReviewCommentPayload = { val headRepoPayload = ApiRepository(headRepository, headOwner) val baseRepoPayload = ApiRepository(baseRepository, baseOwner) val senderPayload = ApiUser(sender) + WebHookPullRequestReviewCommentPayload( - action = action, - comment = ApiPullRequestReviewComment(comment, senderPayload, RepositoryName(baseRepository), issue.issueId), - pull_request = ApiPullRequest(issue, pullRequest, headRepoPayload, baseRepoPayload, ApiUser(issueUser)), - repository = baseRepoPayload, - sender = senderPayload) + action = action, + comment = ApiPullRequestReviewComment( + comment = comment, + commentedUser = senderPayload, + repositoryName = RepositoryName(baseRepository), + issueId = issue.issueId + ), + pull_request = ApiPullRequest( + issue = issue, + pullRequest = pullRequest, + headRepo = headRepoPayload, + baseRepo = baseRepoPayload, + user = ApiUser(issueUser), + merged = merged, + mergedAt = mergedAt + ), + repository = baseRepoPayload, + sender = senderPayload) } } }