This commit is contained in:
Mohamed Karray
2018-11-29 16:01:43 +01:00
62 changed files with 1253 additions and 983 deletions

View File

@@ -124,7 +124,7 @@ public class AbstractHgHandler
protected AbstractHgHandler(HgRepositoryHandler handler, HgContext context,
Repository repository)
{
this(handler, context, repository, handler.getDirectory(repository));
this(handler, context, repository, handler.getDirectory(repository.getId()));
}
/**

View File

@@ -78,7 +78,7 @@ public class AbstractHgPushOrPullCommand extends AbstractCommand
if (repo != null)
{
url =
handler.getDirectory(request.getRemoteRepository()).getAbsolutePath();
handler.getDirectory(request.getRemoteRepository().getId()).getAbsolutePath();
}
else if (request.getRemoteUrl() != null)
{

View File

@@ -81,7 +81,7 @@ public class HgIncomingCommand extends AbstractCommand
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getIncomingChangesets(IncomingCommandRequest request) {
File remoteRepository = handler.getDirectory(request.getRemoteRepository());
File remoteRepository = handler.getDirectory(request.getRemoteRepository().getId());
com.aragost.javahg.Repository repository = open();

View File

@@ -83,7 +83,7 @@ public class HgOutgoingCommand extends AbstractCommand
public ChangesetPagingResult getOutgoingChangesets(
OutgoingCommandRequest request)
{
File remoteRepository = handler.getDirectory(request.getRemoteRepository());
File remoteRepository = handler.getDirectory(request.getRemoteRepository().getId());
com.aragost.javahg.Repository repository = open();

View File

@@ -81,7 +81,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
{
this.repository = repository;
this.handler = handler;
this.repositoryDirectory = handler.getDirectory(repository);
this.repositoryDirectory = handler.getDirectory(repository.getId());
this.context = new HgCommandContext(hookManager, handler, repository,
repositoryDirectory);
}

View File

@@ -80,6 +80,9 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
/** Field description */
public static final String ENV_REPOSITORY_PATH = "SCM_REPOSITORY_PATH";
/** Field description */
public static final String ENV_REPOSITORY_ID = "SCM_REPOSITORY_ID";
/** Field description */
public static final String ENV_SESSION_PREFIX = "SCM_";
@@ -250,8 +253,7 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
HttpServletResponse response, Repository repository)
throws IOException, ServletException
{
String name = repository.getName();
File directory = handler.getDirectory(repository);
File directory = handler.getDirectory(repository.getId());
CGIExecutor executor = cgiExecutorFactory.createExecutor(configuration,
getServletContext(), request, response);
@@ -261,6 +263,7 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
executor.setStatusCodeHandler(exceptionHandler);
executor.setContentLengthWorkaround(true);
executor.getEnvironment().set(ENV_REPOSITORY_NAME, repository.getNamespace() + "/" + repository.getName());
executor.getEnvironment().set(ENV_REPOSITORY_ID, repository.getId());
executor.getEnvironment().set(ENV_REPOSITORY_PATH,
directory.getAbsolutePath());

View File

@@ -35,6 +35,7 @@ package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.io.Closeables;
import com.google.inject.Inject;
@@ -44,12 +45,10 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ContextEntry;
import sonia.scm.NotFoundException;
import sonia.scm.repository.HgContext;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.api.HgHookMessage;
import sonia.scm.repository.api.HgHookMessage.Severity;
@@ -88,7 +87,7 @@ public class HgHookCallbackServlet extends HttpServlet
public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup";
/** Field description */
public static final String PARAM_REPOSITORYPATH = "repositoryPath";
public static final String PARAM_REPOSITORYID = "repositoryId";
/** Field description */
private static final String PARAM_CHALLENGE = "challenge";
@@ -170,7 +169,7 @@ public class HgHookCallbackServlet extends HttpServlet
if (m.matches())
{
File repositoryPath = getRepositoryPath(request);
String repositoryId = getRepositoryId(request);
String type = m.group(1);
String challenge = request.getParameter(PARAM_CHALLENGE);
@@ -187,7 +186,7 @@ public class HgHookCallbackServlet extends HttpServlet
authenticate(request, credentials);
}
hookCallback(response, repositoryPath, type, challenge, node);
hookCallback(response, type, repositoryId, challenge, node);
}
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
{
HgHookContextProvider context = null;
@@ -258,10 +257,10 @@ public class HgHookCallbackServlet extends HttpServlet
contextProvider.get().setPending(true);
}
File repositoryDirectory = handler.getDirectory(repositoryId);
context = new HgHookContextProvider(handler, repositoryDirectory, hookManager,
node, type);
String repositoryId = getRepositoryId(repositoryDirectory);
hookEventFacade.handle(repositoryId).fireHookEvent(type, 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))
{
RepositoryHookType type = null;
@@ -296,7 +295,7 @@ public class HgHookCallbackServlet extends HttpServlet
if (type != null)
{
fireHook(response, repositoryDirectory, node, type);
fireHook(response, repositoryId, node, type);
}
else
{
@@ -441,21 +440,11 @@ public class HgHookCallbackServlet extends HttpServlet
//~--- get methods ----------------------------------------------------------
@SuppressWarnings("squid:S2083") // we do nothing with the path given, so this should be no issue
private String getRepositoryId(File repositoryPath)
private String getRepositoryId(HttpServletRequest request)
{
return handler.getRepositoryId(repositoryPath);
}
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");
}
String id = request.getParameter(PARAM_REPOSITORYID);
Preconditions.checkArgument(!Strings.isNullOrEmpty(id), "repository id not found in request");
return id;
}
//~--- fields ---------------------------------------------------------------

