implement mercurial pre-receive hook

This commit is contained in:
Sebastian Sdorra
2011-09-28 09:42:16 +02:00
parent 8cf66a7f15
commit b07810c2d1
4 changed files with 150 additions and 26 deletions

View File

@@ -47,6 +47,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
/**
*
* @author Sebastian Sdorra
@@ -68,6 +70,20 @@ public class SimpleCommand implements Command
*/
public SimpleCommand(String... command)
{
this(null, command);
}
/**
* Constructs ...
*
*
* @param environment
* @param command
* @since 1.8
*/
public SimpleCommand(Map<String, String> environment, String... command)
{
this.environment = environment;
this.command = command;
}
@@ -91,6 +107,18 @@ public class SimpleCommand implements Command
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param environment
* @since 1.8
*/
public void setEnvironment(Map<String, String> environment)
{
this.environment = environment;
}
/**
* Method description
*
@@ -134,6 +162,11 @@ public class SimpleCommand implements Command
processBuilder = processBuilder.directory(workDirectory);
}
if (environment != null)
{
processBuilder.environment().putAll(environment);
}
return processBuilder.redirectErrorStream(true).start();
}
@@ -208,6 +241,9 @@ public class SimpleCommand implements Command
/** Field description */
private String[] command;
/** Field description */
private Map<String, String> environment;
/** Field description */
private File workDirectory;
}

View File

@@ -52,7 +52,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
@@ -82,6 +84,9 @@ public class HgChangesetViewer implements ChangesetViewer
+ "</changeset>\"";
//J+
/** Field description */
public static final String ENV_PENDING = "HG_PENDING";
/** Field description */
public static final String TEMPLATE_TOTAL = "{rev}";
@@ -179,12 +184,14 @@ public class HgChangesetViewer implements ChangesetViewer
*
* @param startRev
* @param endRev
* @param pending
*
* @return
*
* @throws IOException
*/
public List<Changeset> getChangesets(String startRev, String endRev)
public List<Changeset> getChangesets(String startRev, String endRev,
boolean pending)
throws IOException
{
List<Changeset> changesetList = null;
@@ -192,10 +199,19 @@ public class HgChangesetViewer implements ChangesetViewer
try
{
Command command = new SimpleCommand(handler.getConfig().getHgBinary(),
"-R", repositoryPath, "log", "-r",
startRev + ":" + endRev, "--template",
TEMPLATE_CHANGESETS);
SimpleCommand command =
new SimpleCommand(handler.getConfig().getHgBinary(), "-R",
repositoryPath, "log", "-r", startRev + ":" + endRev,
"--template", TEMPLATE_CHANGESETS);
if (pending)
{
Map<String, String> env = new HashMap<String, String>();
env.put(ENV_PENDING, repositoryPath);
command.setEnvironment(env);
}
CommandResult result = command.execute();
if (result.isSuccessfull())
@@ -229,6 +245,23 @@ public class HgChangesetViewer implements ChangesetViewer
return changesetList;
}
/**
* Method description
*
*
* @param startRev
* @param endRev
*
* @return
*
* @throws IOException
*/
public List<Changeset> getChangesets(String startRev, String endRev)
throws IOException
{
return getChangesets(startRev, endRev, false);
}
/**
* Method description
*

View File

@@ -69,13 +69,16 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
* @param handler
* @param repositoryName
* @param startRev
* @param type
*/
public HgRepositoryHookEvent(HgRepositoryHandler handler,
String repositoryName, String startRev)
String repositoryName, String startRev,
RepositoryHookType type)
{
this.handler = handler;
this.repositoryName = repositoryName;
this.startRev = startRev;
this.type = type;
}
//~--- get methods ----------------------------------------------------------
@@ -93,7 +96,8 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
{
try
{
changesets = createChangesetViewer().getChangesets(startRev, REV_TIP);
changesets = createChangesetViewer().getChangesets(startRev, REV_TIP,
true);
}
catch (IOException ex)
{
@@ -113,7 +117,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
@Override
public RepositoryHookType getType()
{
return RepositoryHookType.POST_RECEIVE;
return type;
}
//~--- methods --------------------------------------------------------------
@@ -146,4 +150,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
/** Field description */
private String startRev;
/** Field description */
private RepositoryHookType type;
}

View File

@@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory;
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.util.HttpUtil;
@@ -69,6 +70,12 @@ import javax.servlet.http.HttpServletResponse;
public class HgHookCallbackServlet extends HttpServlet
{
/** Field description */
public static final String HGHOOK_POST_RECEIVE = "changegroup";
/** Field description */
public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup";
/** Field description */
private static final String PARAM_CHALLENGE = "challenge";
@@ -164,39 +171,80 @@ public class HgHookCallbackServlet extends HttpServlet
*
* @param response
* @param repositoryName
* @param node
* @param type
*
* @throws IOException
*/
private void fireHook(HttpServletResponse response, String repositoryName,
String node, RepositoryHookType type)
throws IOException
{
try
{
repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME,
repositoryName,
new HgRepositoryHookEvent(handler,
repositoryName, node, type));
}
catch (RepositoryNotFoundException ex)
{
if (logger.isErrorEnabled())
{
logger.error("could not find repository {}", repositoryName);
if (logger.isTraceEnabled())
{
logger.trace("repository not found", ex);
}
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
/**
* Method description
*
*
* @param response
* @param repositoryName
* @param typeName
* @param challenge
* @param node
*
* @throws IOException
*/
private void hookCallback(HttpServletResponse response,
String repositoryName, String type,
String repositoryName, String typeName,
String challenge, String node)
throws IOException
{
if (hookManager.isAcceptAble(challenge))
{
try
{
repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME,
repositoryName,
new HgRepositoryHookEvent(handler,
repositoryName, node));
}
catch (RepositoryNotFoundException ex)
{
if (logger.isErrorEnabled())
{
logger.error("could not find repository {}", repositoryName);
RepositoryHookType type = null;
if (logger.isTraceEnabled())
{
logger.trace("repository not found", ex);
}
if (HGHOOK_PRE_RECEIVE.equals(typeName))
{
type = RepositoryHookType.PRE_RECEIVE;
}
else if (HGHOOK_POST_RECEIVE.equals(typeName))
{
type = RepositoryHookType.POST_RECEIVE;
}
if (type != null)
{
fireHook(response, repositoryName, node, type);
}
else
{
if (logger.isWarnEnabled())
{
logger.warn("unknown hook type {}", typeName);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
else