mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-06 14:49:32 +02:00
Merge pull request #1324 from scm-manager/feature/template_plugin
Feature/template plugin
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.mapstruct.AfterMapping;
|
||||
@@ -31,15 +31,21 @@ import org.mapstruct.MappingTarget;
|
||||
import org.slf4j.MDC;
|
||||
import sonia.scm.ExceptionWithContext;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Mapper
|
||||
public abstract class ExceptionWithContextToErrorDtoMapper {
|
||||
|
||||
@Mapping(target = "errorCode", source = "code")
|
||||
@Mapping(target = "transactionId", ignore = true)
|
||||
@Mapping(target = "violations", ignore = true)
|
||||
@Mapping(target = "url", ignore = true)
|
||||
public abstract ErrorDto map(ExceptionWithContext exception);
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") // is ok for mapping
|
||||
public String mapOptional(Optional<String> optionalString) {
|
||||
return optionalString.orElse(null);
|
||||
}
|
||||
|
||||
@AfterMapping
|
||||
void setTransactionId(@MappingTarget ErrorDto dto) {
|
||||
dto.setTransactionId(MDC.get("transaction_id"));
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -147,7 +147,7 @@ public class RepositoryCollectionResource {
|
||||
mediaType = VndMediaType.ERROR_TYPE,
|
||||
schema = @Schema(implementation = ErrorDto.class)
|
||||
))
|
||||
public Response create(@Valid RepositoryDto repository, @QueryParam("initialize") boolean initialize) {
|
||||
public Response create(@Valid RepositoryCreationDto repository, @QueryParam("initialize") boolean initialize) {
|
||||
AtomicReference<Repository> reference = new AtomicReference<>();
|
||||
Response response = adapter.create(repository,
|
||||
() -> createModelObjectFromDto(repository),
|
||||
@@ -156,7 +156,7 @@ public class RepositoryCollectionResource {
|
||||
return resourceLinks.repository().self(r.getNamespace(), r.getName());
|
||||
});
|
||||
if (initialize) {
|
||||
repositoryInitializer.initialize(reference.get());
|
||||
repositoryInitializer.initialize(reference.get(), repository.getContextEntries());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class RepositoryCreationDto extends RepositoryDto {
|
||||
private Map<String, JsonNode> contextEntries;
|
||||
|
||||
public Map<String, JsonNode> getContextEntries() {
|
||||
if (contextEntries == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return contextEntries;
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,11 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
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;
|
||||
@@ -38,6 +40,8 @@ import javax.inject.Singleton;
|
||||
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
|
||||
@@ -54,11 +58,11 @@ public class RepositoryInitializer {
|
||||
this.contentInitializers = Priorities.sortInstances(contentInitializerSet);
|
||||
}
|
||||
|
||||
public void initialize(Repository repository) {
|
||||
public void initialize(Repository repository, Map<String, JsonNode> contextEntries) {
|
||||
try (RepositoryService service = serviceFactory.create(repository)) {
|
||||
ModifyCommandBuilder modifyCommandBuilder = service.getModifyCommand();
|
||||
|
||||
InitializerContextImpl initializerContext = new InitializerContextImpl(repository, modifyCommandBuilder);
|
||||
InitializerContextImpl initializerContext = new InitializerContextImpl(repository, modifyCommandBuilder, contextEntries);
|
||||
|
||||
for (RepositoryContentInitializer initializer : contentInitializers) {
|
||||
initializer.initialize(initializerContext);
|
||||
@@ -77,10 +81,14 @@ public class RepositoryInitializer {
|
||||
|
||||
private final Repository repository;
|
||||
private final ModifyCommandBuilder builder;
|
||||
private final Map<String, JsonNode> contextEntries;
|
||||
|
||||
InitializerContextImpl(Repository repository, ModifyCommandBuilder builder) {
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
InitializerContextImpl(Repository repository, ModifyCommandBuilder builder, Map<String, JsonNode> contextEntries) {
|
||||
this.repository = repository;
|
||||
this.builder = builder;
|
||||
this.contextEntries = contextEntries;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,6 +96,15 @@ public class RepositoryInitializer {
|
||||
return repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Optional<T> getEntry(String key, Class<T> type) {
|
||||
JsonNode node = contextEntries.get(key);
|
||||
if (node != null) {
|
||||
return Optional.of(mapper.convertValue(node, type));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RepositoryContentInitializer.CreateFile create(String path) {
|
||||
return new CreateFileImpl(this, builder.useDefaultPath(true).createFile(path).setOverwrite(true));
|
||||
@@ -121,5 +138,4 @@ public class RepositoryInitializer {
|
||||
return initializerContext;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user