Fetch pull request from source repository after updating repository.

This commit is contained in:
odz
2013-11-20 23:59:26 +09:00
parent f38924c7fe
commit 223ba791fe
4 changed files with 42 additions and 18 deletions

View File

@@ -70,11 +70,8 @@ trait PullRequestsControllerBase extends ControllerBase {
val name = repository.name
getPullRequest(owner, name, issueId) map { case(issue, pullreq) =>
using(Git.open(getRepositoryDir(owner, name))){ git =>
// prepare head branch
val commitIdTo = fetchPullRequest(git, issueId, pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.requestBranch)
updateCommitIdTo(owner, name, issueId, commitIdTo)
val (commits, diffs) = getRequestCompareInfo(owner, name, pullreq.commitIdFrom, owner, name, commitIdTo)
val (commits, diffs) =
getRequestCompareInfo(owner, name, pullreq.commitIdFrom, owner, name, pullreq.commitIdTo)
pulls.html.pullreq(
issue, pullreq,
@@ -446,16 +443,4 @@ trait PullRequestsControllerBase extends ControllerBase {
hasWritePermission(owner, repoName, context.loginAccount))
}
/**
* Fetch pull request contents into refs/pull/${issueId}/head and return the head commit id of the pull request.
*/
private def fetchPullRequest(git: Git, issueId: Int, requestUserName: String, requestRepositoryName: String, requestBranch: String): String = {
git.fetch
.setRemote(getRepositoryDir(requestUserName, requestRepositoryName).toURI.toString)
.setRefSpecs(new RefSpec(s"refs/heads/${requestBranch}:refs/pull/${issueId}/head").setForceUpdate(true))
.call
git.getRepository.resolve(s"refs/pull/${issueId}/head").getName
}
}

View File

@@ -13,6 +13,11 @@ object PullRequests extends Table[PullRequest]("PULL_REQUEST") with IssueTemplat
def byPrimaryKey(userName: String, repositoryName: String, issueId: Int) = byIssue(userName, repositoryName, issueId)
def byPrimaryKey(userName: Column[String], repositoryName: Column[String], issueId: Column[Int]) = byIssue(userName, repositoryName, issueId)
def byRequestBranch(userName: String, repositoryName: String, branch: String) =
(requestUserName is userName.bind) &&
(requestRepositoryName is repositoryName.bind) &&
(requestBranch is branch.bind)
}
case class PullRequest(

View File

@@ -46,6 +46,13 @@ trait PullRequestService { self: IssuesService =>
commitIdFrom,
commitIdTo))
def getPullRequestsByRequest(userName: String, repositoryName: String, branch: String, closed: Boolean): List[PullRequest] =
Query(PullRequests)
.innerJoin(Issues).on { (t1, t2) => t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) }
.filter { case (t1, t2) => t1.byRequestBranch(userName, repositoryName, branch) && t2.closed is closed.bind }
.map { case (t1, t2) => t1 }
.list
}
object PullRequestService {

View File

@@ -78,7 +78,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
import scala.collection.JavaConverters._
class CommitLogHook(owner: String, repository: String, userName: String, baseURL: String) extends PostReceiveHook
with RepositoryService with AccountService with IssuesService with ActivityService with WebHookService {
with RepositoryService with AccountService with IssuesService with ActivityService with PullRequestService with WebHookService {
private val logger = LoggerFactory.getLogger(classOf[CommitLogHook])
@@ -130,6 +130,16 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL
}
}
if(refName(1) == "heads"){
command.getType match {
case ReceiveCommand.Type.CREATE |
ReceiveCommand.Type.UPDATE |
ReceiveCommand.Type.UPDATE_NONFASTFORWARD =>
updatePullRequests(branchName)
case _ =>
}
}
// call web hook
val webHookURLs = getWebHookURLs(owner, repository)
if(webHookURLs.nonEmpty){
@@ -158,4 +168,21 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL
}
}
/**
* Fetch pull request contents into refs/pull/${issueId}/head and update pull request table.
*/
private def updatePullRequests(branch: String) =
getPullRequestsByRequest(owner, repository, branch, false).foreach { pullreq =>
if(getRepository(pullreq.userName, pullreq.repositoryName, baseURL).isDefined){
using(Git.open(Directory.getRepositoryDir(pullreq.userName, pullreq.repositoryName))){ git =>
git.fetch
.setRemote(Directory.getRepositoryDir(owner, repository).toURI.toString)
.setRefSpecs(new RefSpec(s"refs/heads/${branch}:refs/pull/${pullreq.issueId}/head").setForceUpdate(true))
.call
val commitIdTo = git.getRepository.resolve(s"refs/pull/${pullreq.issueId}/head").getName
updateCommitIdTo(pullreq.userName, pullreq.repositoryName, pullreq.issueId, commitIdTo)
}
}
}
}