Check preconditions when setting parent project through REST
Only update the parent project if the new parent project exists and if setting this parent doesn't introduce a cycle in the line of projects. Never set a parent project for the All-Projects root project. Change-Id: I9caec6921d46f8205febdacb31f52bd6c21f28c8 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -15,13 +15,16 @@
|
||||
package com.google.gerrit.server.project;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||
import com.google.gerrit.extensions.restapi.DefaultInput;
|
||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.config.AllProjectsName;
|
||||
@@ -33,6 +36,8 @@ import com.google.inject.Inject;
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class SetParent implements RestModifyView<ProjectResource, Input> {
|
||||
static class Input {
|
||||
@DefaultInput
|
||||
@@ -54,21 +59,45 @@ class SetParent implements RestModifyView<ProjectResource, Input> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(ProjectResource resource, Input input)
|
||||
throws AuthException, BadRequestException, ResourceConflictException,
|
||||
Exception {
|
||||
ProjectControl ctl = resource.getControl();
|
||||
public String apply(final ProjectResource rsrc, Input input) throws AuthException,
|
||||
BadRequestException, ResourceConflictException,
|
||||
ResourceNotFoundException, UnprocessableEntityException, IOException {
|
||||
ProjectControl ctl = rsrc.getControl();
|
||||
IdentifiedUser user = (IdentifiedUser) ctl.getCurrentUser();
|
||||
if (!user.getCapabilities().canAdministrateServer()) {
|
||||
throw new AuthException("not administrator");
|
||||
}
|
||||
|
||||
if (rsrc.getNameKey().equals(allProjects)) {
|
||||
throw new ResourceConflictException("cannot set parent of "
|
||||
+ allProjects.get());
|
||||
}
|
||||
|
||||
input.parent = Strings.emptyToNull(input.parent);
|
||||
if (input.parent != null) {
|
||||
ProjectState parent = cache.get(new Project.NameKey(input.parent));
|
||||
if (parent == null) {
|
||||
throw new UnprocessableEntityException("parent project " + input.parent
|
||||
+ " not found");
|
||||
}
|
||||
|
||||
if (Iterables.tryFind(parent.tree(), new Predicate<ProjectState>() {
|
||||
@Override
|
||||
public boolean apply(ProjectState input) {
|
||||
return input.getProject().getNameKey().equals(rsrc.getNameKey());
|
||||
}
|
||||
}).isPresent()) {
|
||||
throw new ResourceConflictException("cycle exists between "
|
||||
+ rsrc.getName() + " and " + parent.getProject().getName());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
MetaDataUpdate md = updateFactory.create(resource.getNameKey());
|
||||
MetaDataUpdate md = updateFactory.create(rsrc.getNameKey());
|
||||
try {
|
||||
ProjectConfig config = ProjectConfig.read(md);
|
||||
Project project = config.getProject();
|
||||
project.setParentName(Strings.emptyToNull(input.parent));
|
||||
project.setParentName(input.parent);
|
||||
|
||||
String msg = Strings.emptyToNull(input.commitMessage);
|
||||
if (msg == null) {
|
||||
@@ -89,7 +118,7 @@ class SetParent implements RestModifyView<ProjectResource, Input> {
|
||||
md.close();
|
||||
}
|
||||
} catch (RepositoryNotFoundException notFound) {
|
||||
throw new ResourceNotFoundException(resource.getName());
|
||||
throw new ResourceNotFoundException(rsrc.getName());
|
||||
} catch (ConfigInvalidException e) {
|
||||
throw new ResourceConflictException(String.format(
|
||||
"invalid project.config: %s", e.getMessage()));
|
||||
|
||||
Reference in New Issue
Block a user