mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-01 19:15:52 +01:00
fix checkout of repositories with dots in the names
This commit is contained in:
@@ -32,13 +32,15 @@ public class HttpProtocolServlet extends HttpServlet {
|
|||||||
public static final String PATTERN = PATH + "/*";
|
public static final String PATTERN = PATH + "/*";
|
||||||
|
|
||||||
private final RepositoryServiceFactory serviceFactory;
|
private final RepositoryServiceFactory serviceFactory;
|
||||||
|
private final NamespaceAndNameFromPathExtractor pathExtractor;
|
||||||
private final PushStateDispatcher dispatcher;
|
private final PushStateDispatcher dispatcher;
|
||||||
private final UserAgentParser userAgentParser;
|
private final UserAgentParser userAgentParser;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public HttpProtocolServlet(RepositoryServiceFactory serviceFactory, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
|
public HttpProtocolServlet(RepositoryServiceFactory serviceFactory, NamespaceAndNameFromPathExtractor pathExtractor, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
|
||||||
this.serviceFactory = serviceFactory;
|
this.serviceFactory = serviceFactory;
|
||||||
|
this.pathExtractor = pathExtractor;
|
||||||
this.dispatcher = dispatcher;
|
this.dispatcher = dispatcher;
|
||||||
this.userAgentParser = userAgentParser;
|
this.userAgentParser = userAgentParser;
|
||||||
}
|
}
|
||||||
@@ -51,7 +53,7 @@ public class HttpProtocolServlet extends HttpServlet {
|
|||||||
dispatcher.dispatch(request, response, request.getRequestURI());
|
dispatcher.dispatch(request, response, request.getRequestURI());
|
||||||
} else {
|
} else {
|
||||||
String pathInfo = request.getPathInfo();
|
String pathInfo = request.getPathInfo();
|
||||||
Optional<NamespaceAndName> namespaceAndName = NamespaceAndNameFromPathExtractor.fromUri(pathInfo);
|
Optional<NamespaceAndName> namespaceAndName = pathExtractor.fromUri(pathInfo);
|
||||||
if (namespaceAndName.isPresent()) {
|
if (namespaceAndName.isPresent()) {
|
||||||
service(request, response, namespaceAndName.get());
|
service(request, response, namespaceAndName.get());
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,18 +1,31 @@
|
|||||||
package sonia.scm.web.protocol;
|
package sonia.scm.web.protocol;
|
||||||
|
|
||||||
|
import sonia.scm.Type;
|
||||||
import sonia.scm.repository.NamespaceAndName;
|
import sonia.scm.repository.NamespaceAndName;
|
||||||
|
import sonia.scm.repository.RepositoryManager;
|
||||||
import sonia.scm.util.HttpUtil;
|
import sonia.scm.util.HttpUtil;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static java.util.Optional.empty;
|
import static java.util.Optional.empty;
|
||||||
import static java.util.Optional.of;
|
import static java.util.Optional.of;
|
||||||
|
|
||||||
final class NamespaceAndNameFromPathExtractor {
|
final class NamespaceAndNameFromPathExtractor {
|
||||||
|
|
||||||
private NamespaceAndNameFromPathExtractor() {}
|
private final Set<String> types;
|
||||||
|
|
||||||
static Optional<NamespaceAndName> fromUri(String uri) {
|
@Inject
|
||||||
|
public NamespaceAndNameFromPathExtractor(RepositoryManager repositoryManager) {
|
||||||
|
this.types = repositoryManager.getConfiguredTypes()
|
||||||
|
.stream()
|
||||||
|
.map(Type::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<NamespaceAndName> fromUri(String uri) {
|
||||||
if (uri.startsWith(HttpUtil.SEPARATOR_PATH)) {
|
if (uri.startsWith(HttpUtil.SEPARATOR_PATH)) {
|
||||||
uri = uri.substring(1);
|
uri = uri.substring(1);
|
||||||
}
|
}
|
||||||
@@ -30,12 +43,13 @@ final class NamespaceAndNameFromPathExtractor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String name = uri.substring(endOfNamespace + 1, nameIndex);
|
String name = uri.substring(endOfNamespace + 1, nameIndex);
|
||||||
|
int nameDotIndex = name.lastIndexOf('.');
|
||||||
int nameDotIndex = name.indexOf('.');
|
|
||||||
if (nameDotIndex >= 0) {
|
if (nameDotIndex >= 0) {
|
||||||
return of(new NamespaceAndName(namespace, name.substring(0, nameDotIndex)));
|
String suffix = name.substring(nameDotIndex + 1);
|
||||||
} else {
|
if (types.contains(suffix)) {
|
||||||
return of(new NamespaceAndName(namespace, name));
|
name = name.substring(0, nameDotIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return of(new NamespaceAndName(namespace, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,48 @@
|
|||||||
package sonia.scm.web.protocol;
|
package sonia.scm.web.protocol;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DynamicNode;
|
import org.junit.jupiter.api.DynamicNode;
|
||||||
import org.junit.jupiter.api.DynamicTest;
|
import org.junit.jupiter.api.DynamicTest;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestFactory;
|
import org.junit.jupiter.api.TestFactory;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import sonia.scm.repository.NamespaceAndName;
|
import sonia.scm.repository.NamespaceAndName;
|
||||||
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
import sonia.scm.repository.RepositoryType;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class NamespaceAndNameFromPathExtractorTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RepositoryManager repositoryManager;
|
||||||
|
|
||||||
|
private NamespaceAndNameFromPathExtractor extractor;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpObjectUnderTest() {
|
||||||
|
List<RepositoryType> types = Arrays.asList(
|
||||||
|
new RepositoryType("git", "Git", Collections.emptySet()),
|
||||||
|
new RepositoryType("hg", "Mercurial", Collections.emptySet()),
|
||||||
|
new RepositoryType("svn", "Subversion", Collections.emptySet())
|
||||||
|
);
|
||||||
|
when(repositoryManager.getConfiguredTypes()).thenReturn(types);
|
||||||
|
extractor = new NamespaceAndNameFromPathExtractor(repositoryManager);
|
||||||
|
}
|
||||||
|
|
||||||
public class NamespaceAndNameFromPathExtractorTest {
|
|
||||||
@TestFactory
|
@TestFactory
|
||||||
Stream<DynamicNode> shouldExtractCorrectNamespaceAndName() {
|
Stream<DynamicNode> shouldExtractCorrectNamespaceAndName() {
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
@@ -26,21 +57,26 @@ public class NamespaceAndNameFromPathExtractorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@TestFactory
|
@TestFactory
|
||||||
Stream<DynamicNode> shouldHandleTrailingDotSomethings() {
|
Stream<DynamicNode> shouldHandleTypeSuffix() {
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
"/space/repo.git",
|
"/space/repo.git",
|
||||||
"/space/repo.and.more",
|
"/space/repo.hg",
|
||||||
"/space/repo."
|
"/space/repo.svn",
|
||||||
|
"/space/repo"
|
||||||
).map(this::createCorrectTest);
|
).map(this::createCorrectTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DynamicTest createCorrectTest(String path) {
|
private DynamicTest createCorrectTest(String path) {
|
||||||
|
return createCorrectTest(path, new NamespaceAndName("space", "repo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private DynamicTest createCorrectTest(String path, NamespaceAndName expected) {
|
||||||
return dynamicTest(
|
return dynamicTest(
|
||||||
"should extract correct namespace and name for path " + path,
|
"should extract correct namespace and name for path " + path,
|
||||||
() -> {
|
() -> {
|
||||||
Optional<NamespaceAndName> namespaceAndName = NamespaceAndNameFromPathExtractor.fromUri(path);
|
Optional<NamespaceAndName> namespaceAndName = extractor.fromUri(path);
|
||||||
|
|
||||||
assertThat(namespaceAndName.get()).isEqualTo(new NamespaceAndName("space", "repo"));
|
assertThat(namespaceAndName.get()).isEqualTo(expected);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -59,10 +95,26 @@ public class NamespaceAndNameFromPathExtractorTest {
|
|||||||
return dynamicTest(
|
return dynamicTest(
|
||||||
"should not fail for wrong path " + path,
|
"should not fail for wrong path " + path,
|
||||||
() -> {
|
() -> {
|
||||||
Optional<NamespaceAndName> namespaceAndName = NamespaceAndNameFromPathExtractor.fromUri(path);
|
Optional<NamespaceAndName> namespaceAndName = extractor.fromUri(path);
|
||||||
|
|
||||||
assertThat(namespaceAndName.isPresent()).isFalse();
|
assertThat(namespaceAndName.isPresent()).isFalse();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
Stream<DynamicNode> shouldHandleDots() {
|
||||||
|
return Stream.of(
|
||||||
|
"/space/repo.with.dots.git",
|
||||||
|
"/space/repo.with.dots.hg",
|
||||||
|
"/space/repo.with.dots.svn",
|
||||||
|
"/space/repo.with.dots"
|
||||||
|
).map(path -> createCorrectTest(path, new NamespaceAndName("space", "repo.with.dots")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotFailOnEndingDot() {
|
||||||
|
Optional<NamespaceAndName> namespaceAndName = extractor.fromUri("/space/repo.");
|
||||||
|
assertThat(namespaceAndName).contains(new NamespaceAndName("space", "repo."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user