diff --git a/scm-plugins/scm-git-plugin/src/main/java/org/eclipse/jgit/transport/ScmTransportProtocol.java b/scm-plugins/scm-git-plugin/src/main/java/org/eclipse/jgit/transport/ScmTransportProtocol.java index 83e411d8db..88c988537d 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/org/eclipse/jgit/transport/ScmTransportProtocol.java +++ b/scm-plugins/scm-git-plugin/src/main/java/org/eclipse/jgit/transport/ScmTransportProtocol.java @@ -30,6 +30,7 @@ */ + package org.eclipse.jgit.transport; //~--- non-JDK imports -------------------------------------------------------- @@ -46,7 +47,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; import sonia.scm.repository.GitRepositoryHandler; -import sonia.scm.repository.RepositoryManager; +import sonia.scm.repository.spi.HookEventFacade; import sonia.scm.web.GitReceiveHook; //~--- JDK imports ------------------------------------------------------------ @@ -80,18 +81,17 @@ public class ScmTransportProtocol extends TransportProtocol * Constructs ... * * - * @param repositoryManager - * @param handler * - * @param repositoryManagerProvider + * @param hookEventFacadeProvider + * * @param repositoryHandlerProvider */ @Inject public ScmTransportProtocol( - Provider repositoryManagerProvider, + Provider hookEventFacadeProvider, Provider repositoryHandlerProvider) { - this.repositoryManagerProvider = repositoryManagerProvider; + this.hookEventFacadeProvider = hookEventFacadeProvider; this.repositoryHandlerProvider = repositoryHandlerProvider; } @@ -149,7 +149,7 @@ public class ScmTransportProtocol extends TransportProtocol //J- return new TransportLocalWithHooks( - repositoryManagerProvider.get(), + hookEventFacadeProvider.get(), repositoryHandlerProvider.get(), local, uri, gitDir ); @@ -198,17 +198,18 @@ public class ScmTransportProtocol extends TransportProtocol * Constructs ... * * - * @param repositoryManager + * + * @param hookEventFacade * @param handler * @param local * @param uri * @param gitDir */ - public TransportLocalWithHooks(RepositoryManager repositoryManager, + public TransportLocalWithHooks(HookEventFacade hookEventFacade, GitRepositoryHandler handler, Repository local, URIish uri, File gitDir) { super(local, uri, gitDir); - this.repositoryManager = repositoryManager; + this.hookEventFacade = hookEventFacade; this.handler = handler; } @@ -227,9 +228,9 @@ public class ScmTransportProtocol extends TransportProtocol { ReceivePack pack = new ReceivePack(dst); - if ((repositoryManager != null) && (handler != null)) + if ((hookEventFacade != null) && (handler != null)) { - GitReceiveHook hook = new GitReceiveHook(repositoryManager, handler); + GitReceiveHook hook = new GitReceiveHook(hookEventFacade, handler); pack.setPreReceiveHook(hook); pack.setPostReceiveHook(hook); @@ -244,15 +245,15 @@ public class ScmTransportProtocol extends TransportProtocol private GitRepositoryHandler handler; /** Field description */ - private RepositoryManager repositoryManager; + private HookEventFacade hookEventFacade; } //~--- fields --------------------------------------------------------------- /** Field description */ - private Provider repositoryHandlerProvider; + private Provider hookEventFacadeProvider; /** Field description */ - private Provider repositoryManagerProvider; + private Provider repositoryHandlerProvider; } diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/api/GitHookMessageProvider.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/api/GitHookMessageProvider.java new file mode 100644 index 0000000000..f6e9b0b5b1 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/api/GitHookMessageProvider.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.api; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.eclipse.jgit.transport.ReceivePack; + +import sonia.scm.web.GitHooks; + +/** + * + * @author Sebastian Sdorra + */ +public final class GitHookMessageProvider implements HookMessageProvider +{ + + /** + * Constructs ... + * + * + * @param receivePack + */ + public GitHookMessageProvider(ReceivePack receivePack) + { + this.receivePack = receivePack; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param message + */ + @Override + public void sendError(String message) + { + GitHooks.sendPrefixedError(receivePack, message); + } + + /** + * Method description + * + * + * @param message + */ + @Override + public void sendMessage(String message) + { + GitHooks.sendPrefixedMessage(receivePack, message); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private ReceivePack receivePack; +} diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookChangesetProvider.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookChangesetProvider.java new file mode 100644 index 0000000000..2a8748e7c0 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookChangesetProvider.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.spi; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.eclipse.jgit.transport.ReceiveCommand; +import org.eclipse.jgit.transport.ReceivePack; + +import sonia.scm.repository.GitHookChangesetCollector; +import sonia.scm.repository.RepositoryHookType; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class GitHookChangesetProvider implements HookChangesetProvider +{ + + /** + * Constructs ... + * + * + * @param receivePack + * @param receiveCommands + * @param type + */ + public GitHookChangesetProvider(ReceivePack receivePack, + List receiveCommands, RepositoryHookType type) + { + this.receivePack = receivePack; + this.receiveCommands = receiveCommands; + this.type = type; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param request + * + * @return + */ + @Override + public HookChangesetResponse handleRequest(HookChangesetRequest request) + { + if (response == null) + { + GitHookChangesetCollector collector = + new GitHookChangesetCollector(receivePack, receiveCommands); + + response = new HookChangesetResponse(collector.collectChangesets()); + } + + return response; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private List receiveCommands; + + /** Field description */ + private ReceivePack receivePack; + + /** Field description */ + private HookChangesetResponse response; + + /** Field description */ + private RepositoryHookType type; +} diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookContextProvider.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookContextProvider.java new file mode 100644 index 0000000000..cfb9c00d68 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHookContextProvider.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository.spi; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.eclipse.jgit.transport.ReceiveCommand; +import org.eclipse.jgit.transport.ReceivePack; + +import sonia.scm.repository.RepositoryHookType; +import sonia.scm.repository.api.GitHookMessageProvider; +import sonia.scm.repository.api.HookMessageProvider; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.List; + +/** + * + * @author Sebastian Sdorra + */ +public class GitHookContextProvider extends HookContextProvider +{ + + /** + * Constructs ... + * + * + * @param receivePack + * @param receiveCommands + * @param type + */ + public GitHookContextProvider(ReceivePack receivePack, + List receiveCommands, RepositoryHookType type) + { + this.receivePack = receivePack; + this.receiveCommands = receiveCommands; + this.type = type; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public HookChangesetProvider getChangesetProvider() + { + return new GitHookChangesetProvider(receivePack, receiveCommands, type); + } + + /** + * Method description + * + * + * @return + */ + @Override + public HookMessageProvider createMessageProvider() + { + return new GitHookMessageProvider(receivePack); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private List receiveCommands; + + /** Field description */ + private ReceivePack receivePack; + + /** Field description */ + private RepositoryHookType type; +} diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java index 2bcda87d04..ea74f76ab2 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java @@ -45,10 +45,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.repository.GitRepositoryHandler; -import sonia.scm.repository.GitRepositoryHookEvent; import sonia.scm.repository.RepositoryHookType; -import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryUtil; +import sonia.scm.repository.spi.GitHookContextProvider; +import sonia.scm.repository.spi.HookEventFacade; //~--- JDK imports ------------------------------------------------------------ @@ -75,13 +75,14 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook * Constructs ... * * - * @param repositoryManager + * + * @param hookEventFacade * @param handler */ - public GitReceiveHook(RepositoryManager repositoryManager, + public GitReceiveHook(HookEventFacade hookEventFacade, GitRepositoryHandler handler) { - this.repositoryManager = repositoryManager; + this.hookEventFacade = hookEventFacade; this.handler = handler; } @@ -134,9 +135,12 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook logger.trace("resolved repository name to {}", repositoryName); - repositoryManager.fireHookEvent(GitRepositoryHandler.TYPE_NAME, - repositoryName, - new GitRepositoryHookEvent(rpack, receiveCommands, type)); + GitHookContextProvider context = new GitHookContextProvider(rpack, + receiveCommands, type); + + hookEventFacade.handle(GitRepositoryHandler.TYPE_NAME, + repositoryName).fireHookEvent(type, context); + } catch (Exception ex) { @@ -209,5 +213,5 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook private GitRepositoryHandler handler; /** Field description */ - private RepositoryManager repositoryManager; + private HookEventFacade hookEventFacade; } diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceivePackFactory.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceivePackFactory.java index d046a4c5d7..535f0a2714 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceivePackFactory.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceivePackFactory.java @@ -45,7 +45,7 @@ import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import sonia.scm.repository.GitRepositoryHandler; -import sonia.scm.repository.RepositoryManager; +import sonia.scm.repository.spi.HookEventFacade; //~--- JDK imports ------------------------------------------------------------ @@ -56,21 +56,22 @@ import javax.servlet.http.HttpServletRequest; * @author Sebastian Sdorra */ public class GitReceivePackFactory - implements ReceivePackFactory + implements ReceivePackFactory { /** * Constructs ... * * - * @param repositoryManager + * + * @param hookEventFacade * @param handler */ @Inject - public GitReceivePackFactory(RepositoryManager repositoryManager, - GitRepositoryHandler handler) + public GitReceivePackFactory(HookEventFacade hookEventFacade, + GitRepositoryHandler handler) { - hook = new GitReceiveHook(repositoryManager, handler); + hook = new GitReceiveHook(hookEventFacade, handler); } //~--- methods -------------------------------------------------------------- @@ -89,7 +90,7 @@ public class GitReceivePackFactory */ @Override public ReceivePack create(HttpServletRequest request, Repository repository) - throws ServiceNotEnabledException, ServiceNotAuthorizedException + throws ServiceNotEnabledException, ServiceNotAuthorizedException { ReceivePack rpack = defaultFactory.create(request, repository); diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java index e2c81f8204..f9d38f2dac 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitPushCommandTest.java @@ -30,6 +30,7 @@ */ + package sonia.scm.repository.spi; //~--- non-JDK imports -------------------------------------------------------- @@ -46,7 +47,6 @@ import org.junit.Test; import sonia.scm.repository.GitRepositoryHandler; import sonia.scm.repository.RepositoryException; -import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.api.PushResponse; import static org.junit.Assert.*; @@ -111,11 +111,11 @@ public class GitPushCommandTest extends AbstractRemoteCommandTestBase { // store reference to handle weak references - proto = new ScmTransportProtocol(new Provider() + proto = new ScmTransportProtocol(new Provider() { @Override - public RepositoryManager get() + public HookEventFacade get() { return null; }