Change CommitHook to ReceiveHook

This commit is contained in:
Naoki Takezoe
2016-01-19 12:26:20 +09:00
parent 03d4b9e9c6
commit 1b4c621fef
7 changed files with 43 additions and 30 deletions

View File

@@ -1,15 +0,0 @@
package gitbucket.core.plugin
import gitbucket.core.model.Profile._
import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand}
import profile.simple._
trait CommitHook {
def preCommit(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String)
(implicit session: Session): Option[String] = None
def postCommit(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String)
(implicit session: Session): Unit = ()
}

View File

@@ -67,6 +67,16 @@ trait Plugin {
*/ */
def repositoryRoutings(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[GitRepositoryRouting] = Nil def repositoryRoutings(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[GitRepositoryRouting] = Nil
/**
* Override to add receive hooks.
*/
val receiveHooks: Seq[ReceiveHook] = Nil
/**
* Override to add receive hooks.
*/
def receiveHooks(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[ReceiveHook] = Nil
/** /**
* This method is invoked in initialization of plugin system. * This method is invoked in initialization of plugin system.
* Register plugin functionality to PluginRegistry. * Register plugin functionality to PluginRegistry.
@@ -87,6 +97,9 @@ trait Plugin {
(repositoryRoutings ++ repositoryRoutings(registry, context, settings)).foreach { routing => (repositoryRoutings ++ repositoryRoutings(registry, context, settings)).foreach { routing =>
registry.addRepositoryRouting(routing) registry.addRepositoryRouting(routing)
} }
(receiveHooks ++ receiveHooks(registry, context, settings)).foreach { receiveHook =>
registry.addReceiveHook(receiveHook)
}
} }
/** /**

View File

@@ -6,7 +6,7 @@ import javax.servlet.ServletContext
import javax.servlet.http.{HttpServletRequest, HttpServletResponse} import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import gitbucket.core.controller.{Context, ControllerBase} import gitbucket.core.controller.{Context, ControllerBase}
import gitbucket.core.service.ProtectedBranchService.ProtectedBranchCommitHook import gitbucket.core.service.ProtectedBranchService.ProtectedBranchReceiveHook
import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.service.RepositoryService.RepositoryInfo
import gitbucket.core.service.SystemSettingsService.SystemSettings import gitbucket.core.service.SystemSettingsService.SystemSettings
import gitbucket.core.util.ControlUtil._ import gitbucket.core.util.ControlUtil._
@@ -30,8 +30,8 @@ class PluginRegistry {
"md" -> MarkdownRenderer, "markdown" -> MarkdownRenderer "md" -> MarkdownRenderer, "markdown" -> MarkdownRenderer
) )
private val repositoryRoutings = new ListBuffer[GitRepositoryRouting] private val repositoryRoutings = new ListBuffer[GitRepositoryRouting]
private val commitHooks = new ListBuffer[CommitHook] private val receiveHooks = new ListBuffer[ReceiveHook]
commitHooks += new ProtectedBranchCommitHook() receiveHooks += new ProtectedBranchReceiveHook()
def addPlugin(pluginInfo: PluginInfo): Unit = { def addPlugin(pluginInfo: PluginInfo): Unit = {
plugins += pluginInfo plugins += pluginInfo
@@ -101,11 +101,11 @@ class PluginRegistry {
} }
} }
def addCommitHook(commitHook: CommitHook): Unit = { def addReceiveHook(commitHook: ReceiveHook): Unit = {
commitHooks += commitHook receiveHooks += commitHook
} }
def getCommitHooks: Seq[CommitHook] = commitHooks.toSeq def getReceiveHooks: Seq[ReceiveHook] = receiveHooks.toSeq
private case class GlobalAction( private case class GlobalAction(
method: String, method: String,

View File

@@ -0,0 +1,15 @@
package gitbucket.core.plugin
import gitbucket.core.model.Profile._
import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand}
import profile.simple._
trait ReceiveHook {
def preReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String)
(implicit session: Session): Option[String] = None
def postReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String)
(implicit session: Session): Unit = ()
}

View File

@@ -2,7 +2,7 @@ package gitbucket.core.service
import gitbucket.core.model._ import gitbucket.core.model._
import gitbucket.core.model.Profile._ import gitbucket.core.model.Profile._
import gitbucket.core.plugin.CommitHook import gitbucket.core.plugin.ReceiveHook
import profile.simple._ import profile.simple._
import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand} import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand}
@@ -45,8 +45,8 @@ trait ProtectedBranchService {
object ProtectedBranchService { object ProtectedBranchService {
class ProtectedBranchCommitHook extends CommitHook with ProtectedBranchService { class ProtectedBranchReceiveHook extends ReceiveHook with ProtectedBranchService {
override def preCommit(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) override def preReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String)
(implicit session: Session): Option[String] = { (implicit session: Session): Option[String] = {
val branch = command.getRefName.stripPrefix("refs/heads/") val branch = command.getRefName.stripPrefix("refs/heads/")
if(branch != command.getRefName){ if(branch != command.getRefName){

View File

@@ -120,8 +120,8 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
try { try {
commands.asScala.foreach { command => commands.asScala.foreach { command =>
// call pre-commit hook // call pre-commit hook
PluginRegistry().getCommitHooks PluginRegistry().getReceiveHooks
.flatMap(_.preCommit(owner, repository, receivePack, command, pusher)) .flatMap(_.preReceive(owner, repository, receivePack, command, pusher))
.headOption.foreach { error => .headOption.foreach { error =>
command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error) command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error)
} }
@@ -217,7 +217,7 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
} }
// call post-commit hook // call post-commit hook
PluginRegistry().getCommitHooks.foreach(_.postCommit(owner, repository, receivePack, command, pusher)) PluginRegistry().getReceiveHooks.foreach(_.postReceive(owner, repository, receivePack, command, pusher))
} }
} }
// update repository last modified time. // update repository last modified time.

View File

@@ -4,11 +4,11 @@ import org.specs2.mutable.Specification
import org.eclipse.jgit.transport.ReceiveCommand import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.lib.ObjectId import org.eclipse.jgit.lib.ObjectId
import gitbucket.core.model.CommitState import gitbucket.core.model.CommitState
import gitbucket.core.service.ProtectedBranchService.{ProtectedBranchCommitHook, ProtectedBranchInfo} import gitbucket.core.service.ProtectedBranchService.{ProtectedBranchReceiveHook, ProtectedBranchInfo}
class ProtectedBranchServiceSpec extends Specification with ServiceSpecBase with ProtectedBranchService with CommitStatusService { class ProtectedBranchServiceSpec extends Specification with ServiceSpecBase with ProtectedBranchService with CommitStatusService {
val commitHook = new ProtectedBranchCommitHook() val commitHook = new ProtectedBranchReceiveHook()
val now = new java.util.Date() val now = new java.util.Date()
val sha = "0c77148632618b59b6f70004e3084002be2b8804" val sha = "0c77148632618b59b6f70004e3084002be2b8804"
val sha2 = "0c77148632618b59b6f70004e3084002be2b8805" val sha2 = "0c77148632618b59b6f70004e3084002be2b8805"