Check on duplicate name for rename

Add check whether namespace and name already exist
when renaming a repository.
This commit is contained in:
Laura Gorzitze
2024-06-24 10:12:53 +02:00
parent 74ec91a95c
commit 38d0325b4c
5 changed files with 70 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.AlreadyExistsException;
import sonia.scm.ConfigurationException;
import sonia.scm.HandlerEventType;
import sonia.scm.ManagerDaoAdapter;
@@ -183,7 +184,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
},
newRepository -> {
if (repositoryDAO.contains(newRepository.getNamespaceAndName())) {
throw alreadyExists(entity(newRepository.getClass(), newRepository.getNamespaceAndName().logString()));
throw alreadyExists(entity(newRepository.getNamespaceAndName()));
}
}
);
@@ -292,10 +293,16 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
public Repository rename(Repository repository, String newNamespace, String newName) {
NamespaceAndName newNamespaceAndName = new NamespaceAndName(newNamespace, newName);
if (hasNamespaceOrNameNotChanged(repository, newNamespace, newName)) {
throw new NoChangesMadeException(repository);
}
if (this.get(newNamespaceAndName) != null){
throw AlreadyExistsException.alreadyExists(entity(NamespaceAndName.class, newNamespaceAndName.logString()));
}
Repository changedRepository = repository.clone();
if (!Strings.isNullOrEmpty(newName)) {
changedRepository.setName(newName);