mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 20:15:52 +01:00
implement new hook api for git repositories
This commit is contained in:
@@ -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<RepositoryManager> repositoryManagerProvider,
|
||||
Provider<HookEventFacade> hookEventFacadeProvider,
|
||||
Provider<GitRepositoryHandler> 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<GitRepositoryHandler> repositoryHandlerProvider;
|
||||
private Provider<HookEventFacade> hookEventFacadeProvider;
|
||||
|
||||
/** Field description */
|
||||
private Provider<RepositoryManager> repositoryManagerProvider;
|
||||
private Provider<GitRepositoryHandler> repositoryHandlerProvider;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<ReceiveCommand> 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<ReceiveCommand> receiveCommands;
|
||||
|
||||
/** Field description */
|
||||
private ReceivePack receivePack;
|
||||
|
||||
/** Field description */
|
||||
private HookChangesetResponse response;
|
||||
|
||||
/** Field description */
|
||||
private RepositoryHookType type;
|
||||
}
|
||||
@@ -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<ReceiveCommand> 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<ReceiveCommand> receiveCommands;
|
||||
|
||||
/** Field description */
|
||||
private ReceivePack receivePack;
|
||||
|
||||
/** Field description */
|
||||
private RepositoryHookType type;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<HttpServletRequest>
|
||||
implements ReceivePackFactory<HttpServletRequest>
|
||||
{
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user