Refactor the repository store implementation in order to store repositories in specific paths.

This commit is contained in:
Mohamed Karray
2018-11-15 10:04:16 +01:00
parent 2cd2cfbde2
commit f8ae7cedf7
51 changed files with 628 additions and 594 deletions

View File

@@ -6,14 +6,11 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.File;
@NoArgsConstructor
@Getter
@Setter
public class GitConfigDto extends HalRepresentation {
private File repositoryDirectory;
private boolean disabled = false;
private String gcExpression;

View File

@@ -88,7 +88,7 @@ public class GitRepositoryHandler
GitRepositoryServiceProvider.COMMANDS);
private static final Object LOCK = new Object();
private final Scheduler scheduler;
private Task task;
@@ -98,15 +98,15 @@ public class GitRepositoryHandler
/**
* Constructs ...
*
*
* @param storeFactory
* @param storeFactory
* @param fileSystem
* @param scheduler
* @param repositoryLocationResolver
*/
@Inject
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler)
public GitRepositoryHandler(ConfigurationStoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler, RepositoryLocationResolver repositoryLocationResolver)
{
super(storeFactory, fileSystem);
super(storeFactory, fileSystem, repositoryLocationResolver);
this.scheduler = scheduler;
}

View File

@@ -106,7 +106,7 @@ public class GitRepositoryResolver implements RepositoryResolver<HttpServletRequ
if (config.isValid())
{
File gitdir = findRepository(config.getRepositoryDirectory(), repo.getId());
File gitdir = handler.getDirectory(repo);
if (gitdir == null) {
throw new RepositoryNotFoundException(repositoryName);
}
@@ -132,32 +132,6 @@ public class GitRepositoryResolver implements RepositoryResolver<HttpServletRequ
}
}
@VisibleForTesting
File findRepository(File parentDirectory, String repositoryName) {
File repositoryDirectory = new File(parentDirectory, repositoryName);
if (repositoryDirectory.exists()) {
return repositoryDirectory;
}
if (endsWithDotGit(repositoryName)) {
String repositoryNameWithoutDotGit = repositoryNameWithoutDotGit(repositoryName);
repositoryDirectory = new File(parentDirectory, repositoryNameWithoutDotGit);
if (repositoryDirectory.exists()) {
return repositoryDirectory;
}
}
return null;
}
private boolean endsWithDotGit(String repositoryName) {
return repositoryName.endsWith(GitRepositoryHandler.DOT_GIT);
}
private String repositoryNameWithoutDotGit(String repositoryName) {
return repositoryName.substring(0, repositoryName.length() - GitRepositoryHandler.DOT_GIT.length());
}
//~--- fields ---------------------------------------------------------------
private final GitRepositoryHandler handler;

View File

@@ -6,8 +6,6 @@ import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.repository.GitConfig;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -22,7 +20,6 @@ public class GitConfigDtoToGitConfigMapperTest {
GitConfigDto dto = createDefaultDto();
GitConfig config = mapper.map(dto);
assertEquals("express", config.getGcExpression());
assertEquals("repository/directory", config.getRepositoryDirectory().getPath());
assertFalse(config.isDisabled());
}
@@ -30,7 +27,6 @@ public class GitConfigDtoToGitConfigMapperTest {
GitConfigDto gitConfigDto = new GitConfigDto();
gitConfigDto.setGcExpression("express");
gitConfigDto.setDisabled(false);
gitConfigDto.setRepositoryDirectory(new File("repository/directory"));
return gitConfigDto;
}
}

View File

