Mark config entry stores explicitly in exports (#1545)

The default (XML) store of SCM-Manager does not distinguish between config and config entry stores in regards to
storage locations. Nonetheless, we want to make a difference in export files, so that other store providers can handle
these stores differently. To do so, this change adds an attribute to the top level xml element of config entry stores
to mark them. In exports, these store files can now be exported in a different folder. To mark existing stores, this
introduces an update step.
This commit is contained in:
René Pfeuffer
2021-02-23 09:37:59 +01:00
committed by GitHub
parent 83a9c90130
commit ee02ba096f
18 changed files with 555 additions and 47 deletions

View File

@@ -33,7 +33,7 @@ import static java.util.Optional.of;
class ExportableBlobFileStore extends ExportableDirectoryBasedFileStore {
static Function<StoreType, Optional<Function<Path, ExportableStore>>> BLOB_FACTORY =
static final Function<StoreType, Optional<Function<Path, ExportableStore>>> BLOB_FACTORY =
storeType -> storeType == StoreType.BLOB ? of(ExportableBlobFileStore::new) : empty();
ExportableBlobFileStore(Path directory) {

View File

@@ -0,0 +1,56 @@
/*
* 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.store;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static sonia.scm.store.ExportCopier.putFileContentIntoStream;
class ExportableConfigEntryFileStore implements ExportableStore {
private final Path file;
static final Function<StoreType, Optional<Function<Path, ExportableStore>>> CONFIG_ENTRY_FACTORY =
storeType -> storeType == StoreType.CONFIG_ENTRY ? of(ExportableConfigEntryFileStore::new) : empty();
ExportableConfigEntryFileStore(Path file) {
this.file = file;
}
@Override
public StoreEntryMetaData getMetaData() {
return new StoreEntryMetaData(StoreType.CONFIG_ENTRY, file.getFileName().toString());
}
@Override
public void export(Exporter exporter) throws IOException {
putFileContentIntoStream(exporter, file);
}
}

View File

@@ -37,7 +37,7 @@ class ExportableConfigFileStore implements ExportableStore {
private final Path file;
static Function<StoreType, Optional<Function<Path, ExportableStore>>> CONFIG_FACTORY =
static final Function<StoreType, Optional<Function<Path, ExportableStore>>> CONFIG_FACTORY =
storeType -> storeType == StoreType.CONFIG ? of(ExportableConfigFileStore::new) : empty();
ExportableConfigFileStore(Path file) {
@@ -51,8 +51,6 @@ class ExportableConfigFileStore implements ExportableStore {
@Override
public void export(Exporter exporter) throws IOException {
if (file.getFileName().toString().endsWith(".xml")) {
putFileContentIntoStream(exporter, file);
}
putFileContentIntoStream(exporter, file);
}
}

View File

@@ -33,7 +33,7 @@ import static java.util.Optional.of;
class ExportableDataFileStore extends ExportableDirectoryBasedFileStore {
static Function<StoreType, Optional<Function<Path, ExportableStore>>> DATA_FACTORY =
static final Function<StoreType, Optional<Function<Path, ExportableStore>>> DATA_FACTORY =
storeType -> storeType == StoreType.DATA ? of(ExportableDataFileStore::new) : empty();
ExportableDataFileStore(Path directory) {

View File

@@ -24,11 +24,16 @@
package sonia.scm.store;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.repository.api.ExportFailedException;
import sonia.scm.xml.XmlStreams;
import javax.inject.Inject;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -43,15 +48,18 @@ import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static sonia.scm.ContextEntry.ContextBuilder.noContext;
import static sonia.scm.store.ExportableBlobFileStore.BLOB_FACTORY;
import static sonia.scm.store.ExportableConfigEntryFileStore.CONFIG_ENTRY_FACTORY;
import static sonia.scm.store.ExportableConfigFileStore.CONFIG_FACTORY;
import static sonia.scm.store.ExportableDataFileStore.DATA_FACTORY;
public class FileStoreExporter implements StoreExporter {
private final RepositoryLocationResolver locationResolver;
private static final Collection<Function<StoreType, Optional<Function<Path, ExportableStore>>>> STORE_FACTORIES =
asList(DATA_FACTORY, BLOB_FACTORY, CONFIG_FACTORY);
asList(DATA_FACTORY, BLOB_FACTORY, CONFIG_FACTORY, CONFIG_ENTRY_FACTORY);
private static final Logger LOG = LoggerFactory.getLogger(FileStoreExporter.class);
private final RepositoryLocationResolver locationResolver;
@Inject
public FileStoreExporter(RepositoryLocationResolver locationResolver) {
@@ -103,19 +111,45 @@ public class FileStoreExporter implements StoreExporter {
private Optional<ExportableStore> getStoreFor(Path storePath) {
return STORE_FACTORIES
.stream()
.map(factory -> factory.apply(getEnumForValue(storePath.getParent())))
.map(factory -> factory.apply(getEnumForValue(storePath)))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.map(f -> f.apply(storePath));
}
private StoreType getEnumForValue(Path storeTypeDirectory) {
for (StoreType type : StoreType.values()) {
if (type.getValue().equals(storeTypeDirectory.getFileName().toString())) {
return type;
private StoreType getEnumForValue(Path storePath) {
if (Files.isDirectory(storePath)) {
for (StoreType type : StoreType.values()) {
if (type.getValue().equals(storePath.getParent().getFileName().toString())) {
return type;
}
}
} else if (storePath.toString().endsWith(".xml")) {
return determineConfigType(storePath);
} else {
LOG.info("ignoring unknown file '{}' in export", storePath);
}
return null;
}
private StoreType determineConfigType(Path storePath) {
XMLStreamReader reader = null;
try {
reader = XmlStreams.createReader(storePath);
reader.nextTag();
if (
"configuration".equals(reader.getLocalName())
&& "config-entry".equals(reader.getAttributeValue(0))
&& "type".equals(reader.getAttributeName(0).getLocalPart())) {
return StoreType.CONFIG_ENTRY;
} else {
return StoreType.CONFIG;
}
} catch (XMLStreamException | IOException e) {
throw new ExportFailedException(noContext(), "Failed to read store file " + storePath, e);
} finally {
XmlStreams.close(reader);
}
}
}

View File

@@ -195,7 +195,7 @@ public class JAXBConfigurationEntryStore<V> implements ConfigurationEntryStore<V
// configuration start
writer.writeStartElement(TAG_CONFIGURATION);
writer.writeAttribute("type", "config-entry");
for (Entry<String, V> e : entries.entrySet()) {

View File

@@ -91,16 +91,6 @@ class ExportableFileStoreTest {
assertThat(os.toString()).isNotBlank();
}
@Test
void shouldFilterNoneConfigFiles(@TempDir Path temp) throws IOException {
createFile(temp, "config", "", "first.bck");
ExportableStore exportableConfigFileStore = new ExportableConfigFileStore(temp.resolve("config").resolve("first.bck"));
exportableConfigFileStore.export(exporter);
verify(exporter, never()).put(anyString(), anyLong());
}
@Test
void shouldPutContentIntoExporterForBlobStore(@TempDir Path temp) throws IOException {
createFile(temp, "blob", "assets", "first.blob");

View File

@@ -24,6 +24,7 @@
package sonia.scm.store;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
@@ -38,7 +39,8 @@ import sonia.scm.repository.RepositoryTestData;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@@ -55,6 +57,14 @@ class FileStoreExporterTest {
@InjectMocks
private FileStoreExporter fileStoreExporter;
private Path storePath;
@BeforeEach
void setUpStorePath(@TempDir Path temp) {
storePath = temp.resolve("store");
when(resolver.forClass(Path.class).getLocation(REPOSITORY.getId())).thenReturn(temp);
}
@Test
void shouldReturnEmptyList(@TempDir Path temp) {
when(resolver.forClass(Path.class).getLocation(REPOSITORY.getId())).thenReturn(temp);
@@ -64,27 +74,57 @@ class FileStoreExporterTest {
}
@Test
void shouldReturnListOfExportableStores(@TempDir Path temp) throws IOException {
Path storePath = temp.resolve("store");
createFile(storePath, StoreType.CONFIG.getValue(), null, "first.xml");
createFile(storePath, StoreType.DATA.getValue(), "ci", "second.xml");
createFile(storePath, StoreType.DATA.getValue(), "jenkins", "third.xml");
when(resolver.forClass(Path.class).getLocation(REPOSITORY.getId())).thenReturn(temp);
void shouldReturnConfigStores() throws IOException {
createFile(StoreType.CONFIG.getValue(), "config.xml")
.withContent("<?xml version=\"1.0\" ?>", "<data>", "some arbitrary content", "</data>");
List<ExportableStore> exportableStores = fileStoreExporter.listExportableStores(REPOSITORY);
assertThat(exportableStores).hasSize(3);
assertThat(exportableStores).hasSize(1);
assertThat(exportableStores.stream().filter(e -> e.getMetaData().getType().equals(StoreType.CONFIG))).hasSize(1);
}
@Test
void shouldReturnConfigEntryStores() throws IOException {
createFile(StoreType.CONFIG.getValue(), "config-entry.xml")
.withContent("<?xml version=\"1.0\" ?>", "<configuration type=\"config-entry\">", "</configuration>");
List<ExportableStore> exportableStores = fileStoreExporter.listExportableStores(REPOSITORY);
assertThat(exportableStores).hasSize(1);
assertThat(exportableStores.stream().filter(e -> e.getMetaData().getType().equals(StoreType.CONFIG_ENTRY))).hasSize(1);
}
@Test
void shouldReturnDataStores() throws IOException {
createFile(StoreType.DATA.getValue(), "ci", "data.xml");
createFile(StoreType.DATA.getValue(), "jenkins", "data.xml");
List<ExportableStore> exportableStores = fileStoreExporter.listExportableStores(REPOSITORY);
assertThat(exportableStores).hasSize(2);
assertThat(exportableStores.stream().filter(e -> e.getMetaData().getType().equals(StoreType.DATA))).hasSize(2);
}
private void createFile(Path storePath, String type, String name, String fileName) throws IOException {
Path path = name != null ? storePath.resolve(type).resolve(name) : storePath.resolve(type);
Files.createDirectories(path);
Path file = path.resolve(fileName);
private FileWriter createFile(String... names) throws IOException {
Path file = Arrays.stream(names).map(Paths::get).reduce(Path::resolve).map(storePath::resolve).orElse(storePath);
Files.createDirectories(file.getParent());
if (!Files.exists(file)) {
Files.createFile(file);
}
Files.write(file, Collections.singletonList("something"));
return new FileWriter(file);
}
private static class FileWriter {
private final Path file;
private FileWriter(Path file) {
this.file = file;
}
void withContent(String... content) throws IOException {
Files.write(file, Arrays.asList(content));
}
}
}