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

View File

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

View File

@@ -69,13 +69,16 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
* @param handler * @param handler
* @param repositoryName * @param repositoryName
* @param startRev * @param startRev
* @param type
*/ */
public HgRepositoryHookEvent(HgRepositoryHandler handler, public HgRepositoryHookEvent(HgRepositoryHandler handler,
String repositoryName, String startRev) String repositoryName, String startRev,
RepositoryHookType type)
{ {
this.handler = handler; this.handler = handler;
this.repositoryName = repositoryName; this.repositoryName = repositoryName;
this.startRev = startRev; this.startRev = startRev;
this.type = type;
} }
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
@@ -93,7 +96,8 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
{ {
try try
{ {
changesets = createChangesetViewer().getChangesets(startRev, REV_TIP); changesets = createChangesetViewer().getChangesets(startRev, REV_TIP,
true);
} }
catch (IOException ex) catch (IOException ex)
{ {
@@ -113,7 +117,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
@Override @Override
public RepositoryHookType getType() public RepositoryHookType getType()
{ {
return RepositoryHookType.POST_RECEIVE; return type;
} }
//~--- methods -------------------------------------------------------------- //~--- methods --------------------------------------------------------------
@@ -146,4 +150,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent
/** Field description */ /** Field description */
private String startRev; 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.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.HgRepositoryHookEvent; import sonia.scm.repository.HgRepositoryHookEvent;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.util.HttpUtil; import sonia.scm.util.HttpUtil;
@@ -69,6 +70,12 @@ import javax.servlet.http.HttpServletResponse;
public class HgHookCallbackServlet extends HttpServlet 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 */ /** Field description */
private static final String PARAM_CHALLENGE = "challenge"; private static final String PARAM_CHALLENGE = "challenge";
@@ -164,25 +171,21 @@ public class HgHookCallbackServlet extends HttpServlet
* *
* @param response * @param response
* @param repositoryName * @param repositoryName
* @param type
* @param challenge
* @param node * @param node
* @param type
* *
* @throws IOException * @throws IOException
*/ */
private void hookCallback(HttpServletResponse response, private void fireHook(HttpServletResponse response, String repositoryName,
String repositoryName, String type, String node, RepositoryHookType type)
String challenge, String node)
throws IOException throws IOException
{
if (hookManager.isAcceptAble(challenge))
{ {
try try
{ {
repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME, repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME,
repositoryName, repositoryName,
new HgRepositoryHookEvent(handler, new HgRepositoryHookEvent(handler,
repositoryName, node)); repositoryName, node, type));
} }
catch (RepositoryNotFoundException ex) catch (RepositoryNotFoundException ex)
{ {
@@ -199,6 +202,51 @@ public class HgHookCallbackServlet extends HttpServlet
response.sendError(HttpServletResponse.SC_NOT_FOUND); 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 typeName,
String challenge, String node)
throws IOException
{
if (hookManager.isAcceptAble(challenge))
{
RepositoryHookType type = null;
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_BAD_REQUEST);
}
}
else else
{ {
if (logger.isWarnEnabled()) if (logger.isWarnEnabled())