ListChildProjects: Add limit option

Change-Id: Ied90e49cec5c9b658ad65e7a8ab45f2ec5e03948
This commit is contained in:
David Ostrovsky
2019-02-03 21:30:42 +01:00
committed by David Pursehouse
parent ddd38f36dc
commit d3039e7143
4 changed files with 42 additions and 0 deletions

View File

@@ -106,6 +106,8 @@ public interface ProjectApi {
List<ProjectInfo> children(boolean recursive) throws RestApiException; List<ProjectInfo> children(boolean recursive) throws RestApiException;
List<ProjectInfo> children(int limit) throws RestApiException;
ChildProjectApi child(String name) throws RestApiException; ChildProjectApi child(String name) throws RestApiException;
/** /**
@@ -284,6 +286,11 @@ public interface ProjectApi {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public List<ProjectInfo> children(int limit) throws RestApiException {
throw new NotImplementedException();
}
@Override @Override
public ChildProjectApi child(String name) throws RestApiException { public ChildProjectApi child(String name) throws RestApiException {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@@ -484,6 +484,15 @@ public class ProjectApiImpl implements ProjectApi {
} }
} }
@Override
public List<ProjectInfo> children(int limit) throws RestApiException {
try {
return children.list().withLimit(limit).apply(checkExists());
} catch (Exception e) {
throw asRestApiException("Cannot list children", e);
}
}
@Override @Override
public ChildProjectApi child(String name) throws RestApiException { public ChildProjectApi child(String name) throws RestApiException {
try { try {

View File

@@ -17,6 +17,8 @@ package com.google.gerrit.server.restapi.project;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import com.google.gerrit.extensions.common.ProjectInfo; import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.TopLevelResource; import com.google.gerrit.extensions.restapi.TopLevelResource;
@@ -37,6 +39,9 @@ public class ListChildProjects implements RestReadView<ProjectResource> {
@Option(name = "--recursive", usage = "to list child projects recursively") @Option(name = "--recursive", usage = "to list child projects recursively")
private boolean recursive; private boolean recursive;
@Option(name = "--limit", usage = "maximum number of parents projects to list")
private int limit;
private final PermissionBackend permissionBackend; private final PermissionBackend permissionBackend;
private final ChildProjects childProjects; private final ChildProjects childProjects;
private final Provider<QueryProjects> queryProvider; private final Provider<QueryProjects> queryProvider;
@@ -55,9 +60,20 @@ public class ListChildProjects implements RestReadView<ProjectResource> {
this.recursive = recursive; this.recursive = recursive;
} }
public ListChildProjects withLimit(int limit) {
this.limit = limit;
return this;
}
@Override @Override
public List<ProjectInfo> apply(ProjectResource rsrc) public List<ProjectInfo> apply(ProjectResource rsrc)
throws PermissionBackendException, OrmException, RestApiException { throws PermissionBackendException, OrmException, RestApiException {
if (limit < 0) {
throw new BadRequestException("limit must be a positive number");
}
if (recursive && limit != 0) {
throw new ResourceConflictException("recursive and limit options are mutually exclusive");
}
rsrc.getProjectState().checkStatePermitsRead(); rsrc.getProjectState().checkStatePermitsRead();
if (recursive) { if (recursive) {
return childProjects.list(rsrc.getNameKey()); return childProjects.list(rsrc.getNameKey());
@@ -72,6 +88,7 @@ public class ListChildProjects implements RestReadView<ProjectResource> {
return queryProvider return queryProvider
.get() .get()
.withQuery("parent:" + parent.get()) .withQuery("parent:" + parent.get())
.withLimit(limit)
.apply(TopLevelResource.INSTANCE) .apply(TopLevelResource.INSTANCE)
.stream() .stream()
.filter( .filter(

View File

@@ -48,6 +48,15 @@ public class ListChildProjectsIT extends AbstractDaemonTest {
.inOrder(); .inOrder();
} }
@Test
public void listChildrenWithLimit() throws Exception {
Project.NameKey child1 = createProject("p1");
Project.NameKey child1_1 = createProject("p1.1", child1);
createProject("p1.2", child1);
assertThatNameList(gApi.projects().name(child1.get()).children(1)).containsExactly(child1_1);
}
@Test @Test
public void listChildrenRecursively() throws Exception { public void listChildrenRecursively() throws Exception {
Project.NameKey child1 = createProject("p1"); Project.NameKey child1 = createProject("p1");