JENKINS-37202# Set pagination by default (#486)

This commit is contained in:
vivek 2016-09-08 00:29:44 -07:00 committed by GitHub
parent 0875637afb
commit 44bace7dc4
2 changed files with 37 additions and 2 deletions

View File

@ -122,6 +122,31 @@ public class PipelineApiTest extends BaseTest {
}
@Test
public void getPipelinesDefaultPaginationTest() throws Exception {
for(int i=0; i < 110; i++){
j.createFreeStyleProject("pipeline"+i);
}
List<Map> responses = get("/search/?q=type:pipeline", List.class);
Assert.assertEquals(100, responses.size());
responses = get("/search/?q=type:pipeline&limit=110", List.class);
Assert.assertEquals(110, responses.size());
responses = get("/search/?q=type:pipeline&limit=50", List.class);
Assert.assertEquals(50, responses.size());
responses = get("/organizations/jenkins/pipelines/", List.class);
Assert.assertEquals(100, responses.size());
responses = get("/organizations/jenkins/pipelines/?limit=40", List.class);
Assert.assertEquals(40, responses.size());
}
@Test
public void getPipelineTest() throws IOException {
Project p = j.createFreeStyleProject("pipeline1");

View File

@ -28,6 +28,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@InterceptorAnnotation(PagedResponse.Processor.class)
public @interface PagedResponse {
class Processor extends Interceptor {
private static final int DEFAULT_LIMIT=100;
@Override
public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object[] arguments)
throws IllegalAccessException, InvocationTargetException, ServletException {
@ -44,11 +45,18 @@ public @interface PagedResponse {
int start = (req.getParameter("start") != null) ? Integer.parseInt(req.getParameter("start")) : -1;
int limit = (req.getParameter("limit") != null) ? Integer.parseInt(req.getParameter("limit")) : -1;
if(start == -1){
start = 0;
}
if(limit == -1){
limit = DEFAULT_LIMIT;
}
Object[] page;
if (start >= 0 && limit >= 0) {
page = Iterators.toArray(resp.iterator(start, limit), Object.class);
// TODO: this is still a toy just to show the concept
rsp.setHeader("Link", "<" + req.getRequestURI() + "&start=" + (start + limit) + ">; rel=\"next\"");
String separator = (req.getQueryString() != null) ? "&" : "?";
rsp.setHeader("Link", "<" + req.getRequestURIWithQueryString() + separator + "start=" + (start + limit) + "&limit="+limit + ">; rel=\"next\"");
} else {
page = Iterators.toArray(resp.iterator(), Object.class);
}
@ -56,5 +64,7 @@ public @interface PagedResponse {
}
};
}
}
}