Merge changes I6974d505,I3685381f

* changes:
  Allow change GroupJson options in CreateGroup
  CreateGroup: add members to GroupInput
This commit is contained in:
Edwin Kempin
2016-08-17 06:22:08 +00:00
committed by Gerrit Code Review
4 changed files with 44 additions and 12 deletions

View File

@@ -1318,6 +1318,8 @@ Whether the group is visible to all registered users. +
This can be a group UUID, a legacy numeric group ID or a unique group This can be a group UUID, a legacy numeric group ID or a unique group
name. + name. +
If not set, the new group will be self-owned. If not set, the new group will be self-owned.
|`members` |optional|The initial members in a list of +
link:#account-id[account ids].
|=========================== |===========================
[[group-options-info]] [[group-options-info]]

View File

@@ -14,9 +14,12 @@
package com.google.gerrit.extensions.api.groups; package com.google.gerrit.extensions.api.groups;
import java.util.List;
public class GroupInput { public class GroupInput {
public String name; public String name;
public String description; public String description;
public Boolean visibleToAll; public Boolean visibleToAll;
public String ownerId; public String ownerId;
public List<String> members;
} }

View File

@@ -125,11 +125,11 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
GroupControl control = resource.getControl(); GroupControl control = resource.getControl();
Set<Account.Id> newMemberIds = new HashSet<>(); Set<Account.Id> newMemberIds = new HashSet<>();
for (String nameOrEmail : input.members) { for (String nameOrEmailOrId : input.members) {
Account a = findAccount(nameOrEmail); Account a = findAccount(nameOrEmailOrId);
if (!a.isActive()) { if (!a.isActive()) {
throw new UnprocessableEntityException(String.format( throw new UnprocessableEntityException(String.format(
"Account Inactive: %s", nameOrEmail)); "Account Inactive: %s", nameOrEmailOrId));
} }
if (!control.canAddMember()) { if (!control.canAddMember()) {
@@ -142,10 +142,10 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
return toAccountInfoList(newMemberIds); return toAccountInfoList(newMemberIds);
} }
private Account findAccount(String nameOrEmail) throws AuthException, Account findAccount(String nameOrEmailOrId) throws AuthException,
UnprocessableEntityException, OrmException, IOException { UnprocessableEntityException, OrmException, IOException {
try { try {
return accounts.parse(nameOrEmail).getAccount(); return accounts.parse(nameOrEmailOrId).getAccount();
} catch (UnprocessableEntityException e) { } catch (UnprocessableEntityException e) {
// might be because the account does not exist or because the account is // might be because the account does not exist or because the account is
// not visible // not visible
@@ -153,9 +153,9 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
case HTTP_LDAP: case HTTP_LDAP:
case CLIENT_SSL_CERT_LDAP: case CLIENT_SSL_CERT_LDAP:
case LDAP: case LDAP:
if (accountResolver.find(db.get(), nameOrEmail) == null) { if (accountResolver.find(db.get(), nameOrEmailOrId) == null) {
// account does not exist, try to create it // account does not exist, try to create it
Account a = createAccountByLdap(nameOrEmail); Account a = createAccountByLdap(nameOrEmailOrId);
if (a != null) { if (a != null) {
return a; return a;
} }

View File

@@ -21,8 +21,10 @@ import com.google.gerrit.common.data.GroupDescription;
import com.google.gerrit.common.data.GroupDescriptions; import com.google.gerrit.common.data.GroupDescriptions;
import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.api.groups.GroupInput; import com.google.gerrit.extensions.api.groups.GroupInput;
import com.google.gerrit.extensions.client.ListGroupsOption;
import com.google.gerrit.extensions.common.GroupInfo; import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.registration.DynamicSet; import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
@@ -51,6 +53,8 @@ import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@@ -96,9 +100,19 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
this.name = name; this.name = name;
} }
public CreateGroup addOption(ListGroupsOption o) {
json.addOption(o);
return this;
}
public CreateGroup addOptions(Collection<ListGroupsOption> o) {
json.addOptions(o);
return this;
}
@Override @Override
public GroupInfo apply(TopLevelResource resource, GroupInput input) public GroupInfo apply(TopLevelResource resource, GroupInput input)
throws BadRequestException, UnprocessableEntityException, throws AuthException, BadRequestException, UnprocessableEntityException,
ResourceConflictException, OrmException, IOException { ResourceConflictException, OrmException, IOException {
if (input == null) { if (input == null) {
input = new GroupInput(); input = new GroupInput();
@@ -114,9 +128,22 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll, args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll,
defaultVisibleToAll); defaultVisibleToAll);
args.ownerGroupId = ownerId; args.ownerGroupId = ownerId;
if (input.members != null && !input.members.isEmpty()) {
List<Account.Id> members = new ArrayList<>();
for (String nameOrEmailOrId : input.members) {
Account a = addMembers.findAccount(nameOrEmailOrId);
if (!a.isActive()) {
throw new UnprocessableEntityException(String.format(
"Account Inactive: %s", nameOrEmailOrId));
}
members.add(a.getId());
}
args.initialMembers = members;
} else {
args.initialMembers = ownerId == null args.initialMembers = ownerId == null
? Collections.singleton(self.get().getAccountId()) ? Collections.singleton(self.get().getAccountId())
: Collections.<Account.Id> emptySet(); : Collections.<Account.Id> emptySet();
}
for (GroupCreationValidationListener l : groupCreationValidationListeners) { for (GroupCreationValidationListener l : groupCreationValidationListeners) {
try { try {