diff --git a/src/main/scala/gitbucket/core/plugin/CommitHook.scala b/src/main/scala/gitbucket/core/plugin/CommitHook.scala deleted file mode 100644 index a7af9b8..0000000 --- a/src/main/scala/gitbucket/core/plugin/CommitHook.scala +++ /dev/null @@ -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 = () - -} diff --git a/src/main/scala/gitbucket/core/plugin/Plugin.scala b/src/main/scala/gitbucket/core/plugin/Plugin.scala index 975791d..482e404 100644 --- a/src/main/scala/gitbucket/core/plugin/Plugin.scala +++ b/src/main/scala/gitbucket/core/plugin/Plugin.scala @@ -68,6 +68,16 @@ 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. * Register plugin functionality to PluginRegistry. */ @@ -87,6 +97,9 @@ (repositoryRoutings ++ repositoryRoutings(registry, context, settings)).foreach { routing => registry.addRepositoryRouting(routing) } + (receiveHooks ++ receiveHooks(registry, context, settings)).foreach { receiveHook => + registry.addReceiveHook(receiveHook) + } } /** diff --git a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala index 0b87c68..e69ab61 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRegistory.scala @@ -6,7 +6,7 @@ import javax.servlet.http.{HttpServletRequest, HttpServletResponse} 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.SystemSettingsService.SystemSettings import gitbucket.core.util.ControlUtil._ @@ -30,8 +30,8 @@ "md" -> MarkdownRenderer, "markdown" -> MarkdownRenderer ) private val repositoryRoutings = new ListBuffer[GitRepositoryRouting] - private val commitHooks = new ListBuffer[CommitHook] - commitHooks += new ProtectedBranchCommitHook() + private val receiveHooks = new ListBuffer[ReceiveHook] + receiveHooks += new ProtectedBranchReceiveHook() def addPlugin(pluginInfo: PluginInfo): Unit = { plugins += pluginInfo @@ -101,11 +101,11 @@ } } - def addCommitHook(commitHook: CommitHook): Unit = { - commitHooks += commitHook + def addReceiveHook(commitHook: ReceiveHook): Unit = { + receiveHooks += commitHook } - def getCommitHooks: Seq[CommitHook] = commitHooks.toSeq + def getReceiveHooks: Seq[ReceiveHook] = receiveHooks.toSeq private case class GlobalAction( method: String, diff --git a/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala b/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala new file mode 100644 index 0000000..125f6a2 --- /dev/null +++ b/src/main/scala/gitbucket/core/plugin/ReceiveHook.scala @@ -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 = () + +} diff --git a/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala b/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala index e80347b..165910b 100644 --- a/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala +++ b/src/main/scala/gitbucket/core/service/ProtectedBranchService.scala @@ -2,7 +2,7 @@ import gitbucket.core.model._ import gitbucket.core.model.Profile._ -import gitbucket.core.plugin.CommitHook +import gitbucket.core.plugin.ReceiveHook import profile.simple._ import org.eclipse.jgit.transport.{ReceivePack, ReceiveCommand} @@ -45,9 +45,9 @@ object ProtectedBranchService { - class ProtectedBranchCommitHook extends CommitHook with ProtectedBranchService { - override def preCommit(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) - (implicit session: Session): Option[String] = { + class ProtectedBranchReceiveHook extends ReceiveHook with ProtectedBranchService { + override def preReceive(owner: String, repository: String, receivePack: ReceivePack, command: ReceiveCommand, pusher: String) + (implicit session: Session): Option[String] = { val branch = command.getRefName.stripPrefix("refs/heads/") if(branch != command.getRefName){ getProtectedBranchInfo(owner, repository, branch).getStopReason(receivePack.isAllowNonFastForwards, command, pusher) diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index fdab116..1f8347a 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -120,8 +120,8 @@ try { commands.asScala.foreach { command => // call pre-commit hook - PluginRegistry().getCommitHooks - .flatMap(_.preCommit(owner, repository, receivePack, command, pusher)) + PluginRegistry().getReceiveHooks + .flatMap(_.preReceive(owner, repository, receivePack, command, pusher)) .headOption.foreach { error => command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error) } @@ -217,7 +217,7 @@ } // 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. diff --git a/src/test/scala/gitbucket/core/service/ProtectedBranchServiceSpec.scala b/src/test/scala/gitbucket/core/service/ProtectedBranchServiceSpec.scala index 1cbdc20..aa343e4 100644 --- a/src/test/scala/gitbucket/core/service/ProtectedBranchServiceSpec.scala +++ b/src/test/scala/gitbucket/core/service/ProtectedBranchServiceSpec.scala @@ -4,11 +4,11 @@ import org.eclipse.jgit.transport.ReceiveCommand import org.eclipse.jgit.lib.ObjectId 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 { - val commitHook = new ProtectedBranchCommitHook() + val commitHook = new ProtectedBranchReceiveHook() val now = new java.util.Date() val sha = "0c77148632618b59b6f70004e3084002be2b8804" val sha2 = "0c77148632618b59b6f70004e3084002be2b8805"