Support retrieving of non-direct child projects via REST

To retrieve a non-direct child project the parameter 'recursive' must
be specified:
  GET /projects/myParent/children/myGrandChild?recursive

Without the 'recursive' flag being set only direct child projects can
be retrieved.

Change-Id: Ia702665efcd5a7ca59bfd9d9a0cc6eccc9ed1d9c
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2013-03-21 15:02:22 +01:00 committed by Gerrit Code Review
parent c6b69e739a
commit 8a3fb5b120
5 changed files with 43 additions and 8 deletions

View File

@ -598,7 +598,8 @@ Get Child Project
[verse]
'GET /projects/link:#project-name[\{project-name\}]/children/link:#project-name[\{project-name\}]'
Retrieves a child project.
Retrieves a child project. If a non-direct child project should be
retrieved the parameter `recursive` must be set.
.Request
----

View File

@ -103,6 +103,23 @@ public class GetChildProjectIT extends AbstractDaemonTest {
.getStatusCode());
}
@Test
public void getGrandChildProjectWithRecursiveFlag() throws IOException,
JSchException {
SshSession sshSession = new SshSession(admin);
Project.NameKey child = new Project.NameKey("p1");
createProject(sshSession, child.get());
Project.NameKey grandChild = new Project.NameKey("p1.1");
createProject(sshSession, grandChild.get(), child);
RestResponse r =
GET("/projects/" + allProjects.get() + "/children/" + grandChild.get()
+ "?recursive");
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
ProjectInfo grandChildInfo =
(new Gson()).fromJson(r.getReader(), new TypeToken<ProjectInfo>() {}.getType());
assertProjectInfo(projectCache.get(grandChild).getProject(), grandChildInfo);
}
private RestResponse GET(String endpoint) throws IOException {
return session.get(endpoint);
}

View File

@ -14,6 +14,7 @@
package com.google.gerrit.server.project;
import com.google.common.collect.Iterables;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.inject.TypeLiteral;
@ -31,4 +32,11 @@ public class ChildProjectResource extends ProjectResource {
public ProjectControl getChild() {
return child;
}
public boolean isDirectChild() {
ProjectState parent =
Iterables.getFirst(child.getProjectState().parents(), null);
return parent != null
&& getNameKey().equals(parent.getProject().getNameKey());
}
}

View File

@ -14,7 +14,6 @@
package com.google.gerrit.server.project;
import com.google.common.collect.Iterables;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ChildCollection;
@ -53,10 +52,10 @@ public class ChildProjectsCollection implements
throws ResourceNotFoundException, IOException {
ProjectResource p =
projectsCollection.get().parse(TopLevelResource.INSTANCE, id);
ProjectState pp =
Iterables.getFirst(p.getControl().getProjectState().parents(), null);
if (pp != null && parent.getNameKey().equals(pp.getProject().getNameKey())) {
return new ChildProjectResource(parent, p.getControl());
for (ProjectState pp : p.getControl().getProjectState().parents()) {
if (parent.getNameKey().equals(pp.getProject().getNameKey())) {
return new ChildProjectResource(parent, p.getControl());
}
}
throw new ResourceNotFoundException(id);
}

View File

@ -14,11 +14,17 @@
package com.google.gerrit.server.project;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.project.ProjectJson.ProjectInfo;
import com.google.inject.Inject;
import org.kohsuke.args4j.Option;
public class GetChildProject implements RestReadView<ChildProjectResource> {
@Option(name = "--recursive", usage = "to list child projects recursively")
private boolean recursive;
private final ProjectJson json;
@Inject
@ -27,7 +33,11 @@ public class GetChildProject implements RestReadView<ChildProjectResource> {
}
@Override
public ProjectInfo apply(ChildProjectResource rsrc) {
return json.format(rsrc.getChild().getProject());
public ProjectInfo apply(ChildProjectResource rsrc)
throws ResourceNotFoundException {
if (recursive || rsrc.isDirectChild()) {
return json.format(rsrc.getChild().getProject());
}
throw new ResourceNotFoundException(rsrc.getName());
}
}