implement new hook api for mercurial repositories

This commit is contained in:
Sebastian Sdorra
2013-07-22 11:40:03 +02:00
parent 42b9f41e6b
commit 86bd700bf6
8 changed files with 668 additions and 54 deletions

View File

@@ -36,18 +36,13 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.aragost.javahg.Repository;
import com.aragost.javahg.RepositoryConfiguration;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.HgConfig;
import sonia.scm.repository.HgEnvironment;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.spi.javahg.HgFileviewExtension;
import sonia.scm.web.HgUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -55,8 +50,6 @@ import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
/**
*
* @author Sebastian Sdorra
@@ -67,12 +60,6 @@ public class HgCommandContext implements Closeable
/** Field description */
private static final String PROPERTY_ENCODING = "hg.encoding";
/**
* the logger for HgCommandContext
*/
private static final Logger logger =
LoggerFactory.getLogger(HgCommandContext.class);
//~--- constructors ---------------------------------------------------------
/**
@@ -98,7 +85,6 @@ public class HgCommandContext implements Closeable
*
*
* @param hookManager
* @param config
* @param hanlder
* @param repository
* @param directory
@@ -147,33 +133,8 @@ public class HgCommandContext implements Closeable
{
if (repository == null)
{
RepositoryConfiguration repoConfiguration =
RepositoryConfiguration.DEFAULT;
HgEnvironment.prepareEnvironment(repoConfiguration.getEnvironment(),
hanlder, hookManager);
repoConfiguration.addExtension(HgFileviewExtension.class);
repoConfiguration.setEnablePendingChangesets(pending);
try
{
Charset charset = Charset.forName(encoding);
if (logger.isTraceEnabled())
{
logger.trace("set encoding {} for mercurial", encoding);
}
repoConfiguration.setEncoding(charset);
}
catch (IllegalArgumentException ex)
{
logger.error("could not set encoding for mercurial", ex);
}
repoConfiguration.setHgBin(hanlder.getConfig().getHgBinary());
repository = Repository.open(repoConfiguration, directory);
repository = HgUtil.open(hanlder, hookManager, directory, encoding,
pending);
}
return repository;

View File

@@ -0,0 +1,170 @@
/**
* 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 com.aragost.javahg.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.spi.javahg.HgLogChangesetCommand;
import sonia.scm.web.HgUtil;
import static sonia.scm.repository.HgRepositoryHookEvent.REV_TIP;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
/**
*
* @author Sebastian Sdorra
*/
public class HgHookChangesetProvider implements HookChangesetProvider
{
/**
* the logger for HgHookChangesetProvider
*/
private static final Logger logger =
LoggerFactory.getLogger(HgHookChangesetProvider.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param handler
* @param repositoryName
* @param hookManager
* @param startRev
* @param type
*/
public HgHookChangesetProvider(HgRepositoryHandler handler,
String repositoryName, HgHookManager hookManager, String startRev,
RepositoryHookType type)
{
this.handler = handler;
this.repositoryName = repositoryName;
this.hookManager = hookManager;
this.startRev = startRev;
this.type = type;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
@Override
public HookChangesetResponse handleRequest(HookChangesetRequest request)
{
if (response == null)
{
Repository repository = null;
try
{
repository = open();
HgLogChangesetCommand cmd = HgLogChangesetCommand.on(repository,
handler.getConfig());
response = new HookChangesetResponse(
cmd.rev(startRev.concat(":").concat(REV_TIP)).execute());
}
catch (Exception ex)
{
logger.error("could not retrieve changesets", ex);
}
finally
{
if (repository != null)
{
repository.close();
}
}
}
return response;
}
/**
* Method description
*
*
* @return
*/
private Repository open()
{
File directory = handler.getConfig().getRepositoryDirectory();
File repositoryDirectory = new File(directory, repositoryName);
// use HG_PENDING only for pre receive hooks
boolean pending = type == RepositoryHookType.PRE_RECEIVE;
// TODO get repository encoding
return HgUtil.open(handler, hookManager, repositoryDirectory, null,
pending);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private HgRepositoryHandler handler;
/** Field description */
private HgHookManager hookManager;
/** Field description */
private String repositoryName;
/** Field description */
private HookChangesetResponse response;
/** Field description */
private String startRev;
/** Field description */
private RepositoryHookType type;
}

View File

@@ -0,0 +1,158 @@
/**
* 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 sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.api.HookFeature;
import sonia.scm.repository.api.HookMessageProvider;
import sonia.scm.repositorya.api.HgHookMessageProvider;
//~--- JDK imports ------------------------------------------------------------
import java.util.EnumSet;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class HgHookContextProvider extends HookContextProvider
{
/** Field description */
private static final Set<HookFeature> SUPPORTED_FEATURES =
EnumSet.of(HookFeature.CHANGESET_PROVIDER, HookFeature.MESSAGE_PROVIDER);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param handler
* @param repositoryName
* @param hookManager
* @param startRev
* @param type
*/
public HgHookContextProvider(HgRepositoryHandler handler,
String repositoryName, HgHookManager hookManager, String startRev,
RepositoryHookType type)
{
this.handler = handler;
this.repositoryName = repositoryName;
this.hookManager = hookManager;
this.startRev = startRev;
this.type = type;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public HookChangesetProvider getChangesetProvider()
{
return new HgHookChangesetProvider(handler, repositoryName, hookManager,
startRev, type);
}
/**
* Method description
*
*
* @return
*/
public HgHookMessageProvider getHgMessageProvider()
{
if (hgMessageProvider == null)
{
hgMessageProvider = new HgHookMessageProvider();
}
return hgMessageProvider;
}
/**
* Method description
*
*
* @return
*/
@Override
public Set<HookFeature> getSupportedFeatures()
{
return SUPPORTED_FEATURES;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected HookMessageProvider createMessageProvider()
{
return getHgMessageProvider();
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private HgRepositoryHandler handler;
/** Field description */
private HgHookMessageProvider hgMessageProvider;
/** Field description */
private HgHookManager hookManager;
/** Field description */
private String repositoryName;
/** Field description */
private String startRev;
/** Field description */
private RepositoryHookType type;
}

View File

@@ -0,0 +1,102 @@
/**
* 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.repositorya.api;
//~--- JDK imports ------------------------------------------------------------
import java.io.Serializable;
/**
*
* @author Sebastian Sdorra
*/
public final class HgHookMessage implements Serializable
{
/** Field description */
private static final long serialVersionUID = 1804492842452344326L;
//~--- constant enums -------------------------------------------------------
/**
* Enum description
*
*/
public static enum Severity { NOTE, ERROR; }
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param severity
* @param message
*/
public HgHookMessage(Severity severity, String message)
{
this.severity = severity;
this.message = message;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getMessage()
{
return message;
}
/**
* Method description
*
*
* @return
*/
public Severity getSeverity()
{
return severity;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String message;
/** Field description */
private Severity severity;
}

View File

@@ -0,0 +1,98 @@
/**
* 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.repositorya.api;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.Lists;
import sonia.scm.repository.api.HookMessageProvider;
import sonia.scm.repositorya.api.HgHookMessage.Severity;
//~--- JDK imports ------------------------------------------------------------
import java.util.List;
/**
*
* @author Sebastian Sdorra
*/
public class HgHookMessageProvider implements HookMessageProvider
{
/**
* Method description
*
*
* @param message
*/
@Override
public void sendError(String message)
{
getMessages().add(new HgHookMessage(Severity.ERROR, message));
}
/**
* Method description
*
*
* @param message
*/
@Override
public void sendMessage(String message)
{
getMessages().add(new HgHookMessage(Severity.NOTE, message));
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public List<HgHookMessage> getMessages()
{
if (messages == null)
{
messages = Lists.newArrayList();
}
return messages;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private List<HgHookMessage> messages;
}

View File

@@ -49,11 +49,13 @@ import org.slf4j.LoggerFactory;
import sonia.scm.repository.HgContext;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.HgRepositoryHookEvent;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryUtil;
import sonia.scm.repository.spi.HgHookContextProvider;
import sonia.scm.repository.spi.HookEventFacade;
import sonia.scm.repositorya.api.HgHookMessage;
import sonia.scm.repositorya.api.HgHookMessage.Severity;
import sonia.scm.security.CipherUtil;
import sonia.scm.security.Tokens;
import sonia.scm.util.HttpUtil;
@@ -64,6 +66,7 @@ import sonia.scm.util.Util;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -118,18 +121,18 @@ public class HgHookCallbackServlet extends HttpServlet
* Constructs ...
*
*
* @param repositoryManager
*
* @param hookEventFacade
* @param handler
* @param hookManager
* @param contextProvider
* @param securityContextProvider
*/
@Inject
public HgHookCallbackServlet(RepositoryManager repositoryManager,
public HgHookCallbackServlet(HookEventFacade hookEventFacade,
HgRepositoryHandler handler, HgHookManager hookManager,
Provider<HgContext> contextProvider)
{
this.repositoryManager = repositoryManager;
this.hookEventFacade = hookEventFacade;
this.handler = handler;
this.hookManager = hookManager;
this.contextProvider = contextProvider;
@@ -228,7 +231,6 @@ public class HgHookCallbackServlet extends HttpServlet
*
*
* @param request
* @param response
* @param credentials
*/
private void authenticate(HttpServletRequest request, String credentials)
@@ -278,9 +280,14 @@ public class HgHookCallbackServlet extends HttpServlet
contextProvider.get().setPending(true);
}
repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME,
repositoryName,
new HgRepositoryHookEvent(handler, hookManager, repositoryName, node, type));
HgHookContextProvider context = new HgHookContextProvider(handler,
repositoryName, hookManager, node,
type);
hookEventFacade.handle(HgRepositoryHandler.TYPE_NAME,
repositoryName).fireHookEvent(type, context);
printMessages(response, context);
}
catch (RepositoryNotFoundException ex)
{
@@ -356,6 +363,52 @@ public class HgHookCallbackServlet extends HttpServlet
}
}
/**
* Method description
*
*
* @param resonse
* @param context
*
* @throws IOException
*/
private void printMessages(HttpServletResponse resonse,
HgHookContextProvider context)
throws IOException
{
List<HgHookMessage> msgs = context.getHgMessageProvider().getMessages();
if (Util.isNotEmpty(msgs))
{
PrintWriter writer = null;
try
{
writer = resonse.getWriter();
for (HgHookMessage msg : msgs)
{
writer.append('_');
if (msg.getSeverity() == Severity.ERROR)
{
writer.append("e[SCM] Error: ");
}
else
{
writer.append("n[SCM] ");
}
writer.println(msg.getMessage());
}
}
finally
{
Closeables.close(writer, false);
}
}
}
/**
* Method description
*
@@ -437,8 +490,8 @@ public class HgHookCallbackServlet extends HttpServlet
private HgRepositoryHandler handler;
/** Field description */
private HgHookManager hookManager;
private HookEventFacade hookEventFacade;
/** Field description */
private RepositoryManager repositoryManager;
private HgHookManager hookManager;
}

View File

@@ -35,15 +35,29 @@ package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import com.aragost.javahg.Repository;
import com.aragost.javahg.RepositoryConfiguration;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContext;
import sonia.scm.repository.HgConfig;
import sonia.scm.repository.HgEnvironment;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgPythonScript;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.spi.javahg.HgFileviewExtension;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.nio.charset.Charset;
/**
*
* @author Sebastian Sdorra
@@ -54,6 +68,11 @@ public final class HgUtil
/** Field description */
public static final String REVISION_TIP = "tip";
/**
* the logger for HgUtil
*/
private static final Logger logger = LoggerFactory.getLogger(HgUtil.class);
//~--- constructors ---------------------------------------------------------
/**
@@ -62,6 +81,56 @@ public final class HgUtil
*/
private HgUtil() {}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param handler
* @param hookManager
* @param directory
* @param encoding
* @param pending
*
* @return
*/
public static Repository open(HgRepositoryHandler handler,
HgHookManager hookManager, File directory, String encoding, boolean pending)
{
String enc = encoding;
if (Strings.isNullOrEmpty(enc))
{
enc = handler.getConfig().getEncoding();
}
RepositoryConfiguration repoConfiguration = RepositoryConfiguration.DEFAULT;
HgEnvironment.prepareEnvironment(repoConfiguration.getEnvironment(),
handler, hookManager);
repoConfiguration.addExtension(HgFileviewExtension.class);
repoConfiguration.setEnablePendingChangesets(pending);
try
{
Charset charset = Charset.forName(enc);
logger.trace("set encoding {} for mercurial", enc);
repoConfiguration.setEncoding(charset);
}
catch (IllegalArgumentException ex)
{
logger.error("could not set encoding for mercurial", ex);
}
repoConfiguration.setHgBin(handler.getConfig().getHgBinary());
return Repository.open(repoConfiguration, directory);
}
//~--- get methods ----------------------------------------------------------
/**

View File

@@ -55,6 +55,9 @@ def callHookUrl(ui, repo, hooktype, node):
conn = opener.open(req)
if conn.code >= 200 and conn.code < 300:
ui.debug( "scm-hook " + hooktype + " success with status code " + str(conn.code) + "\n" )
for line in conn:
if line.startswith("_e") or line.startswith("_n"):
ui.warn(line[2:]);
abort = False
else:
ui.warn( "ERROR: scm-hook failed with error code " + str(conn.code) + "\n" )