@@ -81,7 +81,6 @@ public class GitConfigResourceTest {
ObjectNode responseJson = new ObjectMapper().readValue(responseString, ObjectNode.class);
assertTrue(responseString.contains("\"disabled\":false"));
assertTrue(responseJson.get("repositoryDirectory").asText().endsWith("repository/directory"));
assertTrue(responseString.contains("\"gcExpression\":\"valid Git GC Cron Expression\""));
assertTrue(responseString.contains("\"self\":{\"href\":\"/v2/config/git"));
assertTrue(responseString.contains("\"update\":{\"href\":\"/v2/config/git"));
@@ -152,7 +151,6 @@ public class GitConfigResourceTest {
GitConfig config = new GitConfig();
config.setGcExpression("valid Git GC Cron Expression");
config.setDisabled(false);
config.setRepositoryDirectory(new File("repository/directory"));
return config;
}

View File

@@ -60,7 +60,6 @@ public class GitConfigToGitConfigDtoMapperTest {
assertEquals("express", dto.getGcExpression());
assertFalse(dto.isDisabled());
assertEquals("repository/directory", dto.getRepositoryDirectory().getPath());
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("update").get().getHref());
}
@@ -79,7 +78,6 @@ public class GitConfigToGitConfigDtoMapperTest {
private GitConfig createConfiguration() {
GitConfig config = new GitConfig();
config.setDisabled(false);
config.setRepositoryDirectory(new File("repository/directory"));
config.setGcExpression("express");
return config;
}

View File

@@ -42,9 +42,13 @@ import sonia.scm.schedule.Scheduler;
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 GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
@Mock
private ConfigurationStoreFactory factory;
RepositoryLocationResolver repositoryLocationResolver ;
private Path repoDir;
@Override
protected void checkDirectory(File directory) {
File head = new File(directory, "HEAD");
@@ -82,15 +89,20 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
@Override
protected RepositoryHandler createRepositoryHandler(ConfigurationStoreFactory factory,
File directory) {
File directory) throws RepositoryPathNotFoundException {
DefaultFileSystem fileSystem = new DefaultFileSystem();
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
InitialRepositoryLocationResolver initialRepositoryLocationResolver = new InitialRepositoryLocationResolver(contextProvider,fileSystem);
repositoryLocationResolver = new RepositoryLocationResolver(repoDao, initialRepositoryLocationResolver);
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
new DefaultFileSystem(), scheduler);
fileSystem, scheduler, repositoryLocationResolver);
repoDir = directory.toPath();
when(repoDao.getPath(any())).thenReturn(repoDir);
repositoryHandler.init(contextProvider);
GitConfig config = new GitConfig();
config.setRepositoryDirectory(directory);
// TODO fix event bus exception
repositoryHandler.setConfig(config);
@@ -98,17 +110,18 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
}
@Test
public void getDirectory() {
public void getDirectory() {
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
new DefaultFileSystem(), scheduler);
GitConfig gitConfig = new GitConfig();
gitConfig.setRepositoryDirectory(new File("/path"));
repositoryHandler.setConfig(gitConfig);
new DefaultFileSystem(), scheduler, repositoryLocationResolver);
Repository repository = new Repository("id", "git", "Space", "Name");
GitConfig config = new GitConfig();
config.setDisabled(false);
config.setGcExpression("gc exp");
repositoryHandler.setConfig(config);
File path = repositoryHandler.getDirectory(repository);
assertEquals("/path/id", path.getAbsolutePath());
assertEquals(repoDir.toString()+File.separator+InitialRepositoryLocationResolver.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
}
}

View File

@@ -1,110 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.web;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.repository.GitConfig;
import sonia.scm.repository.GitRepositoryHandler;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link GitRepositoryResolver}.
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class GitRepositoryResolverTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File parentDirectory;
@Mock
private GitRepositoryHandler handler;
@InjectMocks
private GitRepositoryResolver resolver;
@Before
public void setUp() throws IOException {
parentDirectory = temporaryFolder.newFolder();
GitConfig config = new GitConfig();
config.setRepositoryDirectory(parentDirectory);
}
@Test
public void testFindRepositoryWithoutDotGit() {
createRepositories("a", "ab");
File directory = resolver.findRepository(parentDirectory, "a");
assertNotNull(directory);
assertEquals("a", directory.getName());
directory = resolver.findRepository(parentDirectory, "ab");
assertNotNull(directory);
assertEquals("ab", directory.getName());
}
@Test
public void testFindRepositoryWithDotGit() {
createRepositories("a", "ab");
File directory = resolver.findRepository(parentDirectory, "a.git");
assertNotNull(directory);
assertEquals("a", directory.getName());
directory = resolver.findRepository(parentDirectory, "ab.git");
assertNotNull(directory);
assertEquals("ab", directory.getName());
}
private void createRepositories(String... names) {
for (String name : names) {
assertTrue(new File(parentDirectory, name).mkdirs());
}
}
}