Merge "Implement /groups/?user=* in terms of /accounts/*/groups"

This commit is contained in:
Edwin Kempin
2013-01-17 21:54:33 +00:00
committed by Gerrit Code Review
2 changed files with 45 additions and 27 deletions

View File

@@ -25,7 +25,7 @@ import com.google.inject.Inject;
import java.util.List; import java.util.List;
class GetGroups implements RestReadView<AccountResource> { public class GetGroups implements RestReadView<AccountResource> {
private final GroupControl.Factory groupControlFactory; private final GroupControl.Factory groupControlFactory;
@Inject @Inject

View File

@@ -15,6 +15,8 @@
package com.google.gerrit.server.group; package com.google.gerrit.server.group;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.gerrit.common.data.GroupDescriptions; import com.google.gerrit.common.data.GroupDescriptions;
import com.google.gerrit.common.errors.NoSuchGroupException; import com.google.gerrit.common.errors.NoSuchGroupException;
@@ -27,13 +29,17 @@ import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.OutputFormat; import com.google.gerrit.server.OutputFormat;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.GetGroups;
import com.google.gerrit.server.account.GroupCache; import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.VisibleGroups; import com.google.gerrit.server.account.VisibleGroups;
import com.google.gerrit.server.ioutil.ColumnFormatter; import com.google.gerrit.server.ioutil.ColumnFormatter;
import com.google.gerrit.server.project.ProjectControl; import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.util.Url;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider;
import org.kohsuke.args4j.Option; import org.kohsuke.args4j.Option;
@@ -52,6 +58,7 @@ public class ListGroups implements RestReadView<TopLevelResource> {
private final GroupCache groupCache; private final GroupCache groupCache;
private final VisibleGroups.Factory visibleGroupsFactory; private final VisibleGroups.Factory visibleGroupsFactory;
private final IdentifiedUser.GenericFactory userFactory; private final IdentifiedUser.GenericFactory userFactory;
private final Provider<GetGroups> accountGetGroups;
@Option(name = "--project", aliases = {"-p"}, @Option(name = "--project", aliases = {"-p"},
usage = "projects for which the groups should be listed") usage = "projects for which the groups should be listed")
@@ -79,10 +86,12 @@ public class ListGroups implements RestReadView<TopLevelResource> {
@Inject @Inject
protected ListGroups(final GroupCache groupCache, protected ListGroups(final GroupCache groupCache,
final VisibleGroups.Factory visibleGroupsFactory, final VisibleGroups.Factory visibleGroupsFactory,
final IdentifiedUser.GenericFactory userFactory) { final IdentifiedUser.GenericFactory userFactory,
Provider<GetGroups> accountGetGroups) {
this.groupCache = groupCache; this.groupCache = groupCache;
this.visibleGroupsFactory = visibleGroupsFactory; this.visibleGroupsFactory = visibleGroupsFactory;
this.userFactory = userFactory; this.userFactory = userFactory;
this.accountGetGroups = accountGetGroups;
} }
public Account.Id getUser() { public Account.Id getUser() {
@@ -112,43 +121,52 @@ public class ListGroups implements RestReadView<TopLevelResource> {
} }
try { try {
final VisibleGroups visibleGroups = visibleGroupsFactory.create(); List<GroupInfo> groups;
if (user != null) {
groups = accountGetGroups.get().apply(
new AccountResource(userFactory.create(user)));
} else {
VisibleGroups visibleGroups = visibleGroupsFactory.create();
visibleGroups.setOnlyVisibleToAll(visibleToAll); visibleGroups.setOnlyVisibleToAll(visibleToAll);
visibleGroups.setGroupType(groupType); visibleGroups.setGroupType(groupType);
visibleGroups.setMatch(matchSubstring); visibleGroups.setMatch(matchSubstring);
final List<AccountGroup> groupList; List<AccountGroup> groupList;
if (!projects.isEmpty()) { if (!projects.isEmpty()) {
groupList = visibleGroups.get(projects); groupList = visibleGroups.get(projects);
} else if (user != null) {
groupList = visibleGroups.get(userFactory.create(user));
} else { } else {
groupList = visibleGroups.get(); groupList = visibleGroups.get();
} }
groups = Lists.newArrayListWithCapacity(groupList.size());
for (AccountGroup group : groupList) {
groups.add(new GroupInfo(GroupDescriptions.forAccountGroup(group)));
}
}
if (stdout == null) { if (stdout == null) {
final Map<String, GroupInfo> output = Maps.newTreeMap(); final Map<String, GroupInfo> output = Maps.newTreeMap();
for (AccountGroup g : groupList) { for (GroupInfo info : groups) {
GroupInfo info = new GroupInfo(GroupDescriptions.forAccountGroup(g)); output.put(Objects.firstNonNull(
output.put(Objects.firstNonNull(info.name, "Group " + info.id), info); info.name,
"Group " + Url.decode(info.id)), info);
info.name = null; info.name = null;
} }
return OutputFormat.JSON.newGson().toJsonTree(output, return OutputFormat.JSON.newGson().toJsonTree(output,
new TypeToken<Map<String, GroupInfo>>() {}.getType()); new TypeToken<Map<String, GroupInfo>>() {}.getType());
} else { } else {
final ColumnFormatter formatter = new ColumnFormatter(stdout, '\t'); final ColumnFormatter formatter = new ColumnFormatter(stdout, '\t');
for (final AccountGroup g : groupList) { for (GroupInfo info : groups) {
formatter.addColumn(g.getName()); formatter.addColumn(info.name);
if (verboseOutput) { if (verboseOutput) {
formatter.addColumn(g.getGroupUUID().get()); AccountGroup o = info.ownerId != null
formatter.addColumn( ? groupCache.get(new AccountGroup.UUID(Url.decode(info.ownerId)))
g.getDescription() != null ? g.getDescription() : ""); : null;
formatter.addColumn(g.getType().toString());
final AccountGroup owningGroup = formatter.addColumn(Url.decode(info.id));
groupCache.get(g.getOwnerGroupUUID()); formatter.addColumn(Strings.nullToEmpty(info.description));
formatter.addColumn( formatter.addColumn(o != null ? o.getName() : "n/a");
owningGroup != null ? owningGroup.getName() : "n/a"); formatter.addColumn(o != null ? o.getGroupUUID().get() : "");
formatter.addColumn(g.getOwnerGroupUUID().get()); formatter.addColumn(Boolean.toString(
formatter.addColumn(Boolean.toString(g.isVisibleToAll())); Objects.firstNonNull(info.visibleToAll, Boolean.FALSE)));
} }
formatter.nextLine(); formatter.nextLine();
} }