mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
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:
@@ -2,7 +2,18 @@ package sonia.scm.repository;
|
|||||||
|
|
||||||
import sonia.scm.plugin.ExtensionPoint;
|
import sonia.scm.plugin.ExtensionPoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strategy to create a namespace for the new repository. Namespaces are used to order and identify repositories.
|
||||||
|
*/
|
||||||
@ExtensionPoint
|
@ExtensionPoint
|
||||||
public interface NamespaceStrategy {
|
public interface NamespaceStrategy {
|
||||||
String getNamespace();
|
|
||||||
|
/**
|
||||||
|
* Create new namespace for the given repository.
|
||||||
|
*
|
||||||
|
* @param repository repository
|
||||||
|
*
|
||||||
|
* @return namespace
|
||||||
|
*/
|
||||||
|
String createNamespace(Repository repository);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
package sonia.scm.repository;
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import sonia.scm.plugin.Extension;
|
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
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@Extension
|
@Extension
|
||||||
public class DefaultNamespaceStrategy implements NamespaceStrategy {
|
public class DefaultNamespaceStrategy implements NamespaceStrategy {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNamespace() {
|
public String createNamespace(Repository repository) {
|
||||||
return SecurityUtils.getSubject().getPrincipal().toString();
|
String namespace = repository.getNamespace();
|
||||||
|
if (Strings.isNullOrEmpty(namespace)) {
|
||||||
|
namespace = SecurityUtils.getSubject().getPrincipal().toString();
|
||||||
|
}
|
||||||
|
return namespace;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
|
|
||||||
public Repository create(Repository repository, boolean initRepository) throws RepositoryException {
|
public Repository create(Repository repository, boolean initRepository) throws RepositoryException {
|
||||||
repository.setId(keyGenerator.createKey());
|
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());
|
logger.info("create repository {} of type {} in namespace {}", repository.getName(), repository.getType(), repository.getNamespace());
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,16 @@ public class DefaultNamespaceStrategyTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SubjectAware(username = "trillian", password = "secret")
|
@SubjectAware(username = "trillian", password = "secret")
|
||||||
public void testNamespaceStrategy() {
|
public void testNamespaceStrategyWithoutPreset() {
|
||||||
assertEquals("trillian", namespaceStrategy.getNamespace());
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import org.apache.shiro.authz.UnauthorizedException;
|
|||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
import sonia.scm.Manager;
|
import sonia.scm.Manager;
|
||||||
@@ -65,9 +66,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.hasProperty;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
@@ -501,7 +500,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
|||||||
configuration.setEnableRepositoryArchive(archiveEnabled);
|
configuration.setEnableRepositoryArchive(archiveEnabled);
|
||||||
|
|
||||||
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
|
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,
|
return new DefaultRepositoryManager(configuration, contextProvider,
|
||||||
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);
|
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);
|
||||||
|
|||||||
Reference in New Issue
Block a user