View File

@@ -41,6 +41,7 @@ import os, urllib, urllib2
baseUrl = os.environ['SCM_URL']
challenge = os.environ['SCM_CHALLENGE']
credentials = os.environ['SCM_CREDENTIALS']
repositoryId = os.environ['SCM_REPOSITORY_ID']
def printMessages(ui, msgs):
for line in msgs:
@@ -53,7 +54,7 @@ def callHookUrl(ui, repo, hooktype, node):
try:
url = baseUrl + hooktype
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
proxy_handler = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_handler)

View File

@@ -50,7 +50,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.Silent.class)
public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
@Mock
@@ -59,8 +59,6 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
@Mock
private com.google.inject.Provider<HgContext> provider;
private RepositoryLocationResolver repositoryLocationResolver;
@Override
protected void checkDirectory(File directory) {
File hgDirectory = new File(directory, ".hg");
@@ -70,11 +68,8 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
}
@Override
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory,
File directory) {
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(contextProvider));
HgRepositoryHandler handler = new HgRepositoryHandler(factory,
new HgContextProvider(), repositoryLocationResolver);
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory, RepositoryLocationResolver locationResolver, File directory) {
HgRepositoryHandler handler = new HgRepositoryHandler(factory, new HgContextProvider(), locationResolver);
handler.init(contextProvider);
HgTestUtil.checkForSkip(handler);
@@ -84,8 +79,7 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
@Test
public void getDirectory() {
HgRepositoryHandler repositoryHandler = new HgRepositoryHandler(factory,
provider, repositoryLocationResolver);
HgRepositoryHandler repositoryHandler = new HgRepositoryHandler(factory, provider, locationResolver);
HgConfig hgConfig = new HgConfig();
hgConfig.setHgBinary("hg");
@@ -93,7 +87,7 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
repositoryHandler.setConfig(hgConfig);
initRepository();
File path = repositoryHandler.getDirectory(repository);
File path = repositoryHandler.getDirectory(repository.getId());
assertEquals(repoPath.toString() + File.separator + AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
}
}

View File

@@ -103,7 +103,7 @@ public final class HgTestUtil
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
RepositoryLocationResolver repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(context));
RepositoryLocationResolver repositoryLocationResolver = new RepositoryLocationResolver(context, repoDao, new InitialRepositoryLocationResolver());
HgRepositoryHandler handler =
new HgRepositoryHandler(new InMemoryConfigurationStoreFactory(), new HgContextProvider(), repositoryLocationResolver);
Path repoDir = directory.toPath();

View File

@@ -41,6 +41,7 @@ import sonia.scm.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
/**
*
@@ -136,6 +137,11 @@ public class TempSCMContextProvider implements SCMContextProvider
this.baseDirectory = baseDirectory;
}
@Override
public Path resolve(Path path) {
return baseDirectory.toPath().resolve(path);
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -94,9 +94,9 @@ public abstract class IncomingOutgoingTestBase extends AbstractTestBase
outgoing = Repository.create(createConfig(temp), outgoingDirectory);
handler = mock(HgRepositoryHandler.class);
when(handler.getDirectory(incomingRepository)).thenReturn(
when(handler.getDirectory(incomingRepository.getId())).thenReturn(
incomingDirectory);
when(handler.getDirectory(outgoingRepository)).thenReturn(
when(handler.getDirectory(outgoingRepository.getId())).thenReturn(
outgoingDirectory);
when(handler.getConfig()).thenReturn(temp.getConfig());
when(handler.getHgContext()).thenReturn(new HgContext());