Support to query groups by owner group name
Change-Id: If514ec35caafe6fb359f82ff43a9e8efc8f520f2 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
parent
144a07d079
commit
dad613ef3e
|
@ -49,9 +49,10 @@ name:'NAME'::
|
||||||
Matches groups that have the name 'NAME' (case-insensitive).
|
Matches groups that have the name 'NAME' (case-insensitive).
|
||||||
|
|
||||||
[[owner]]
|
[[owner]]
|
||||||
owner:'UUID'::
|
owner:'OWNER'::
|
||||||
+
|
+
|
||||||
Matches groups that are owned by a group that has the UUID 'UUID'.
|
Matches groups that are owned by the group whose name best matches
|
||||||
|
'OWNER' or that has the UUID 'OWNER'.
|
||||||
|
|
||||||
[[uuid]]
|
[[uuid]]
|
||||||
uuid:'UUID'::
|
uuid:'UUID'::
|
||||||
|
|
|
@ -14,30 +14,14 @@
|
||||||
|
|
||||||
package com.google.gerrit.server.query.group;
|
package com.google.gerrit.server.query.group;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||||
import com.google.gerrit.server.index.FieldDef;
|
import com.google.gerrit.server.index.FieldDef;
|
||||||
import com.google.gerrit.server.index.IndexPredicate;
|
import com.google.gerrit.server.index.IndexPredicate;
|
||||||
import com.google.gerrit.server.index.group.GroupField;
|
import com.google.gerrit.server.index.group.GroupField;
|
||||||
import com.google.gerrit.server.query.Predicate;
|
import com.google.gerrit.server.query.Predicate;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class GroupPredicates {
|
public class GroupPredicates {
|
||||||
public static Predicate<AccountGroup> defaultPredicate(String query) {
|
|
||||||
// Adapt the capacity of this list when adding more default predicates.
|
|
||||||
List<Predicate<AccountGroup>> preds = Lists.newArrayListWithCapacity(5);
|
|
||||||
preds.add(uuid(new AccountGroup.UUID(query)));
|
|
||||||
preds.add(name(query));
|
|
||||||
preds.add(inname(query));
|
|
||||||
if (!Strings.isNullOrEmpty(query)) {
|
|
||||||
preds.add(description(query));
|
|
||||||
}
|
|
||||||
preds.add(owner(query));
|
|
||||||
return Predicate.or(preds);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Predicate<AccountGroup> uuid(AccountGroup.UUID uuid) {
|
public static Predicate<AccountGroup> uuid(AccountGroup.UUID uuid) {
|
||||||
return new GroupPredicate(GroupField.UUID, GroupQueryBuilder.FIELD_UUID, uuid.get());
|
return new GroupPredicate(GroupField.UUID, GroupQueryBuilder.FIELD_UUID, uuid.get());
|
||||||
}
|
}
|
||||||
|
@ -57,8 +41,9 @@ public class GroupPredicates {
|
||||||
GroupField.NAME, GroupQueryBuilder.FIELD_NAME, name.toLowerCase(Locale.US));
|
GroupField.NAME, GroupQueryBuilder.FIELD_NAME, name.toLowerCase(Locale.US));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Predicate<AccountGroup> owner(String owner) {
|
public static Predicate<AccountGroup> owner(AccountGroup.UUID ownerUuid) {
|
||||||
return new GroupPredicate(GroupField.OWNER_UUID, GroupQueryBuilder.FIELD_OWNER, owner);
|
return new GroupPredicate(
|
||||||
|
GroupField.OWNER_UUID, GroupQueryBuilder.FIELD_OWNER, ownerUuid.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Predicate<AccountGroup> isVisibleToAll() {
|
public static Predicate<AccountGroup> isVisibleToAll() {
|
||||||
|
|
|
@ -15,13 +15,19 @@
|
||||||
package com.google.gerrit.server.query.group;
|
package com.google.gerrit.server.query.group;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.primitives.Ints;
|
import com.google.common.primitives.Ints;
|
||||||
|
import com.google.gerrit.common.data.GroupReference;
|
||||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||||
|
import com.google.gerrit.server.account.GroupBackend;
|
||||||
|
import com.google.gerrit.server.account.GroupBackends;
|
||||||
|
import com.google.gerrit.server.account.GroupCache;
|
||||||
import com.google.gerrit.server.query.LimitPredicate;
|
import com.google.gerrit.server.query.LimitPredicate;
|
||||||
import com.google.gerrit.server.query.Predicate;
|
import com.google.gerrit.server.query.Predicate;
|
||||||
import com.google.gerrit.server.query.QueryBuilder;
|
import com.google.gerrit.server.query.QueryBuilder;
|
||||||
import com.google.gerrit.server.query.QueryParseException;
|
import com.google.gerrit.server.query.QueryParseException;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** Parses a query string meant to be applied to group objects. */
|
/** Parses a query string meant to be applied to group objects. */
|
||||||
public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
|
public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
|
||||||
|
@ -35,9 +41,23 @@ public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
|
||||||
private static final QueryBuilder.Definition<AccountGroup, GroupQueryBuilder> mydef =
|
private static final QueryBuilder.Definition<AccountGroup, GroupQueryBuilder> mydef =
|
||||||
new QueryBuilder.Definition<>(GroupQueryBuilder.class);
|
new QueryBuilder.Definition<>(GroupQueryBuilder.class);
|
||||||
|
|
||||||
|
public static class Arguments {
|
||||||
|
final GroupCache groupCache;
|
||||||
|
final GroupBackend groupBackend;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Arguments(GroupCache groupCache, GroupBackend groupBackend) {
|
||||||
|
this.groupCache = groupCache;
|
||||||
|
this.groupBackend = groupBackend;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Arguments args;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
GroupQueryBuilder() {
|
GroupQueryBuilder(Arguments args) {
|
||||||
super(mydef);
|
super(mydef);
|
||||||
|
this.args = args;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operator
|
@Operator
|
||||||
|
@ -68,8 +88,16 @@ public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operator
|
@Operator
|
||||||
public Predicate<AccountGroup> owner(String owner) {
|
public Predicate<AccountGroup> owner(String owner) throws QueryParseException {
|
||||||
return GroupPredicates.owner(owner);
|
AccountGroup group = args.groupCache.get(new AccountGroup.UUID(owner));
|
||||||
|
if (group != null) {
|
||||||
|
return GroupPredicates.owner(group.getGroupUUID());
|
||||||
|
}
|
||||||
|
GroupReference g = GroupBackends.findBestSuggestion(args.groupBackend, owner);
|
||||||
|
if (g == null) {
|
||||||
|
throw error("Group " + owner + " not found");
|
||||||
|
}
|
||||||
|
return GroupPredicates.owner(g.getUUID());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operator
|
@Operator
|
||||||
|
@ -81,8 +109,21 @@ public class GroupQueryBuilder extends QueryBuilder<AccountGroup> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Predicate<AccountGroup> defaultField(String query) {
|
protected Predicate<AccountGroup> defaultField(String query) throws QueryParseException {
|
||||||
return GroupPredicates.defaultPredicate(query);
|
// Adapt the capacity of this list when adding more default predicates.
|
||||||
|
List<Predicate<AccountGroup>> preds = Lists.newArrayListWithCapacity(5);
|
||||||
|
preds.add(uuid(query));
|
||||||
|
preds.add(name(query));
|
||||||
|
preds.add(inname(query));
|
||||||
|
if (!Strings.isNullOrEmpty(query)) {
|
||||||
|
preds.add(description(query));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
preds.add(owner(query));
|
||||||
|
} catch (QueryParseException e) {
|
||||||
|
// Skip.
|
||||||
|
}
|
||||||
|
return Predicate.or(preds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operator
|
@Operator
|
||||||
|
|
|
@ -216,14 +216,15 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void byOwner() throws Exception {
|
public void byOwner() throws Exception {
|
||||||
assertQuery("owner:non-existing");
|
|
||||||
|
|
||||||
GroupInfo ownerGroup = createGroup(name("owner-group"));
|
GroupInfo ownerGroup = createGroup(name("owner-group"));
|
||||||
GroupInfo group = createGroupWithOwner(name("group"), ownerGroup);
|
GroupInfo group = createGroupWithOwner(name("group"), ownerGroup);
|
||||||
createGroup(name("group2"));
|
createGroup(name("group2"));
|
||||||
|
|
||||||
|
assertQuery("owner:" + group.id);
|
||||||
|
|
||||||
// ownerGroup owns itself
|
// ownerGroup owns itself
|
||||||
assertQuery("owner:" + ownerGroup.id, group, ownerGroup);
|
assertQuery("owner:" + ownerGroup.id, group, ownerGroup);
|
||||||
|
assertQuery("owner:" + ownerGroup.name, group, ownerGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue