Add pagination options to ListBranches REST API

Add limit and skip options to allow pagination. Those options are
supported by other similar REST API (ListProjects, ListGroups).

Change-Id: I2d14c8cc33f00033ef894b550e29e377ac5778b6
This commit is contained in:
Hugo Arès
2014-10-14 14:19:01 -04:00
parent 3133b4b28e
commit 158566267f
3 changed files with 115 additions and 0 deletions

View File

@@ -58,6 +58,12 @@ public class ListBranches implements RestReadView<ProjectResource> {
private final DynamicMap<RestView<BranchResource>> branchViews;
private final WebLinks webLinks;
@Option(name = "--limit", aliases = {"-n"}, metaVar = "CNT", usage = "maximum number of branches to list")
private int limit;
@Option(name = "--start", aliases = {"-s"}, metaVar = "CNT", usage = "number of branches to skip")
private int start;
@Option(name = "--match", aliases = {"-m"}, metaVar = "MATCH", usage = "match branches substring")
private String matchSubstring;
@@ -170,6 +176,17 @@ public class ListBranches implements RestReadView<ProjectResource> {
} else {
filteredBranches = branches;
}
if (!filteredBranches.isEmpty()) {
int end = filteredBranches.size();
if (limit > 0 && start + limit < end) {
end = start + limit;
}
if (start <= end) {
filteredBranches = filteredBranches.subList(start, end);
} else {
filteredBranches = Collections.emptyList();
}
}
return filteredBranches;
}