Reuse REST code to list child projects in AdminSetParent

For 'set-project-parent --children-of PROJECT' AdminSetParent needs to
list the child projects of PROJECT. To do this use the REST endpoint
for listing child projects instead of having an own implementation for
computing the child projects in AdminSetParent.

Change-Id: Ic557623842f743c440df4bba0dfe713bd7d52ba1
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-03-19 10:16:43 +01:00
committed by Gerrit Code Review
parent 8a3fb5b120
commit 3d57eadf92
6 changed files with 39 additions and 28 deletions

View File

@@ -47,6 +47,9 @@ public interface ProjectCache {
/** Invalidate the cached information about the given project. */ /** Invalidate the cached information about the given project. */
public void evict(Project p); public void evict(Project p);
/** Invalidate the cached information about the given project. */
public void evict(Project.NameKey p);
/** /**
* Remove information about the given project from the cache. It will no * Remove information about the given project from the cache. It will no
* longer be returned from {@link #all()}. * longer be returned from {@link #all()}.

View File

@@ -142,6 +142,13 @@ public class ProjectCacheImpl implements ProjectCache {
} }
} }
/** Invalidate the cached information about the given project. */
public void evict(final Project.NameKey p) {
if (p != null) {
byName.invalidate(p.get());
}
}
@Override @Override
public void remove(final Project p) { public void remove(final Project p) {
listLock.lock(); listLock.lock();

View File

@@ -25,7 +25,7 @@ public class ProjectResource implements RestResource {
private final ProjectControl control; private final ProjectControl control;
ProjectResource(ProjectControl control) { public ProjectResource(ProjectControl control) {
this.control = control; this.control = control;
} }

View File

@@ -105,6 +105,11 @@ public class GerritCommonTest extends PrologTestCase {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void evict(Project.NameKey p) {
throw new UnsupportedOperationException();
}
@Override @Override
public void remove(Project p) { public void remove(Project p) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();

View File

@@ -428,6 +428,10 @@ public class RefControlTest extends TestCase {
public void evict(Project p) { public void evict(Project p) {
} }
@Override
public void evict(Project.NameKey p) {
}
@Override @Override
public void remove(Project p) { public void remove(Project p) {
} }

View File

@@ -17,18 +17,23 @@ package com.google.gerrit.sshd.commands;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsName; import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.git.MetaDataUpdate; import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig; import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ListChildProjects;
import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectControl; import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.ProjectJson.ProjectInfo;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.project.ProjectState; import com.google.gerrit.server.project.ProjectState;
import com.google.gerrit.sshd.CommandMetaData; import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand; import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
@@ -73,6 +78,9 @@ final class AdminSetParent extends SshCommand {
@Inject @Inject
private AllProjectsName allProjectsName; private AllProjectsName allProjectsName;
@Inject
private Provider<ListChildProjects> listChildProjects;
private Project.NameKey newParentKey = null; private Project.NameKey newParentKey = null;
@Override @Override
@@ -108,17 +116,16 @@ final class AdminSetParent extends SshCommand {
} }
} }
final List<Project> childProjects = new ArrayList<Project>(); final List<Project.NameKey> childProjects = Lists.newArrayList();
for (final ProjectControl pc : children) { for (final ProjectControl pc : children) {
childProjects.add(pc.getProject()); childProjects.add(pc.getProject().getNameKey());
} }
if (oldParent != null) { if (oldParent != null) {
childProjects.addAll(getChildrenForReparenting(oldParent)); childProjects.addAll(getChildrenForReparenting(oldParent));
} }
for (final Project project : childProjects) { for (final Project.NameKey nameKey : childProjects) {
final String name = project.getName(); final String name = nameKey.get();
final Project.NameKey nameKey = project.getNameKey();
if (allProjectsName.equals(nameKey)) { if (allProjectsName.equals(nameKey)) {
// Don't allow the wild card project to have a parent. // Don't allow the wild card project to have a parent.
@@ -159,7 +166,7 @@ final class AdminSetParent extends SshCommand {
err.append("error: " + msg + "\n"); err.append("error: " + msg + "\n");
} }
projectCache.evict(project); projectCache.evict(nameKey);
} }
if (err.length() > 0) { if (err.length() > 0) {
@@ -175,8 +182,8 @@ final class AdminSetParent extends SshCommand {
* reparented. The returned list of child projects does not contain projects * reparented. The returned list of child projects does not contain projects
* that were specified to be excluded from reparenting. * that were specified to be excluded from reparenting.
*/ */
private List<Project> getChildrenForReparenting(final ProjectControl parent) { private List<Project.NameKey> getChildrenForReparenting(final ProjectControl parent) {
final List<Project> childProjects = new ArrayList<Project>(); final List<Project.NameKey> childProjects = Lists.newArrayList();
final List<Project.NameKey> excluded = final List<Project.NameKey> excluded =
new ArrayList<Project.NameKey>(excludedChildren.size()); new ArrayList<Project.NameKey>(excludedChildren.size());
for (final ProjectControl excludedChild : excludedChildren) { for (final ProjectControl excludedChild : excludedChildren) {
@@ -187,11 +194,12 @@ final class AdminSetParent extends SshCommand {
if (newParentKey != null) { if (newParentKey != null) {
automaticallyExcluded.addAll(getAllParents(newParentKey)); automaticallyExcluded.addAll(getAllParents(newParentKey));
} }
for (final Project child : getChildren(parent.getProject().getNameKey())) { for (final ProjectInfo child : listChildProjects.get().apply(
final Project.NameKey childName = child.getNameKey(); new ProjectResource(parent))) {
final Project.NameKey childName = new Project.NameKey(child.name);
if (!excluded.contains(childName)) { if (!excluded.contains(childName)) {
if (!automaticallyExcluded.contains(childName)) { if (!automaticallyExcluded.contains(childName)) {
childProjects.add(child); childProjects.add(childName);
} else { } else {
stdout.println("Automatically excluded '" + childName + "' " + stdout.println("Automatically excluded '" + childName + "' " +
"from reparenting because it is in the parent " + "from reparenting because it is in the parent " +
@@ -213,20 +221,4 @@ final class AdminSetParent extends SshCommand {
} }
})); }));
} }
private List<Project> getChildren(final Project.NameKey parentName) {
final List<Project> childProjects = new ArrayList<Project>();
for (final Project.NameKey projectName : projectCache.all()) {
final ProjectState e = projectCache.get(projectName);
if (e == null) {
// If we can't get it from the cache, pretend it's not present.
continue;
}
if (parentName.equals(e.getProject().getParent(allProjectsName))) {
childProjects.add(e.getProject());
}
}
return childProjects;
}
} }