change NamespaceStrategy signature to createNamespace(Repository)

This change allows us to implement NamespaceStrategies, such as by type (git, hg, svn) or manual defined.
The DefaultNamespaceStrategy accepts now a predefined namespace and only if no namespace was set the username of the currently logged in user is used.
This commit is contained in:
Sebastian Sdorra
2018-08-01 09:43:49 +02:00
parent 6ee0e05e0c
commit ea17e536f1
5 changed files with 36 additions and 11 deletions

View File

@@ -2,7 +2,18 @@ package sonia.scm.repository;
import sonia.scm.plugin.ExtensionPoint;
/**
* Strategy to create a namespace for the new repository. Namespaces are used to order and identify repositories.
*/
@ExtensionPoint
public interface NamespaceStrategy {
String getNamespace();
/**
* Create new namespace for the given repository.
*
* @param repository repository
*
* @return namespace
*/
String createNamespace(Repository repository);
}

View File

@@ -1,17 +1,24 @@
package sonia.scm.repository;
import com.google.common.base.Strings;
import org.apache.shiro.SecurityUtils;
import sonia.scm.plugin.Extension;
/**
* The DefaultNamespaceStrategy returns the username of the currently logged in user as namespace.
* The DefaultNamespaceStrategy returns the predefined namespace of the given repository, if the namespace was not set
* the username of the currently loggedin user is used.
*
* @since 2.0.0
*/
@Extension
public class DefaultNamespaceStrategy implements NamespaceStrategy {
@Override
public String getNamespace() {
return SecurityUtils.getSubject().getPrincipal().toString();
public String createNamespace(Repository repository) {
String namespace = repository.getNamespace();
if (Strings.isNullOrEmpty(namespace)) {
namespace = SecurityUtils.getSubject().getPrincipal().toString();
}
return namespace;
}
}

View File

@@ -141,7 +141,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
public Repository create(Repository repository, boolean initRepository) throws RepositoryException {
repository.setId(keyGenerator.createKey());
repository.setNamespace(namespaceStrategy.getNamespace());
repository.setNamespace(namespaceStrategy.createNamespace(repository));
logger.info("create repository {} of type {} in namespace {}", repository.getName(), repository.getType(), repository.getNamespace());

View File

@@ -17,8 +17,16 @@ public class DefaultNamespaceStrategyTest {
@Test
@SubjectAware(username = "trillian", password = "secret")
public void testNamespaceStrategy() {
assertEquals("trillian", namespaceStrategy.getNamespace());
public void testNamespaceStrategyWithoutPreset() {
assertEquals("trillian", namespaceStrategy.createNamespace(new Repository()));
}
@Test
@SubjectAware(username = "trillian", password = "secret")
public void testNamespaceStrategyWithPreset() {
Repository repository = new Repository();
repository.setNamespace("awesome");
assertEquals("awesome", namespaceStrategy.createNamespace(repository));
}
}

View File

@@ -40,6 +40,7 @@ import org.apache.shiro.authz.UnauthorizedException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import sonia.scm.HandlerEventType;
import sonia.scm.Manager;
@@ -65,9 +66,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -501,7 +500,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
configuration.setEnableRepositoryArchive(archiveEnabled);
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
when(namespaceStrategy.getNamespace()).thenAnswer(invocation -> mockedNamespace);
when(namespaceStrategy.createNamespace(Mockito.any(Repository.class))).thenAnswer(invocation -> mockedNamespace);
return new DefaultRepositoryManager(configuration, contextProvider,
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);