Support querying groups by name and name part

This adds two query operators for group queries:

1. name:
   Exact match on the group name (case-insensitive)
2. inname:
   Prefix match on a group name part (case-insensitive)

Change-Id: Iedec2574690881470ba7f0ff7cd12263e466352f
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-01-03 16:15:01 +01:00
parent fadf363ef0
commit a45b072688
6 changed files with 82 additions and 7 deletions

View File

@@ -226,7 +226,7 @@ As result a list of link:#group-info[GroupInfo] entities is returned.
.Request
----
GET /groups/?query2=uuid:59b92f35489e62c80d1ab1bf0c2d17843038df8b HTTP/1.0
GET /groups/?query2=inname:test HTTP/1.0
----
.Response
@@ -238,13 +238,22 @@ As result a list of link:#group-info[GroupInfo] entities is returned.
)]}'
[
{
"url": "#/admin/groups/uuid-59b92f35489e62c80d1ab1bf0c2d17843038df8b",
"url": "#/admin/groups/uuid-68236a40ca78de8be630312d8ba50250bc5638ae",
"options": {},
"description": "Gerrit Site Administrators",
"group_id": 1,
"owner": "Administrators",
"description": "Group for running tests on MyProject",
"group_id": 20,
"owner": "MyProject-Test-Group",
"owner_id": "59b92f35489e62c80d1ab1bf0c2d17843038df8b",
"id": "59b92f35489e62c80d1ab1bf0c2d17843038df8b"
"id": "68236a40ca78de8be630312d8ba50250bc5638ae"
},
{
"url": "#/admin/groups/uuid-99a534526313324a2667025c3f4e089199b736aa",
"options": {},
"description": "Testers for ProjectX",
"group_id": 17,
"owner": "ProjectX-Testers",
"owner_id": "59b92f35489e62c80d1ab1bf0c2d17843038df8b",
"id": "99a534526313324a2667025c3f4e089199b736aa"
}
]
----

View File

@@ -10,6 +10,17 @@ Operators act as restrictions on the search. As more operators
are added to the same query string, they further restrict the
returned results.
[[inname]]
inname:'NAMEPART'::
+
Matches groups that have a name part that starts with 'NAMEPART'
(case-insensitive).
[[name]]
name:'NAME'::
+
Matches groups that have the name 'NAME' (case-insensitive).
[[uuid]]
uuid:'UUID'::
+

View File

@@ -56,7 +56,7 @@ public interface Groups {
* Query groups.
* <p>
* Example code:
* {@code query().withQuery("uuid:085178e5de6302324675715ca22f4027538253ba").get()}
* {@code query().withQuery("inname:test").withLimit(10).get()}
*
* @return API for setting parameters and getting result.
*/

View File

@@ -20,12 +20,24 @@ import com.google.gerrit.server.index.IndexPredicate;
import com.google.gerrit.server.index.group.GroupField;
import com.google.gerrit.server.query.Predicate;
import java.util.Locale;
public class GroupPredicates {
public static Predicate<AccountGroup> uuid(AccountGroup.UUID uuid) {
return new GroupPredicate(GroupField.UUID,
GroupQueryBuilder.FIELD_UUID, uuid.get());
}
public static Predicate<AccountGroup> inname(String name) {
return new GroupPredicate(GroupField.NAME_PART,
GroupQueryBuilder.FIELD_INNAME, name.toLowerCase(Locale.US));
}
public static Predicate<AccountGroup> name(String name) {
return new GroupPredicate(GroupField.NAME,
GroupQueryBuilder.FIELD_NAME, name.toLowerCase(Locale.US));
}
static class GroupPredicate extends IndexPredicate<AccountGroup> {
GroupPredicate(FieldDef<AccountGroup, ?> def, String value) {
super(def, value);

View File

@@ -27,6 +27,8 @@ import com.google.inject.Inject;
*/
public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
public static final String FIELD_UUID = "uuid";
public static final String FIELD_INNAME = "inname";
public static final String FIELD_NAME = "name";
public static final String FIELD_LIMIT = "limit";
private static final QueryBuilder.Definition<AccountGroup, GroupQueryBuilder> mydef =
@@ -42,6 +44,19 @@ public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
return GroupPredicates.uuid(new AccountGroup.UUID(uuid));
}
@Operator
public Predicate<AccountGroup> inname(String namePart) {
if (namePart.isEmpty()) {
return name(namePart);
}
return GroupPredicates.inname(namePart);
}
@Operator
public Predicate<AccountGroup> name(String name) {
return GroupPredicates.name(name);
}
@Operator
public Predicate<AccountGroup> limit(String query)
throws QueryParseException {

View File

@@ -60,6 +60,7 @@ import org.junit.rules.TestName;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@Ignore
public abstract class AbstractQueryGroupsTest extends GerritServerTests {
@@ -184,6 +185,33 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
assertQuery("uuid:" + group.id, group);
}
@Test
public void byName() throws Exception {
assertQuery("name:non-existing");
GroupInfo group = createGroup(name("group"));
assertQuery("name:" + group.name, group);
assertQuery("name:" + group.name.toUpperCase(Locale.US), group);
// only exact match
GroupInfo groupWithHyphen = createGroup(name("group-with-hyphen"));
createGroup(name("group-no-match-with-hyphen"));
assertQuery("name:" + groupWithHyphen.name, groupWithHyphen);
}
@Test
public void byInname() throws Exception {
String namePart = testName.getMethodName();
GroupInfo group1 = createGroup("group-" + namePart);
GroupInfo group2 = createGroup("group-" + namePart + "-2");
GroupInfo group3 = createGroup("group-" + namePart + "3");
assertQuery("inname:" + namePart, group1, group2, group3);
assertQuery("inname:" + namePart.toUpperCase(Locale.US), group1, group2,
group3);
assertQuery("inname:" + namePart.toLowerCase(Locale.US), group1, group2,
group3);
}
@Test
public void withLimit() throws Exception {
GroupInfo group1 = createGroup(name("group1"));