Use manually entered namespace and name

This commit is contained in:
René Pfeuffer
2019-06-11 13:10:31 +02:00
parent fdb8143b76
commit 802fb3e0cf
9 changed files with 427 additions and 195 deletions

View File

@@ -3,12 +3,14 @@ package sonia.scm.update;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.boot.RestartEvent;
import sonia.scm.event.ScmEventBus;
import sonia.scm.update.repository.MigrationStrategy;
import sonia.scm.update.repository.MigrationStrategyDao;
import sonia.scm.update.repository.V1Repository;
import sonia.scm.update.repository.XmlRepositoryV1UpdateStep;
import sonia.scm.util.ValidationUtil;
@@ -19,6 +21,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -62,9 +65,7 @@ class MigrationWizardServlet extends HttpServlet {
.stream()
.anyMatch(entry -> entry.isNamespaceInvalid() || entry.isNameInvalid()));
MustacheFactory mf = new DefaultMustacheFactory();
Mustache template = mf.compile("templates/repository-migration.mustache");
respondWithTemplate(resp, model, template);
respondWithTemplate(resp, model, "templates/repository-migration.mustache");
}
@Override
@@ -76,6 +77,7 @@ class MigrationWizardServlet extends HttpServlet {
String id = repositoryLineEntry.getId();
String namespace = req.getParameter("namespace-" + id);
String name = req.getParameter("name-" + id);
String strategy = req.getParameter("strategy-" + id);
repositoryLineEntry.setNamespace(namespace);
repositoryLineEntry.setName(name);
@@ -105,19 +107,15 @@ class MigrationWizardServlet extends HttpServlet {
}
);
MustacheFactory mf = new DefaultMustacheFactory();
Mustache template = mf.compile("templates/repository-migration-restart.mustache");
Map<String, Object> model = Collections.singletonMap("contextPath", req.getContextPath());
respondWithTemplate(resp, model, template);
resp.setStatus(200);
respondWithTemplate(resp, model, "templates/repository-migration-restart.mustache");
ScmEventBus.getInstance().post(new RestartEvent(MigrationWizardServlet.class, "wrote migration data"));
}
private List<RepositoryLineEntry> getRepositoryLineEntries() {
List<XmlRepositoryV1UpdateStep.V1Repository> repositoriesWithoutMigrationStrategies =
List<V1Repository> repositoriesWithoutMigrationStrategies =
repositoryV1UpdateStep.getRepositoriesWithoutMigrationStrategies();
return repositoriesWithoutMigrationStrategies.stream()
.map(RepositoryLineEntry::new)
@@ -129,7 +127,11 @@ class MigrationWizardServlet extends HttpServlet {
return MigrationStrategy.values();
}
private void respondWithTemplate(HttpServletResponse resp, Map<String, Object> model, Mustache template) {
@VisibleForTesting
void respondWithTemplate(HttpServletResponse resp, Map<String, Object> model, String templateName) {
MustacheFactory mf = new DefaultMustacheFactory();
Mustache template = mf.compile(templateName);
PrintWriter writer;
try {
writer = resp.getWriter();
@@ -152,12 +154,30 @@ class MigrationWizardServlet extends HttpServlet {
private boolean namespaceValid = true;
private boolean nameValid = true;
public RepositoryLineEntry(XmlRepositoryV1UpdateStep.V1Repository repository) {
public RepositoryLineEntry(V1Repository repository) {
this.id = repository.getId();
this.type = repository.getType();
this.path = repository.getPath();
this.namespace = repository.getNewNamespace();
this.name = repository.getNewName();
this.path = repository.getType() + "/" + repository.getName();
this.namespace = computeNewNamespace(repository);
this.name = computeNewName(repository);
}
private static String computeNewNamespace(V1Repository v1Repository) {
String[] nameParts = getNameParts(v1Repository.getName());
return nameParts.length > 1 ? nameParts[0] : v1Repository.getType();
}
private static String computeNewName(V1Repository v1Repository) {
String[] nameParts = getNameParts(v1Repository.getName());
return nameParts.length == 1 ? nameParts[0] : concatPathElements(nameParts);
}
private static String[] getNameParts(String v1Name) {
return v1Name.split("/");
}
private static String concatPathElements(String[] nameParts) {
return Arrays.stream(nameParts).skip(1).collect(Collectors.joining("_"));
}
public String getId() {

View File

@@ -19,7 +19,7 @@ public class MigrationStrategyDao {
this.plan = store.getOptional().orElse(new RepositoryMigrationPlan());
}
public Optional<MigrationStrategy> get(String id) {
public Optional<RepositoryMigrationPlan.RepositoryMigrationEntry> get(String id) {
return plan.get(id);
}

View File

@@ -13,53 +13,50 @@ import static java.util.Arrays.asList;
@XmlRootElement(name = "repository-migration")
class RepositoryMigrationPlan {
private List<RepositoryEntry> entries;
private List<RepositoryMigrationEntry> entries;
RepositoryMigrationPlan() {
this(new RepositoryEntry[0]);
this(new RepositoryMigrationEntry[0]);
}
RepositoryMigrationPlan(RepositoryEntry... entries) {
RepositoryMigrationPlan(RepositoryMigrationEntry... entries) {
this.entries = new ArrayList<>(asList(entries));
}
Optional<MigrationStrategy> get(String repositoryId) {
return findEntry(repositoryId)
.map(RepositoryEntry::getDataMigrationStrategy);
}
public void set(String repositoryId, MigrationStrategy strategy, String newNamespace, String newName) {
Optional<RepositoryEntry> entry = findEntry(repositoryId);
if (entry.isPresent()) {
entry.get().setStrategy(strategy);
entry.get().setNewNamespace(newNamespace);
entry.get().setNewName(newName);
} else {
entries.add(new RepositoryEntry(repositoryId, strategy));
}
}
private Optional<RepositoryEntry> findEntry(String repositoryId) {
Optional<RepositoryMigrationEntry> get(String repositoryId) {
return entries.stream()
.filter(repositoryEntry -> repositoryId.equals(repositoryEntry.repositoryId))
.findFirst();
}
public void set(String repositoryId, MigrationStrategy strategy, String newNamespace, String newName) {
Optional<RepositoryMigrationEntry> entry = get(repositoryId);
if (entry.isPresent()) {
entry.get().setStrategy(strategy);
entry.get().setNewNamespace(newNamespace);
entry.get().setNewName(newName);
} else {
entries.add(new RepositoryMigrationEntry(repositoryId, strategy, newNamespace, newName));
}
}
@XmlRootElement(name = "entries")
@XmlAccessorType(XmlAccessType.FIELD)
static class RepositoryEntry {
static class RepositoryMigrationEntry {
private String repositoryId;
private MigrationStrategy dataMigrationStrategy;
private String newNamespace;
private String newName;
RepositoryEntry() {
RepositoryMigrationEntry() {
}
RepositoryEntry(String repositoryId, MigrationStrategy dataMigrationStrategy) {
RepositoryMigrationEntry(String repositoryId, MigrationStrategy dataMigrationStrategy, String newNamespace, String newName) {
this.repositoryId = repositoryId;
this.dataMigrationStrategy = dataMigrationStrategy;
this.newNamespace = newNamespace;
this.newName = newName;
}
public MigrationStrategy getDataMigrationStrategy() {

View File

@@ -0,0 +1,25 @@
package sonia.scm.update.repository;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "permissions")
class V1Permission {
private boolean groupPermission;
private String name;
private String type;
public boolean isGroupPermission() {
return groupPermission;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
}

View File

@@ -0,0 +1,92 @@
package sonia.scm.update.repository;
import sonia.scm.update.properties.V1Properties;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "repositories")
public class V1Repository {
private String contact;
private long creationDate;
private Long lastModified;
private String description;
private String id;
private String name;
private boolean isPublic;
private boolean archived;
private String type;
private List<V1Permission> permissions;
private V1Properties properties;
public V1Repository() {
}
public V1Repository(String id, String type, String name) {
this.id = id;
this.type = type;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getContact() {
return contact;
}
public long getCreationDate() {
return creationDate;
}
public Long getLastModified() {
return lastModified;
}
public String getDescription() {
return description;
}
public boolean isPublic() {
return isPublic;
}
public boolean isArchived() {
return archived;
}
public List<V1Permission> getPermissions() {
return permissions;
}
public V1Properties getProperties() {
return properties;
}
@Override
public String toString() {
return "V1Repository{" +
", contact='" + contact + '\'' +
", creationDate=" + creationDate +
", lastModified=" + lastModified +
", description='" + description + '\'' +
", id='" + id + '\'' +
", name='" + name + '\'' +
", isPublic=" + isPublic +
", archived=" + archived +
", type='" + type + '\'' +
'}';
}
}

View File

@@ -28,7 +28,6 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -104,7 +103,7 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
JAXBContext jaxbContext = JAXBContext.newInstance(V1RepositoryDatabase.class);
readV1Database(jaxbContext).ifPresent(
v1Database -> {
v1Database.repositoryList.repositories.forEach(this::readMigrationStrategy);
v1Database.repositoryList.repositories.forEach(this::readMigrationEntry);
v1Database.repositoryList.repositories.forEach(this::update);
backupOldRepositoriesFile();
}
@@ -141,52 +140,56 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
}
private void update(V1Repository v1Repository) {
Optional<Path> destination = handleDataDirectory(v1Repository);
RepositoryMigrationPlan.RepositoryMigrationEntry repositoryMigrationEntry = readMigrationEntry(v1Repository);
Optional<Path> destination = handleDataDirectory(v1Repository, repositoryMigrationEntry.getDataMigrationStrategy());
LOG.info("using strategy {} to migrate repository {} with id {} using new namespace {} and name {}",
repositoryMigrationEntry.getDataMigrationStrategy().getClass(),
v1Repository.getName(),
v1Repository.getId(),
repositoryMigrationEntry.getNewNamespace(),
repositoryMigrationEntry.getNewName());
destination.ifPresent(
newPath -> {
Repository repository = new Repository(
v1Repository.id,
v1Repository.type,
v1Repository.getNewNamespace(),
v1Repository.getNewName(),
v1Repository.contact,
v1Repository.description,
v1Repository.getId(),
v1Repository.getType(),
repositoryMigrationEntry.getNewNamespace(),
repositoryMigrationEntry.getNewName(),
v1Repository.getContact(),
v1Repository.getDescription(),
createPermissions(v1Repository));
LOG.info("creating new repository {} with id {} from old repository {} in directory {}", repository.getNamespaceAndName(), repository.getId(), v1Repository.name, newPath);
LOG.info("creating new repository {} with id {} from old repository {} in directory {}", repository.getNamespaceAndName(), repository.getId(), v1Repository.getName(), newPath);
repositoryDao.add(repository, newPath);
propertyStore.put(v1Repository.id, v1Repository.properties);
propertyStore.put(v1Repository.getId(), v1Repository.getProperties());
}
);
}
private Optional<Path> handleDataDirectory(V1Repository v1Repository) {
MigrationStrategy dataMigrationStrategy = readMigrationStrategy(v1Repository);
LOG.info("using strategy {} to migrate repository {} with id {}", dataMigrationStrategy.getClass(), v1Repository.name, v1Repository.id);
return dataMigrationStrategy.from(injector).migrate(v1Repository.id, v1Repository.name, v1Repository.type);
private Optional<Path> handleDataDirectory(V1Repository v1Repository, MigrationStrategy dataMigrationStrategy) {
return dataMigrationStrategy
.from(injector)
.migrate(v1Repository.getId(), v1Repository.getName(), v1Repository.getType());
}
private MigrationStrategy readMigrationStrategy(V1Repository v1Repository) {
private RepositoryMigrationPlan.RepositoryMigrationEntry readMigrationEntry(V1Repository v1Repository) {
return findMigrationStrategy(v1Repository)
.orElseThrow(() -> new IllegalStateException("no strategy found for repository with id " + v1Repository.id + " and name " + v1Repository.name));
.orElseThrow(() -> new IllegalStateException("no strategy found for repository with id " + v1Repository.getId() + " and name " + v1Repository.getName()));
}
private Optional<MigrationStrategy> findMigrationStrategy(V1Repository v1Repository) {
return migrationStrategyDao.get(v1Repository.id);
private Optional<RepositoryMigrationPlan.RepositoryMigrationEntry> findMigrationStrategy(V1Repository v1Repository) {
return migrationStrategyDao.get(v1Repository.getId());
}
private RepositoryPermission[] createPermissions(V1Repository v1Repository) {
if (v1Repository.permissions == null) {
return new RepositoryPermission[0];
}
return v1Repository.permissions
return v1Repository.getPermissions()
.stream()
.map(this::createPermission)
.toArray(RepositoryPermission[]::new);
}
private RepositoryPermission createPermission(V1Permission v1Permission) {
LOG.info("creating permission {} for {}", v1Permission.type, v1Permission.name);
return new RepositoryPermission(v1Permission.name, v1Permission.type, v1Permission.groupPermission);
LOG.info("creating permission {} for {}", v1Permission.getType(), v1Permission.getName());
return new RepositoryPermission(v1Permission.getName(), v1Permission.getType(), v1Permission.isGroupPermission());
}
private Optional<V1RepositoryDatabase> readV1Database(JAXBContext jaxbContext) throws JAXBException {
@@ -205,79 +208,6 @@ public class XmlRepositoryV1UpdateStep implements UpdateStep {
).toFile();
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "permissions")
private static class V1Permission {
private boolean groupPermission;
private String name;
private String type;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "repositories")
public static class V1Repository {
private String contact;
private long creationDate;
private Long lastModified;
private String description;
private String id;
private String name;
private boolean isPublic;
private boolean archived;
private String type;
private List<V1Permission> permissions;
private V1Properties properties;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getPath() {
return type + "/" + name;
}
public String getNewNamespace() {
String[] nameParts = getNameParts(name);
return nameParts.length > 1 ? nameParts[0] : type;
}
public String getNewName() {
String[] nameParts = getNameParts(name);
return nameParts.length == 1 ? nameParts[0] : concatPathElements(nameParts);
}
private String[] getNameParts(String v1Name) {
return v1Name.split("/");
}
private String concatPathElements(String[] nameParts) {
return Arrays.stream(nameParts).skip(1).collect(Collectors.joining("_"));
}
@Override
public String toString() {
return "V1Repository{" +
", contact='" + contact + '\'' +
", creationDate=" + creationDate +
", lastModified=" + lastModified +
", description='" + description + '\'' +
", id='" + id + '\'' +
", name='" + name + '\'' +
", isPublic=" + isPublic +
", archived=" + archived +
", type='" + type + '\'' +
'}';
}
}
private static class RepositoryList {
@XmlElement(name = "repository")
private List<V1Repository> repositories;