Skip to content

Commit 6c31738

Browse files
authored
Merge pull request #31 from takezoe/fix-pull-request-build
Fix pull request build
2 parents 7d2d2f8 + 0cc0fe9 commit 6c31738

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

src/main/scala/Plugin.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import javax.servlet.ServletContext
22

33
import gitbucket.core.controller.Context
4-
import gitbucket.core.plugin.{Link, PluginRegistry, ReceiveHook, RepositoryHook}
4+
import gitbucket.core.plugin._
55
import gitbucket.core.service.RepositoryService.RepositoryInfo
66
import gitbucket.core.service.SystemSettingsService
77
import io.github.gitbucket.ci.controller.CIController
8-
import io.github.gitbucket.ci.hook.{CICommitHook, CIRepositoryHook}
8+
import io.github.gitbucket.ci.hook.{CICommitHook, CIPullRequestHook, CIRepositoryHook}
99
import io.github.gitbucket.ci.manager.BuildManager
1010
import io.github.gitbucket.solidbase.migration.LiquibaseMigration
1111
import io.github.gitbucket.solidbase.model.Version
@@ -36,6 +36,7 @@ class Plugin extends gitbucket.core.plugin.Plugin {
3636

3737
override val receiveHooks: Seq[ReceiveHook] = Seq(new CICommitHook())
3838
override val repositoryHooks: Seq[RepositoryHook] = Seq(new CIRepositoryHook())
39+
override val pullRequestHooks: Seq[PullRequestHook] = Seq(new CIPullRequestHook())
3940

4041
// TODO GitBucket should provide a hook method to initialize plugin...
4142
BuildManager.startBuildManager()

src/main/scala/io/github/gitbucket/ci/hook/CICommitHook.scala

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,32 @@ class CICommitHook extends ReceiveHook
4141
config = config
4242
)
4343
}
44-
} else {
45-
val pullRequests = PullRequests
44+
}
45+
46+
for {
47+
(pullreq, issue) <- PullRequests
4648
.join(Issues).on { (t1, t2) => t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) }
4749
.filter { case (t1, t2) =>
4850
(t1.requestUserName === owner.bind) && (t1.requestRepositoryName === repository.bind) &&
49-
(t1.requestBranch === branch.bind) && (t2.closed === false.bind)
51+
(t1.requestBranch === branch.bind) && (t2.closed === false.bind)
5052
}
5153
.list
52-
53-
pullRequests.foreach { case (pullRequest, issue) =>
54-
loadCIConfig(pullRequest.userName, pullRequest.repositoryName).foreach { config =>
55-
runBuild(
56-
userName = pullRequest.userName,
57-
repositoryName = pullRequest.repositoryName,
58-
buildUserName = owner,
59-
buildRepositoryName = repository,
60-
buildBranch = branch,
61-
sha = sha,
62-
commitMessage = revCommit.getShortMessage,
63-
commitUserName = revCommit.getCommitterIdent.getName,
64-
commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
65-
pullRequestId = Some(pullRequest.issueId),
66-
pusher = pusher,
67-
config = config
68-
)
69-
}
70-
}
54+
buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
55+
} yield {
56+
runBuild(
57+
userName = pullreq.userName,
58+
repositoryName = pullreq.repositoryName,
59+
buildUserName = owner,
60+
buildRepositoryName = repository,
61+
buildBranch = branch,
62+
sha = sha,
63+
commitMessage = revCommit.getShortMessage,
64+
commitUserName = revCommit.getCommitterIdent.getName,
65+
commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
66+
pullRequestId = Some(pullreq.issueId),
67+
pusher = pusher,
68+
config = buildConfig
69+
)
7170
}
7271
}
7372
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.gitbucket.ci.hook
2+
3+
import gitbucket.core.controller.Context
4+
import gitbucket.core.model.Issue
5+
import gitbucket.core.plugin.PullRequestHook
6+
import gitbucket.core.service.RepositoryService.RepositoryInfo
7+
import gitbucket.core.model.Profile._
8+
import gitbucket.core.service._
9+
import gitbucket.core.util.Directory.getRepositoryDir
10+
import gitbucket.core.util.JGitUtil
11+
import gitbucket.core.util.SyntaxSugars.using
12+
import io.github.gitbucket.ci.service.CIService
13+
import org.eclipse.jgit.api.Git
14+
import profile.api._
15+
16+
class CIPullRequestHook extends PullRequestHook
17+
with PullRequestService with IssuesService with CommitsService with AccountService
18+
with RepositoryService with CIService {
19+
20+
override def created(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = {
21+
if(issue.isPullRequest){
22+
for {
23+
(_, pullreq) <- getPullRequest(issue.userName, issue.repositoryName, issue.issueId)
24+
buildAuthor <- context.loginAccount
25+
buildConfig <- loadCIConfig(pullreq.userName, pullreq.repositoryName)
26+
} yield {
27+
val revCommit = using(Git.open(getRepositoryDir(pullreq.requestUserName, pullreq.requestRepositoryName))) { git =>
28+
val objectId = git.getRepository.resolve(pullreq.commitIdTo)
29+
JGitUtil.getRevCommitFromId(git, objectId)
30+
}
31+
runBuild(
32+
userName = pullreq.userName,
33+
repositoryName = pullreq.repositoryName,
34+
buildUserName = pullreq.requestUserName,
35+
buildRepositoryName = pullreq.requestRepositoryName,
36+
buildBranch = pullreq.requestBranch,
37+
sha = pullreq.commitIdTo,
38+
commitMessage = revCommit.getShortMessage,
39+
commitUserName = revCommit.getCommitterIdent.getName,
40+
commitMailAddress = revCommit.getCommitterIdent.getEmailAddress,
41+
pullRequestId = Some(pullreq.issueId),
42+
buildAuthor = buildAuthor,
43+
config = buildConfig
44+
)
45+
}
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)