mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 03:55:51 +01:00
Add basic concept for scm protocol
This commit is contained in:
@@ -42,6 +42,7 @@ import sonia.scm.repository.spi.RepositoryServiceProvider;
|
|||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* From the {@link RepositoryService} it is possible to access all commands for
|
* From the {@link RepositoryService} it is possible to access all commands for
|
||||||
@@ -358,4 +359,16 @@ public final class RepositoryService implements Closeable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Set<ScmProtocol> getSupportedProtocols() {
|
||||||
|
return provider.getSupportedProtocols();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends ScmProtocol> T getProtocol(Class<T> clazz) {
|
||||||
|
for (ScmProtocol scmProtocol : getSupportedProtocols()) {
|
||||||
|
if (clazz.isAssignableFrom(scmProtocol.getClass())) {
|
||||||
|
return (T) scmProtocol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("no implementation for " + clazz.getName() + " and repository type " + getRepository().getType());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package sonia.scm.repository.api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An ScmProtocol represents a concrete protocol provided by the SCM-Manager instance
|
||||||
|
* to interact with a repository depending on its type. There may be multiple protocols
|
||||||
|
* available for a repository type (eg. http and ssh).
|
||||||
|
*/
|
||||||
|
public interface ScmProtocol {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the concrete protocol, eg. "http" or "ssh".
|
||||||
|
*/
|
||||||
|
String getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URL to access the repository providing this protocol.
|
||||||
|
*/
|
||||||
|
String getUrl();
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
|
import sonia.scm.repository.api.ScmProtocol;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public interface HttpScmProtocol extends ScmProtocol {
|
||||||
|
@Override
|
||||||
|
default String getType() {
|
||||||
|
return "http";
|
||||||
|
}
|
||||||
|
|
||||||
|
void serve(HttpServletRequest request, HttpServletResponse response);
|
||||||
|
}
|
||||||
@@ -38,15 +38,15 @@ package sonia.scm.repository.spi;
|
|||||||
import sonia.scm.repository.Feature;
|
import sonia.scm.repository.Feature;
|
||||||
import sonia.scm.repository.api.Command;
|
import sonia.scm.repository.api.Command;
|
||||||
import sonia.scm.repository.api.CommandNotSupportedException;
|
import sonia.scm.repository.api.CommandNotSupportedException;
|
||||||
|
import sonia.scm.repository.api.ScmProtocol;
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -244,4 +244,6 @@ public abstract class RepositoryServiceProvider implements Closeable
|
|||||||
{
|
{
|
||||||
throw new CommandNotSupportedException(Command.UNBUNDLE);
|
throw new CommandNotSupportedException(Command.UNBUNDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract Set<ScmProtocol> getSupportedProtocols();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package sonia.scm.repository.api;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.spi.HttpScmProtocol;
|
||||||
|
import sonia.scm.repository.spi.RepositoryServiceProvider;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
import static org.assertj.core.util.IterableUtil.sizeOf;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class RepositoryServiceTest {
|
||||||
|
|
||||||
|
private final RepositoryServiceProvider provider = mock(RepositoryServiceProvider.class);
|
||||||
|
private final Repository repository = mock(Repository.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnProtocolsFromProvider() {
|
||||||
|
when(provider.getSupportedProtocols()).thenReturn(Collections.singleton(new DummyHttpProtocol()));
|
||||||
|
|
||||||
|
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
|
||||||
|
Set<ScmProtocol> supportedProtocols = repositoryService.getSupportedProtocols();
|
||||||
|
|
||||||
|
assertThat(sizeOf(supportedProtocols)).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFindProtocolFromProvider() {
|
||||||
|
when(provider.getSupportedProtocols()).thenReturn(Collections.singleton(new DummyHttpProtocol()));
|
||||||
|
|
||||||
|
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
|
||||||
|
HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
|
||||||
|
|
||||||
|
assertThat(protocol.getUrl()).isEqualTo("dummy");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFailForUnknownProtocol() {
|
||||||
|
when(provider.getSupportedProtocols()).thenReturn(Collections.emptySet());
|
||||||
|
|
||||||
|
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
repositoryService.getProtocol(HttpScmProtocol.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class DummyHttpProtocol implements HttpScmProtocol {
|
||||||
|
@Override
|
||||||
|
public String getUrl() {
|
||||||
|
return "dummy";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serve(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,17 +36,17 @@ package sonia.scm.repository.spi;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
import sonia.scm.repository.GitRepositoryHandler;
|
import sonia.scm.repository.GitRepositoryHandler;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.api.Command;
|
import sonia.scm.repository.api.Command;
|
||||||
|
import sonia.scm.repository.api.ScmProtocol;
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -248,6 +248,12 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider
|
|||||||
return new GitTagsCommand(context, repository);
|
return new GitTagsCommand(context, repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<ScmProtocol> getSupportedProtocols() {
|
||||||
|
// TODO #9246
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
@@ -36,21 +36,21 @@ package sonia.scm.repository.spi;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.io.Closeables;
|
import com.google.common.io.Closeables;
|
||||||
|
|
||||||
import sonia.scm.repository.Feature;
|
import sonia.scm.repository.Feature;
|
||||||
import sonia.scm.repository.HgHookManager;
|
import sonia.scm.repository.HgHookManager;
|
||||||
import sonia.scm.repository.HgRepositoryHandler;
|
import sonia.scm.repository.HgRepositoryHandler;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.api.Command;
|
import sonia.scm.repository.api.Command;
|
||||||
|
import sonia.scm.repository.api.ScmProtocol;
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -273,6 +273,12 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
|
|||||||
return new HgTagsCommand(context, repository);
|
return new HgTagsCommand(context, repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<ScmProtocol> getSupportedProtocols() {
|
||||||
|
// TODO #9248
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
@@ -37,17 +37,17 @@ package sonia.scm.repository.spi;
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.io.Closeables;
|
import com.google.common.io.Closeables;
|
||||||
|
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.SvnRepositoryHandler;
|
import sonia.scm.repository.SvnRepositoryHandler;
|
||||||
import sonia.scm.repository.api.Command;
|
import sonia.scm.repository.api.Command;
|
||||||
|
import sonia.scm.repository.api.ScmProtocol;
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -191,6 +191,12 @@ public class SvnRepositoryServiceProvider extends RepositoryServiceProvider
|
|||||||
return new SvnUnbundleCommand(context, repository);
|
return new SvnUnbundleCommand(context, repository);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<ScmProtocol> getSupportedProtocols() {
|
||||||
|
// TODO #9247
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
Reference in New Issue
Block a user