CreateGroup: add members to GroupInput

Currently in CreateGroup, we use either the ownerId owner group from
input, or the current calling user.

It would be useful to allow customized initial members, for situations
like create a group for a robot account.

Change-Id: I3685381fee6745e7ee3860f0c3757572bfec60aa
This commit is contained in:
Yuxuan 'fishy' Wang
2016-08-12 12:02:55 -07:00
parent 93a4c8e42d
commit 578eb5097b
4 changed files with 32 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

@@ -23,6 +23,7 @@ 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.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 +52,7 @@ 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.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@@ -98,7 +100,7 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
@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 +116,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 {