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

@ -1314,10 +1314,12 @@ If set, must match the group name in the URL.
|`visible_to_all`|optional|
Whether the group is visible to all registered users. +
`false` if not set.
|`owner_id`|optional|The URL encoded ID of the owner group. +
|`owner_id` |optional|The URL encoded ID of the owner group. +
This can be a group UUID, a legacy numeric group ID or a unique group
name. +
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]]

View File

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

View File

@ -125,11 +125,11 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
GroupControl control = resource.getControl();
Set<Account.Id> newMemberIds = new HashSet<>();
for (String nameOrEmail : input.members) {
Account a = findAccount(nameOrEmail);
for (String nameOrEmailOrId : input.members) {
Account a = findAccount(nameOrEmailOrId);
if (!a.isActive()) {
throw new UnprocessableEntityException(String.format(
"Account Inactive: %s", nameOrEmail));
"Account Inactive: %s", nameOrEmailOrId));
}
if (!control.canAddMember()) {
@ -142,10 +142,10 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
return toAccountInfoList(newMemberIds);
}
private Account findAccount(String nameOrEmail) throws AuthException,
Account findAccount(String nameOrEmailOrId) throws AuthException,
UnprocessableEntityException, OrmException, IOException {
try {
return accounts.parse(nameOrEmail).getAccount();
return accounts.parse(nameOrEmailOrId).getAccount();
} catch (UnprocessableEntityException e) {
// might be because the account does not exist or because the account is
// not visible
@ -153,9 +153,9 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
case HTTP_LDAP:
case CLIENT_SSL_CERT_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 a = createAccountByLdap(nameOrEmail);
Account a = createAccountByLdap(nameOrEmailOrId);
if (a != null) {
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.common.GroupInfo;
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.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestModifyView;
@ -51,6 +52,7 @@ import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -98,7 +100,7 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
@Override
public GroupInfo apply(TopLevelResource resource, GroupInput input)
throws BadRequestException, UnprocessableEntityException,
throws AuthException, BadRequestException, UnprocessableEntityException,
ResourceConflictException, OrmException, IOException {
if (input == null) {
input = new GroupInput();
@ -114,9 +116,22 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll,
defaultVisibleToAll);
args.ownerGroupId = ownerId;
args.initialMembers = ownerId == null
? Collections.singleton(self.get().getAccountId())
: Collections.<Account.Id> emptySet();
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
? Collections.singleton(self.get().getAccountId())
: Collections.<Account.Id> emptySet();
}
for (GroupCreationValidationListener l : groupCreationValidationListeners) {
try {