implement new hook api for git repositories

This commit is contained in:
Sebastian Sdorra
2013-07-21 17:35:44 +02:00
parent 624c3ba44e
commit 6caa4e710b
7 changed files with 340 additions and 34 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);