mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
simplify core api
This commit is contained in:
@@ -24,14 +24,11 @@
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.google.common.io.ByteSource;
|
||||
import sonia.scm.plugin.ExtensionPoint;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -47,15 +44,6 @@ public interface RepositoryContentInitializer {
|
||||
*/
|
||||
void initialize(InitializerContext context) throws IOException;
|
||||
|
||||
/**
|
||||
* returns the class to which the creation context will be mapped
|
||||
*
|
||||
* @return the class of the creation context
|
||||
*/
|
||||
default Optional<Class<?>> getType() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this {@link InitializerContext} to create new files on repository initialization
|
||||
* which will be included in the first commit
|
||||
@@ -76,10 +64,15 @@ public interface RepositoryContentInitializer {
|
||||
CreateFile create(String path);
|
||||
|
||||
/**
|
||||
* @return creation context of repository which is going to be initialized
|
||||
* Find the the context object with the given key and unmarshalls it to the given type.
|
||||
*
|
||||
* @param key key of the context object
|
||||
* @param type type of the context object
|
||||
* @return context object or empty optional
|
||||
* @since 2.5.0
|
||||
*/
|
||||
default Map<String, JsonNode> getCreationContext() {
|
||||
return Collections.emptyMap();
|
||||
default <T> Optional<T> getCreationContext(String key, Class<T> type) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -258,7 +258,7 @@ class RepositoryForm extends React.Component<Props, State> {
|
||||
helpText={t("help.initializeRepository")}
|
||||
/>
|
||||
{this.state.initRepository && (
|
||||
<ExtensionPoint name="repos.create.initialize" props={extensionProps} renderAll={false} />
|
||||
<ExtensionPoint name="repos.create.initialize" props={extensionProps} renderAll={true} />
|
||||
)}
|
||||
</CheckboxWrapper>
|
||||
</SpaceBetween>
|
||||
|
||||
@@ -59,8 +59,7 @@ type Props = WithTranslation & {
|
||||
link: string,
|
||||
repository: RepositoryCreation,
|
||||
initRepository: boolean,
|
||||
callback: (repo: Repository) => void,
|
||||
initRepositoryContext?: any
|
||||
callback: (repo: Repository) => void
|
||||
) => void;
|
||||
resetForm: () => void;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.CharSource;
|
||||
import org.slf4j.Logger;
|
||||
@@ -40,6 +41,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Singleton
|
||||
@@ -56,10 +58,6 @@ public class RepositoryInitializer {
|
||||
this.contentInitializers = Priorities.sortInstances(contentInitializerSet);
|
||||
}
|
||||
|
||||
public void initialize(Repository repository) {
|
||||
initialize(repository, null);
|
||||
}
|
||||
|
||||
public void initialize(Repository repository, Map<String, JsonNode> creationContext) {
|
||||
try (RepositoryService service = serviceFactory.create(repository)) {
|
||||
ModifyCommandBuilder modifyCommandBuilder = service.getModifyCommand();
|
||||
@@ -85,6 +83,8 @@ public class RepositoryInitializer {
|
||||
private final ModifyCommandBuilder builder;
|
||||
private final Map<String, JsonNode> creationContext;
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
InitializerContextImpl(Repository repository, ModifyCommandBuilder builder, Map<String, JsonNode> creationContext) {
|
||||
this.repository = repository;
|
||||
this.builder = builder;
|
||||
@@ -97,8 +97,8 @@ public class RepositoryInitializer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, JsonNode> getCreationContext() {
|
||||
return creationContext;
|
||||
public <T> Optional<T> getCreationContext(String key, Class<T> type) {
|
||||
return Optional.of(mapper.convertValue(creationContext.get(key), type));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -134,5 +134,4 @@ public class RepositoryInitializer {
|
||||
return initializerContext;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyObject;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
@@ -315,7 +316,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
|
||||
assertEquals("/v2/repositories/otherspace/repo", response.getOutputHeaders().get("Location").get(0).toString());
|
||||
verify(repositoryManager).create(any(Repository.class));
|
||||
verify(repositoryInitializer, never()).initialize(any(Repository.class));
|
||||
verify(repositoryInitializer, never()).initialize(any(Repository.class), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -336,7 +337,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
|
||||
|
||||
ArgumentCaptor<Repository> captor = ArgumentCaptor.forClass(Repository.class);
|
||||
verify(repositoryInitializer).initialize(captor.capture());
|
||||
verify(repositoryInitializer).initialize(captor.capture(), anyMap());
|
||||
|
||||
Repository repository = captor.getValue();
|
||||
assertEquals("space", repository.getNamespace());
|
||||
|
||||
@@ -29,7 +29,6 @@ import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -44,6 +43,7 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@@ -88,7 +88,7 @@ class RepositoryInitializerTest {
|
||||
);
|
||||
|
||||
RepositoryInitializer initializer = new RepositoryInitializer(repositoryServiceFactory, repositoryContentInitializers);
|
||||
initializer.initialize(repository);
|
||||
initializer.initialize(repository, Collections.emptyMap());
|
||||
|
||||
verifyFileCreation(readmeContentLoader, "# HeartOfGold");
|
||||
verifyFileCreation(licenseContentLoader, "MIT");
|
||||
@@ -108,7 +108,7 @@ class RepositoryInitializerTest {
|
||||
);
|
||||
|
||||
RepositoryInitializer initializer = new RepositoryInitializer(repositoryServiceFactory, repositoryContentInitializers);
|
||||
initializer.initialize(repository);
|
||||
initializer.initialize(repository, Collections.emptyMap());
|
||||
|
||||
verifyFileCreationWithStream(contentLoader, "awesome");
|
||||
|
||||
@@ -138,7 +138,7 @@ class RepositoryInitializerTest {
|
||||
);
|
||||
|
||||
RepositoryInitializer initializer = new RepositoryInitializer(repositoryServiceFactory, repositoryContentInitializers);
|
||||
initializer.initialize(repository);
|
||||
initializer.initialize(repository, Collections.emptyMap());
|
||||
|
||||
assertThat(reference.get()).isEqualTo("MIT");
|
||||
}
|
||||
@@ -149,7 +149,7 @@ class RepositoryInitializerTest {
|
||||
doThrow(new IOException("epic fail")).when(contentLoader).withData(any(ByteSource.class));
|
||||
|
||||
RepositoryInitializer initializer = new RepositoryInitializer(repositoryServiceFactory, ImmutableSet.of(new ReadmeContentInitializer()));
|
||||
assertThrows(InternalRepositoryException.class, () -> initializer.initialize(repository));
|
||||
assertThrows(InternalRepositoryException.class, () -> initializer.initialize(repository, Collections.emptyMap()));
|
||||
|
||||
verify(repositoryService).close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user