From 6115eae5d0e457703f3515af20a408ef6e97dea7 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Thu, 30 Jun 2016 14:21:19 +0200 Subject: [PATCH] QueryAccounts: Support parameter 'start' The 'start' query parameter can be supplied to skip a number of accounts from the list. Change-Id: I31497d0db3ac6049a645fe2d29984797bd6cfcae Signed-off-by: Edwin Kempin --- Documentation/rest-api-accounts.txt | 3 +++ .../gerrit/extensions/api/accounts/Accounts.java | 14 ++++++++++++++ .../gerrit/server/account/QueryAccounts.java | 14 ++++++++++++++ .../gerrit/server/api/accounts/AccountsImpl.java | 1 + .../query/account/AccountQueryProcessor.java | 2 +- .../query/account/AbstractQueryAccountsTest.java | 11 +++++++++++ 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Documentation/rest-api-accounts.txt b/Documentation/rest-api-accounts.txt index c869c55933..f4cbbe085b 100644 --- a/Documentation/rest-api-accounts.txt +++ b/Documentation/rest-api-accounts.txt @@ -49,6 +49,9 @@ returned. ] ---- +The `S` or `start` query parameter can be supplied to skip a number +of accounts from the list. + [[suggest-account]] To get account suggestions set the parameter `suggest` and provide the typed substring as query `q`. If a result limit `n` is not specified, diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/Accounts.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/Accounts.java index e29d73acae..dc2bfd8dd2 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/Accounts.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/accounts/Accounts.java @@ -140,6 +140,7 @@ public interface Accounts { abstract class QueryRequest { private String query; private int limit; + private int start; /** * Executes query and returns a list of accounts. @@ -165,6 +166,15 @@ public interface Accounts { return this; } + /** + * Set number of accounts to skip. + * Optional; no accounts are skipped when not provided. + */ + public QueryRequest withStart(int start) { + this.start = start; + return this; + } + public String getQuery() { return query; } @@ -172,6 +182,10 @@ public interface Accounts { public int getLimit() { return limit; } + + public int getStart() { + return start; + } } /** diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/QueryAccounts.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/QueryAccounts.java index 68f794e5ca..ed7617f7b7 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/account/QueryAccounts.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/QueryAccounts.java @@ -63,6 +63,7 @@ public class QueryAccounts implements RestReadView { private boolean suggest; private int suggestLimit = 10; private String query; + private Integer start; @Option(name = "--suggest", metaVar = "SUGGEST", usage = "suggest users") public void setSuggest(boolean suggest) { @@ -87,6 +88,12 @@ public class QueryAccounts implements RestReadView { this.query = query; } + @Option(name = "--start", aliases = {"-S"}, metaVar = "CNT", + usage = "Number of accounts to skip") + public void setStart(int start) { + this.start = start; + } + @Inject QueryAccounts(AccountControl.Factory accountControlFactory, AccountLoader.Factory accountLoaderFactory, @@ -139,6 +146,9 @@ public class QueryAccounts implements RestReadView { if (!suggest) { throw new MethodNotAllowedException(); } + if (start != null) { + throw new MethodNotAllowedException("option start not allowed"); + } matches = queryFromDb(); } @@ -151,6 +161,10 @@ public class QueryAccounts implements RestReadView { throw new MethodNotAllowedException("query disabled"); } + if (start != null) { + queryProcessor.setStart(start); + } + Map matches = new LinkedHashMap<>(); try { Predicate queryPred; diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountsImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountsImpl.java index 2a9f5557a3..d92b8f7035 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountsImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/accounts/AccountsImpl.java @@ -123,6 +123,7 @@ public class AccountsImpl implements Accounts { QueryAccounts myQueryAccounts = queryAccountsProvider.get(); myQueryAccounts.setQuery(r.getQuery()); myQueryAccounts.setLimit(r.getLimit()); + myQueryAccounts.setStart(r.getStart()); return myQueryAccounts.apply(TopLevelResource.INSTANCE); } catch (OrmException e) { throw new RestApiException("Cannot retrieve suggested accounts", e); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/account/AccountQueryProcessor.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/account/AccountQueryProcessor.java index 4819404fef..48d0897487 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/query/account/AccountQueryProcessor.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/account/AccountQueryProcessor.java @@ -57,6 +57,6 @@ public class AccountQueryProcessor extends QueryProcessor { protected Predicate enforceVisibility( Predicate pred) { return new AndSource<>(pred, - new AccountIsVisibleToPredicate(accountControlFactory.get())); + new AccountIsVisibleToPredicate(accountControlFactory.get()), start); } } diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/query/account/AbstractQueryAccountsTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/query/account/AbstractQueryAccountsTest.java index da0e34c8c1..be515a0a2b 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/query/account/AbstractQueryAccountsTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/query/account/AbstractQueryAccountsTest.java @@ -256,6 +256,17 @@ public abstract class AbstractQueryAccountsTest extends GerritServerTests { assertQuery(newQuery(domain).withLimit(2), user1, user2); } + @Test + public void withStart() throws Exception { + String domain = name("test.com"); + AccountInfo user1 = newAccountWithEmail("user1", "user1@" + domain); + AccountInfo user2 = newAccountWithEmail("user2", "user2@" + domain); + AccountInfo user3 = newAccountWithEmail("user3", "user3@" + domain); + + assertQuery(domain, user1, user2, user3); + assertQuery(newQuery(domain).withStart(1), user2, user3); + } + protected AccountInfo newAccount(String username) throws Exception { return newAccountWithEmail(username, null); }