mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
improve handling of mercurial hooks
This commit is contained in:
@@ -38,26 +38,11 @@ package sonia.scm.repository;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import sonia.scm.ConfigChangedListener;
|
|
||||||
import sonia.scm.SCMContextProvider;
|
|
||||||
import sonia.scm.config.ScmConfiguration;
|
import sonia.scm.config.ScmConfiguration;
|
||||||
import sonia.scm.io.RegexResourceProcessor;
|
|
||||||
import sonia.scm.io.ResourceProcessor;
|
|
||||||
import sonia.scm.util.HttpUtil;
|
import sonia.scm.util.HttpUtil;
|
||||||
import sonia.scm.util.IOUtil;
|
|
||||||
import sonia.scm.web.HgWebConfigWriter;
|
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -67,32 +52,19 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class HgHookManager implements ConfigChangedListener<ScmConfiguration>
|
public class HgHookManager
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
public static final String SCRIPT_TEMPLATE = "/sonia/scm/hghook.py";
|
|
||||||
|
|
||||||
/** the logger for HgHookManager */
|
|
||||||
private static final Logger logger =
|
|
||||||
LoggerFactory.getLogger(HgHookManager.class);
|
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs ...
|
* Constructs ...
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param context
|
|
||||||
* @param configuration
|
* @param configuration
|
||||||
*/
|
*/
|
||||||
@Inject
|
@Inject
|
||||||
public HgHookManager(SCMContextProvider context,
|
public HgHookManager(ScmConfiguration configuration)
|
||||||
ScmConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
this.context = context;
|
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
this.configuration.addListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -101,51 +73,29 @@ public class HgHookManager implements ConfigChangedListener<ScmConfiguration>
|
|||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param config
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void configChanged(ScmConfiguration config)
|
|
||||||
{
|
|
||||||
this.changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO check if file exists
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param request
|
* @param request
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @return
|
||||||
*/
|
*/
|
||||||
public void writeHookScript(HttpServletRequest request) throws IOException
|
public String createUrl(HttpServletRequest request)
|
||||||
{
|
{
|
||||||
if (isScriptWriteAble())
|
String url = null;
|
||||||
{
|
|
||||||
synchronized (this)
|
|
||||||
{
|
|
||||||
if (isScriptWriteAble())
|
|
||||||
{
|
|
||||||
String url = createUrl(request);
|
|
||||||
|
|
||||||
if (hgHookScript == null)
|
if (configuration.isForceBaseUrl())
|
||||||
{
|
{
|
||||||
File cgiDirectory = new File(context.getBaseDirectory(), "cgi-bin");
|
url = HttpUtil.getUriWithoutEndSeperator(
|
||||||
|
configuration.getBaseUrl()).concat("/hook/hg/");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder(request.getScheme());
|
||||||
|
|
||||||
IOUtil.mkdirs(cgiDirectory);
|
sb.append("://localhost:").append(request.getLocalPort());
|
||||||
hgHookScript = new File(cgiDirectory, "scmhooks.py");
|
sb.append(request.getContextPath()).append("/hook/hg/");
|
||||||
|
url = sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled())
|
return url;
|
||||||
{
|
|
||||||
logger.debug("write hg hook script for '{}' to '{}'", url,
|
|
||||||
hgHookScript);
|
|
||||||
}
|
|
||||||
|
|
||||||
writeScript(hgHookScript, url.toString());
|
|
||||||
changed = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
@@ -174,98 +124,11 @@ public class HgHookManager implements ConfigChangedListener<ScmConfiguration>
|
|||||||
return this.challenge.equals(challenge);
|
return this.challenge.equals(challenge);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private String createUrl(HttpServletRequest request)
|
|
||||||
{
|
|
||||||
String url = null;
|
|
||||||
|
|
||||||
if (configuration.isForceBaseUrl())
|
|
||||||
{
|
|
||||||
url = HttpUtil.getUriWithoutEndSeperator(
|
|
||||||
configuration.getBaseUrl()).concat("/hook/hg/");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
StringBuilder sb = new StringBuilder(request.getScheme());
|
|
||||||
|
|
||||||
sb.append("://localhost:").append(request.getLocalPort());
|
|
||||||
sb.append(request.getContextPath()).append("/hook/hg/");
|
|
||||||
url = sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param script
|
|
||||||
* @param url
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
private void writeScript(File script, String url) throws IOException
|
|
||||||
{
|
|
||||||
InputStream input = null;
|
|
||||||
OutputStream output = null;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
input = HgWebConfigWriter.class.getResourceAsStream(SCRIPT_TEMPLATE);
|
|
||||||
output = new FileOutputStream(script);
|
|
||||||
|
|
||||||
ResourceProcessor rp = new RegexResourceProcessor();
|
|
||||||
|
|
||||||
rp.addVariable("url", url);
|
|
||||||
rp.addVariable("challenge", getChallenge());
|
|
||||||
rp.process(input, output);
|
|
||||||
script.setExecutable(true);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
IOUtil.close(input);
|
|
||||||
IOUtil.close(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean isScriptWriteAble()
|
|
||||||
{
|
|
||||||
return (hgHookScript == null) ||!hgHookScript.exists() || changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private boolean changed = false;
|
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private String challenge = UUID.randomUUID().toString();
|
private String challenge = UUID.randomUUID().toString();
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private ScmConfiguration configuration;
|
private ScmConfiguration configuration;
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private SCMContextProvider context;
|
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private volatile File hgHookScript;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ import javax.servlet.http.HttpSession;
|
|||||||
public class HgCGIServlet extends HttpServlet
|
public class HgCGIServlet extends HttpServlet
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String ENV_CHALLENGE = "SCM_CHALLENGE";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String ENV_PYTHON_PATH = "PYTHONPATH";
|
public static final String ENV_PYTHON_PATH = "PYTHONPATH";
|
||||||
|
|
||||||
@@ -89,6 +92,9 @@ public class HgCGIServlet extends HttpServlet
|
|||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String ENV_SESSION_PREFIX = "SCM_";
|
public static final String ENV_SESSION_PREFIX = "SCM_";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String ENV_URL = "SCM_URL";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final long serialVersionUID = -3492811300905099810L;
|
private static final long serialVersionUID = -3492811300905099810L;
|
||||||
|
|
||||||
@@ -225,8 +231,6 @@ public class HgCGIServlet extends HttpServlet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hookManager.writeHookScript(request);
|
|
||||||
|
|
||||||
CGIExecutor executor = cgiExecutorFactory.createExecutor(configuration,
|
CGIExecutor executor = cgiExecutorFactory.createExecutor(configuration,
|
||||||
getServletContext(), request, response);
|
getServletContext(), request, response);
|
||||||
|
|
||||||
@@ -234,6 +238,8 @@ public class HgCGIServlet extends HttpServlet
|
|||||||
executor.getEnvironment().set(ENV_REPOSITORY_NAME, name);
|
executor.getEnvironment().set(ENV_REPOSITORY_NAME, name);
|
||||||
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
|
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
|
||||||
directory.getAbsolutePath());
|
directory.getAbsolutePath());
|
||||||
|
executor.getEnvironment().set(ENV_URL, hookManager.createUrl(request));
|
||||||
|
executor.getEnvironment().set(ENV_CHALLENGE, hookManager.getChallenge());
|
||||||
executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath);
|
executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath);
|
||||||
|
|
||||||
HttpSession session = request.getSession();
|
HttpSession session = request.getSession();
|
||||||
|
|||||||
Reference in New Issue
Block a user