mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Send repository id with hg hook request
This commit is contained in:
@@ -29,11 +29,15 @@ public class RepositoryLocationResolver {
|
|||||||
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
|
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
File getRepositoryDirectory(Repository repository){
|
public File getRepositoryDirectory(Repository repository){
|
||||||
if (repositoryDAO instanceof PathBasedRepositoryDAO) {
|
if (repositoryDAO instanceof PathBasedRepositoryDAO) {
|
||||||
PathBasedRepositoryDAO pathBasedRepositoryDAO = (PathBasedRepositoryDAO) repositoryDAO;
|
PathBasedRepositoryDAO pathBasedRepositoryDAO = (PathBasedRepositoryDAO) repositoryDAO;
|
||||||
return pathBasedRepositoryDAO.getPath(repository).toFile();
|
return pathBasedRepositoryDAO.getPath(repository).toFile();
|
||||||
}
|
}
|
||||||
return initialRepositoryLocationResolver.getRelativeRepositoryPath(repository).getAbsolutePath();
|
return initialRepositoryLocationResolver.getRelativeRepositoryPath(repository).getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getRepositoryDirectory(String repositoryId) {
|
||||||
|
return getRepositoryDirectory(repositoryDAO.get(repositoryId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ public class HgRepositoryHandler
|
|||||||
RepositoryLocationResolver repositoryLocationResolver)
|
RepositoryLocationResolver repositoryLocationResolver)
|
||||||
{
|
{
|
||||||
super(storeFactory, repositoryLocationResolver);
|
super(storeFactory, repositoryLocationResolver);
|
||||||
|
this.repositoryLocationResolver = repositoryLocationResolver;
|
||||||
this.hgContextProvider = hgContextProvider;
|
this.hgContextProvider = hgContextProvider;
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -427,6 +428,10 @@ public class HgRepositoryHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getDirectory(String repositoryId) {
|
||||||
|
return repositoryLocationResolver.getRepositoryDirectory(repositoryId);
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
@@ -434,4 +439,6 @@ public class HgRepositoryHandler
|
|||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private JAXBContext jaxbContext;
|
private JAXBContext jaxbContext;
|
||||||
|
|
||||||
|
private final RepositoryLocationResolver repositoryLocationResolver;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,6 +80,9 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
|
|||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String ENV_REPOSITORY_PATH = "SCM_REPOSITORY_PATH";
|
public static final String ENV_REPOSITORY_PATH = "SCM_REPOSITORY_PATH";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String ENV_REPOSITORY_ID = "SCM_REPOSITORY_ID";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String ENV_SESSION_PREFIX = "SCM_";
|
public static final String ENV_SESSION_PREFIX = "SCM_";
|
||||||
|
|
||||||
@@ -261,6 +264,7 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
|
|||||||
executor.setStatusCodeHandler(exceptionHandler);
|
executor.setStatusCodeHandler(exceptionHandler);
|
||||||
executor.setContentLengthWorkaround(true);
|
executor.setContentLengthWorkaround(true);
|
||||||
executor.getEnvironment().set(ENV_REPOSITORY_NAME, repository.getNamespace() + "/" + repository.getName());
|
executor.getEnvironment().set(ENV_REPOSITORY_NAME, repository.getNamespace() + "/" + repository.getName());
|
||||||
|
executor.getEnvironment().set(ENV_REPOSITORY_ID, repository.getId());
|
||||||
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
|
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
|
||||||
directory.getAbsolutePath());
|
directory.getAbsolutePath());
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ package sonia.scm.web;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.io.Closeables;
|
import com.google.common.io.Closeables;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
@@ -44,12 +45,10 @@ import org.apache.shiro.SecurityUtils;
|
|||||||
import org.apache.shiro.subject.Subject;
|
import org.apache.shiro.subject.Subject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.ContextEntry;
|
|
||||||
import sonia.scm.NotFoundException;
|
import sonia.scm.NotFoundException;
|
||||||
import sonia.scm.repository.HgContext;
|
import sonia.scm.repository.HgContext;
|
||||||
import sonia.scm.repository.HgHookManager;
|
import sonia.scm.repository.HgHookManager;
|
||||||
import sonia.scm.repository.HgRepositoryHandler;
|
import sonia.scm.repository.HgRepositoryHandler;
|
||||||
import sonia.scm.repository.InternalRepositoryException;
|
|
||||||
import sonia.scm.repository.RepositoryHookType;
|
import sonia.scm.repository.RepositoryHookType;
|
||||||
import sonia.scm.repository.api.HgHookMessage;
|
import sonia.scm.repository.api.HgHookMessage;
|
||||||
import sonia.scm.repository.api.HgHookMessage.Severity;
|
import sonia.scm.repository.api.HgHookMessage.Severity;
|
||||||
@@ -88,7 +87,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup";
|
public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String PARAM_REPOSITORYPATH = "repositoryPath";
|
public static final String PARAM_REPOSITORYID = "repositoryId";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final String PARAM_CHALLENGE = "challenge";
|
private static final String PARAM_CHALLENGE = "challenge";
|
||||||
@@ -170,7 +169,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
|
|
||||||
if (m.matches())
|
if (m.matches())
|
||||||
{
|
{
|
||||||
File repositoryPath = getRepositoryPath(request);
|
String repositoryId = getRepositoryId(request);
|
||||||
String type = m.group(1);
|
String type = m.group(1);
|
||||||
String challenge = request.getParameter(PARAM_CHALLENGE);
|
String challenge = request.getParameter(PARAM_CHALLENGE);
|
||||||
|
|
||||||
@@ -187,7 +186,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
authenticate(request, credentials);
|
authenticate(request, credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
hookCallback(response, repositoryPath, type, challenge, node);
|
hookCallback(response, type, repositoryId, challenge, node);
|
||||||
}
|
}
|
||||||
else if (logger.isDebugEnabled())
|
else if (logger.isDebugEnabled())
|
||||||
{
|
{
|
||||||
@@ -246,7 +245,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fireHook(HttpServletResponse response, File repositoryDirectory, String node, RepositoryHookType type)
|
private void fireHook(HttpServletResponse response, String repositoryId, String node, RepositoryHookType type)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
HgHookContextProvider context = null;
|
HgHookContextProvider context = null;
|
||||||
@@ -258,10 +257,10 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
contextProvider.get().setPending(true);
|
contextProvider.get().setPending(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File repositoryDirectory = handler.getDirectory(repositoryId);
|
||||||
context = new HgHookContextProvider(handler, repositoryDirectory, hookManager,
|
context = new HgHookContextProvider(handler, repositoryDirectory, hookManager,
|
||||||
node, type);
|
node, type);
|
||||||
|
|
||||||
String repositoryId = getRepositoryId(repositoryDirectory);
|
|
||||||
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
hookEventFacade.handle(repositoryId).fireHookEvent(type, context);
|
||||||
|
|
||||||
printMessages(response, context);
|
printMessages(response, context);
|
||||||
@@ -280,7 +279,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void hookCallback(HttpServletResponse response, File repositoryDirectory, String typeName, String challenge, String node) throws IOException {
|
private void hookCallback(HttpServletResponse response, String typeName, String repositoryId, String challenge, String node) throws IOException {
|
||||||
if (hookManager.isAcceptAble(challenge))
|
if (hookManager.isAcceptAble(challenge))
|
||||||
{
|
{
|
||||||
RepositoryHookType type = null;
|
RepositoryHookType type = null;
|
||||||
@@ -296,7 +295,7 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
|
|
||||||
if (type != null)
|
if (type != null)
|
||||||
{
|
{
|
||||||
fireHook(response, repositoryDirectory, node, type);
|
fireHook(response, repositoryId, node, type);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -441,21 +440,11 @@ public class HgHookCallbackServlet extends HttpServlet
|
|||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
@SuppressWarnings("squid:S2083") // we do nothing with the path given, so this should be no issue
|
private String getRepositoryId(HttpServletRequest request)
|
||||||
private String getRepositoryId(File repositoryPath)
|
|
||||||
{
|
{
|
||||||
return handler.getRepositoryId(repositoryPath);
|
String id = request.getParameter(PARAM_REPOSITORYID);
|
||||||
}
|
Preconditions.checkArgument(!Strings.isNullOrEmpty(id), "repository id not found in request");
|
||||||
|
return id;
|
||||||
private File getRepositoryPath(HttpServletRequest request) {
|
|
||||||
String path = request.getParameter(PARAM_REPOSITORYPATH);
|
|
||||||
if (Util.isNotEmpty(path)) {
|
|
||||||
return new File(path);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new InternalRepositoryException(ContextEntry.ContextBuilder.entity("directory", path), "could not find hgrc in directory");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import os, urllib, urllib2
|
|||||||
baseUrl = os.environ['SCM_URL']
|
baseUrl = os.environ['SCM_URL']
|
||||||
challenge = os.environ['SCM_CHALLENGE']
|
challenge = os.environ['SCM_CHALLENGE']
|
||||||
credentials = os.environ['SCM_CREDENTIALS']
|
credentials = os.environ['SCM_CREDENTIALS']
|
||||||
|
repositoryId = os.environ['SCM_REPOSITORY_ID']
|
||||||
|
|
||||||
def printMessages(ui, msgs):
|
def printMessages(ui, msgs):
|
||||||
for line in msgs:
|
for line in msgs:
|
||||||
@@ -53,7 +54,7 @@ def callHookUrl(ui, repo, hooktype, node):
|
|||||||
try:
|
try:
|
||||||
url = baseUrl + hooktype
|
url = baseUrl + hooktype
|
||||||
ui.debug( "send scm-hook to " + url + " and " + node + "\n" )
|
ui.debug( "send scm-hook to " + url + " and " + node + "\n" )
|
||||||
data = urllib.urlencode({'node': node, 'challenge': challenge, 'credentials': credentials, 'repositoryPath': repo.root})
|
data = urllib.urlencode({'node': node, 'challenge': challenge, 'credentials': credentials, 'repositoryPath': repo.root, 'repositoryId': repositoryId})
|
||||||
# open url but ignore proxy settings
|
# open url but ignore proxy settings
|
||||||
proxy_handler = urllib2.ProxyHandler({})
|
proxy_handler = urllib2.ProxyHandler({})
|
||||||
opener = urllib2.build_opener(proxy_handler)
|
opener = urllib2.build_opener(proxy_handler)
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package sonia.scm.web;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import sonia.scm.repository.HgRepositoryHandler;
|
|
||||||
import sonia.scm.repository.Repository;
|
|
||||||
import sonia.scm.repository.RepositoryDAO;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.mockito.Matchers.anyInt;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import static sonia.scm.web.HgHookCallbackServlet.PARAM_REPOSITORYPATH;
|
|
||||||
|
|
||||||
public class HgHookCallbackServletTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldExtractCorrectRepositoryId() throws ServletException, IOException {
|
|
||||||
HgRepositoryHandler handler = mock(HgRepositoryHandler.class);
|
|
||||||
HgHookCallbackServlet servlet = new HgHookCallbackServlet(null, handler, null, null);
|
|
||||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
||||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
|
||||||
|
|
||||||
when(request.getContextPath()).thenReturn("http://example.com/scm");
|
|
||||||
when(request.getRequestURI()).thenReturn("http://example.com/scm/hook/hg/pretxnchangegroup");
|
|
||||||
String path = "/tmp/hg/12345";
|
|
||||||
when(request.getParameter(PARAM_REPOSITORYPATH)).thenReturn(path);
|
|
||||||
|
|
||||||
servlet.doPost(request, response);
|
|
||||||
|
|
||||||
verify(response, never()).sendError(anyInt());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user