mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +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.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.io.RegexResourceProcessor;
|
||||
import sonia.scm.io.ResourceProcessor;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.web.HgWebConfigWriter;
|
||||
|
||||
//~--- 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 javax.servlet.http.HttpServletRequest;
|
||||
@@ -67,32 +52,19 @@ import javax.servlet.http.HttpServletRequest;
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@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 ...
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param configuration
|
||||
*/
|
||||
@Inject
|
||||
public HgHookManager(SCMContextProvider context,
|
||||
ScmConfiguration configuration)
|
||||
public HgHookManager(ScmConfiguration configuration)
|
||||
{
|
||||
this.context = context;
|
||||
this.configuration = configuration;
|
||||
this.configuration.addListener(this);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -101,51 +73,29 @@ public class HgHookManager implements ConfigChangedListener<ScmConfiguration>
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param config
|
||||
*/
|
||||
@Override
|
||||
public void configChanged(ScmConfiguration config)
|
||||
{
|
||||
this.changed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO check if file exists
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
*
|
||||
* @throws IOException
|
||||
* @return
|
||||
*/
|
||||
public void writeHookScript(HttpServletRequest request) throws IOException
|
||||
public String createUrl(HttpServletRequest request)
|
||||
{
|
||||
if (isScriptWriteAble())
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
if (isScriptWriteAble())
|
||||
{
|
||||
String url = createUrl(request);
|
||||
String url = null;
|
||||
|
||||
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);
|
||||
hgHookScript = new File(cgiDirectory, "scmhooks.py");
|
||||
sb.append("://localhost:").append(request.getLocalPort());
|
||||
sb.append(request.getContextPath()).append("/hook/hg/");
|
||||
url = sb.toString();
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("write hg hook script for '{}' to '{}'", url,
|
||||
hgHookScript);
|
||||
}
|
||||
|
||||
writeScript(hgHookScript, url.toString());
|
||||
changed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -174,98 +124,11 @@ public class HgHookManager implements ConfigChangedListener<ScmConfiguration>
|
||||
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 ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private boolean changed = false;
|
||||
|
||||
/** Field description */
|
||||
private String challenge = UUID.randomUUID().toString();
|
||||
|
||||
/** Field description */
|
||||
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
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String ENV_CHALLENGE = "SCM_CHALLENGE";
|
||||
|
||||
/** Field description */
|
||||
public static final String ENV_PYTHON_PATH = "PYTHONPATH";
|
||||
|
||||
@@ -89,6 +92,9 @@ public class HgCGIServlet extends HttpServlet
|
||||
/** Field description */
|
||||
public static final String ENV_SESSION_PREFIX = "SCM_";
|
||||
|
||||
/** Field description */
|
||||
public static final String ENV_URL = "SCM_URL";
|
||||
|
||||
/** Field description */
|
||||
private static final long serialVersionUID = -3492811300905099810L;
|
||||
|
||||
@@ -225,8 +231,6 @@ public class HgCGIServlet extends HttpServlet
|
||||
}
|
||||
}
|
||||
|
||||
hookManager.writeHookScript(request);
|
||||
|
||||
CGIExecutor executor = cgiExecutorFactory.createExecutor(configuration,
|
||||
getServletContext(), request, response);
|
||||
|
||||
@@ -234,6 +238,8 @@ public class HgCGIServlet extends HttpServlet
|
||||
executor.getEnvironment().set(ENV_REPOSITORY_NAME, name);
|
||||
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
|
||||
directory.getAbsolutePath());
|
||||
executor.getEnvironment().set(ENV_URL, hookManager.createUrl(request));
|
||||
executor.getEnvironment().set(ENV_CHALLENGE, hookManager.getChallenge());
|
||||
executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath);
|
||||
|
||||
HttpSession session = request.getSession();
|
||||
|
||||
Reference in New Issue
Block a user