mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Refactor the repository store implementation in order to store repositories in specific paths.
This commit is contained in:
@@ -6,15 +6,12 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class HgConfigDto extends HalRepresentation {
|
||||
|
||||
private boolean disabled;
|
||||
private File repositoryDirectory;
|
||||
|
||||
private String encoding;
|
||||
private String hgBinary;
|
||||
|
||||
@@ -52,32 +52,6 @@ import sonia.scm.net.ahc.AdvancedHttpClient;
|
||||
public abstract class AbstractHgInstaller implements HgInstaller
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String DIRECTORY_REPOSITORY = "repositories";
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param baseDirectory
|
||||
* @param config
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void install(File baseDirectory, HgConfig config) throws IOException
|
||||
{
|
||||
File repoDirectory = new File(
|
||||
baseDirectory,
|
||||
DIRECTORY_REPOSITORY.concat(File.separator).concat(
|
||||
HgRepositoryHandler.TYPE_NAME));
|
||||
|
||||
IOUtil.mkdirs(repoDirectory);
|
||||
config.setRepositoryDirectory(repoDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
|
||||
@@ -74,8 +74,6 @@ public class UnixHgInstaller extends AbstractHgInstaller
|
||||
@Override
|
||||
public void install(File baseDirectory, HgConfig config) throws IOException
|
||||
{
|
||||
super.install(baseDirectory, config);
|
||||
|
||||
// search mercurial (hg)
|
||||
if (Util.isEmpty(config.getHgBinary()))
|
||||
{
|
||||
|
||||
@@ -116,8 +116,6 @@ public class WindowsHgInstaller extends AbstractHgInstaller
|
||||
@Override
|
||||
public void install(File baseDirectory, HgConfig config) throws IOException
|
||||
{
|
||||
super.install(baseDirectory, config);
|
||||
|
||||
if (Util.isEmpty(config.getPythonBinary()))
|
||||
{
|
||||
String pythonBinary = getPythonBinary();
|
||||
|
||||
@@ -111,16 +111,16 @@ public class HgRepositoryHandler
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param storeFactory
|
||||
* @param storeFactory
|
||||
* @param fileSystem
|
||||
* @param hgContextProvider
|
||||
* @param repositoryLocationResolver
|
||||
*/
|
||||
@Inject
|
||||
public HgRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem,
|
||||
Provider<HgContext> hgContextProvider)
|
||||
Provider<HgContext> hgContextProvider, RepositoryLocationResolver repositoryLocationResolver)
|
||||
{
|
||||
super(storeFactory, fileSystem);
|
||||
super(storeFactory, fileSystem, repositoryLocationResolver);
|
||||
this.hgContextProvider = hgContextProvider;
|
||||
|
||||
try
|
||||
@@ -566,8 +566,7 @@ public class HgRepositoryHandler
|
||||
|
||||
if (c != null)
|
||||
{
|
||||
File repositoryDirectroy = c.getRepositoryDirectory();
|
||||
|
||||
File repositoryDirectroy = getInitialBaseDirectory();
|
||||
if (repositoryDirectroy.exists())
|
||||
{
|
||||
File lockFile = new File(repositoryDirectroy, PATH_HOOK);
|
||||
|
||||
@@ -123,8 +123,9 @@ public class HgHookChangesetProvider implements HookChangesetProvider
|
||||
*/
|
||||
private Repository open()
|
||||
{
|
||||
File directory = handler.getConfig().getRepositoryDirectory();
|
||||
File repositoryDirectory = new File(directory, id);
|
||||
sonia.scm.repository.Repository repo = new sonia.scm.repository.Repository();
|
||||
repo.setId(id);
|
||||
File repositoryDirectory = handler.getDirectory(repo);
|
||||
|
||||
// use HG_PENDING only for pre receive hooks
|
||||
boolean pending = type == RepositoryHookType.PRE_RECEIVE;
|
||||
|
||||
@@ -23,7 +23,6 @@ public class HgConfigDtoToHgConfigMapperTest {
|
||||
HgConfig config = mapper.map(dto);
|
||||
|
||||
assertTrue(config.isDisabled());
|
||||
assertEquals("repository/directory", config.getRepositoryDirectory().getPath());
|
||||
|
||||
assertEquals("ABC", config.getEncoding());
|
||||
assertEquals("/etc/hg", config.getHgBinary());
|
||||
@@ -36,7 +35,6 @@ public class HgConfigDtoToHgConfigMapperTest {
|
||||
private HgConfigDto createDefaultDto() {
|
||||
HgConfigDto configDto = new HgConfigDto();
|
||||
configDto.setDisabled(true);
|
||||
configDto.setRepositoryDirectory(new File("repository/directory"));
|
||||
configDto.setEncoding("ABC");
|
||||
configDto.setHgBinary("/etc/hg");
|
||||
configDto.setPythonBinary("/py");
|
||||
|
||||
@@ -93,7 +93,6 @@ public class HgConfigResourceTest {
|
||||
ObjectNode responseJson = new ObjectMapper().readValue(responseString, ObjectNode.class);
|
||||
|
||||
assertTrue(responseString.contains("\"disabled\":false"));
|
||||
assertTrue(responseJson.get("repositoryDirectory").asText().endsWith("repository/directory"));
|
||||
assertTrue(responseString.contains("\"self\":{\"href\":\"/v2/config/hg"));
|
||||
assertTrue(responseString.contains("\"update\":{\"href\":\"/v2/config/hg"));
|
||||
}
|
||||
@@ -162,7 +161,6 @@ public class HgConfigResourceTest {
|
||||
private HgConfig createConfiguration() {
|
||||
HgConfig config = new HgConfig();
|
||||
config.setDisabled(false);
|
||||
config.setRepositoryDirectory(new File("repository/directory"));
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ class HgConfigTests {
|
||||
static HgConfig createConfiguration() {
|
||||
HgConfig config = new HgConfig();
|
||||
config.setDisabled(true);
|
||||
config.setRepositoryDirectory(new File("repository/directory"));
|
||||
|
||||
config.setEncoding("ABC");
|
||||
config.setHgBinary("/etc/hg");
|
||||
@@ -30,7 +29,6 @@ class HgConfigTests {
|
||||
|
||||
static void assertEqualsConfiguration(HgConfigDto dto) {
|
||||
assertTrue(dto.isDisabled());
|
||||
assertEquals("repository/directory", dto.getRepositoryDirectory().getPath());
|
||||
|
||||
assertEquals("ABC", dto.getEncoding());
|
||||
assertEquals("/etc/hg", dto.getHgBinary());
|
||||
|
||||
@@ -42,9 +42,13 @@ import sonia.scm.io.DefaultFileSystem;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -61,6 +65,9 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
@Mock
|
||||
private com.google.inject.Provider<HgContext> provider;
|
||||
|
||||
RepositoryLocationResolver repositoryLocationResolver ;
|
||||
private Path repoDir;
|
||||
|
||||
@Override
|
||||
protected void checkDirectory(File directory) {
|
||||
File hgDirectory = new File(directory, ".hg");
|
||||
@@ -77,14 +84,17 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
|
||||
@Override
|
||||
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory,
|
||||
File directory) {
|
||||
File directory) throws RepositoryPathNotFoundException {
|
||||
DefaultFileSystem fileSystem = new DefaultFileSystem();
|
||||
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
|
||||
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(contextProvider,fileSystem));
|
||||
HgRepositoryHandler handler = new HgRepositoryHandler(factory,
|
||||
new DefaultFileSystem(),
|
||||
new HgContextProvider());
|
||||
new HgContextProvider(), repositoryLocationResolver);
|
||||
|
||||
handler.init(contextProvider);
|
||||
handler.getConfig().setRepositoryDirectory(directory);
|
||||
|
||||
repoDir = directory.toPath();
|
||||
when(repoDao.getPath(any())).thenReturn(repoDir);
|
||||
HgTestUtil.checkForSkip(handler);
|
||||
|
||||
return handler;
|
||||
@@ -93,17 +103,15 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
||||
@Test
|
||||
public void getDirectory() {
|
||||
HgRepositoryHandler repositoryHandler = new HgRepositoryHandler(factory,
|
||||
new DefaultFileSystem(), provider);
|
||||
new DefaultFileSystem(), provider, repositoryLocationResolver);
|
||||
|
||||
HgConfig hgConfig = new HgConfig();
|
||||
hgConfig.setRepositoryDirectory(new File("/path"));
|
||||
hgConfig.setHgBinary("hg");
|
||||
hgConfig.setPythonBinary("python");
|
||||
repositoryHandler.setConfig(hgConfig);
|
||||
|
||||
Repository repository = new Repository("id", "git", "Space", "Name");
|
||||
|
||||
File path = repositoryHandler.getDirectory(repository);
|
||||
assertEquals("/path/id", path.getAbsolutePath());
|
||||
assertEquals(repoDir.toString()+File.separator+InitialRepositoryLocationResolver.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import static org.mockito.Mockito.*;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -95,19 +96,21 @@ public final class HgTestUtil
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static HgRepositoryHandler createHandler(File directory)
|
||||
{
|
||||
public static HgRepositoryHandler createHandler(File directory) throws RepositoryPathNotFoundException {
|
||||
TempSCMContextProvider context =
|
||||
(TempSCMContextProvider) SCMContext.getContext();
|
||||
|
||||
context.setBaseDirectory(directory);
|
||||
|
||||
FileSystem fileSystem = mock(FileSystem.class);
|
||||
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
|
||||
|
||||
RepositoryLocationResolver repositoryLocationResolver = new RepositoryLocationResolver(repoDao, new InitialRepositoryLocationResolver(context,fileSystem));
|
||||
HgRepositoryHandler handler =
|
||||
new HgRepositoryHandler(new InMemoryConfigurationStoreFactory(), fileSystem,
|
||||
new HgContextProvider());
|
||||
|
||||
new HgContextProvider(), repositoryLocationResolver);
|
||||
Path repoDir = directory.toPath();
|
||||
when(repoDao.getPath(any())).thenReturn(repoDir);
|
||||
handler.init(context);
|
||||
|
||||
return handler;
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.junit.Before;
|
||||
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.HgTestUtil;
|
||||
import sonia.scm.repository.RepositoryPathNotFoundException;
|
||||
import sonia.scm.repository.RepositoryTestData;
|
||||
import sonia.scm.util.MockUtil;
|
||||
|
||||
@@ -76,8 +77,7 @@ public class AbstractHgCommandTestBase extends ZippedRepositoryTestBase
|
||||
* @throws IOException
|
||||
*/
|
||||
@Before
|
||||
public void initHgHandler() throws IOException
|
||||
{
|
||||
public void initHgHandler() throws IOException, RepositoryPathNotFoundException {
|
||||
this.handler = HgTestUtil.createHandler(tempFolder.newFolder());
|
||||
|
||||
HgTestUtil.checkForSkip(handler);
|
||||
|
||||
@@ -51,6 +51,7 @@ import sonia.scm.repository.HgConfig;
|
||||
import sonia.scm.repository.HgContext;
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.HgTestUtil;
|
||||
import sonia.scm.repository.RepositoryPathNotFoundException;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserTestData;
|
||||
import sonia.scm.util.MockUtil;
|
||||
@@ -78,8 +79,7 @@ public abstract class IncomingOutgoingTestBase extends AbstractTestBase
|
||||
* @throws IOException
|
||||
*/
|
||||
@Before
|
||||
public void initHgHandler() throws IOException
|
||||
{
|
||||
public void initHgHandler() throws IOException, RepositoryPathNotFoundException {
|
||||
HgRepositoryHandler temp = HgTestUtil.createHandler(tempFolder.newFolder());
|
||||
|
||||
HgTestUtil.checkForSkip(temp);
|
||||
|
||||
@@ -25,14 +25,15 @@ public class HgHookCallbackServletTest {
|
||||
HgHookCallbackServlet servlet = new HgHookCallbackServlet(null, handler, null, null);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
HgConfig config = mock(HgConfig.class);
|
||||
|
||||
when(request.getContextPath()).thenReturn("http://example.com/scm");
|
||||
when(request.getRequestURI()).thenReturn("http://example.com/scm/hook/hg/pretxnchangegroup");
|
||||
when(request.getParameter(PARAM_REPOSITORYPATH)).thenReturn("/tmp/hg/12345");
|
||||
String path = "/tmp/hg/12345";
|
||||
when(request.getParameter(PARAM_REPOSITORYPATH)).thenReturn(path);
|
||||
|
||||
when(handler.getConfig()).thenReturn(config);
|
||||
when(config.getRepositoryDirectory()).thenReturn(new File("/tmp/hg"));
|
||||
|
||||
File file = new File(path);
|
||||
when(handler.getInitialBaseDirectory()).thenReturn(file);
|
||||
|
||||
servlet.doPost(request, response);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user