Accept group UUID and group ID as input for setting group owner via REST

At the moment the new owner can only be specified by name.

Change-Id: I3235ee9f19665ad73f5c163800daf6edfcecfad4
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2013-02-15 15:38:03 +01:00
parent d12b9b4516
commit c977090b26
2 changed files with 15 additions and 12 deletions

View File

@ -427,6 +427,9 @@ Sets the owner group of a Gerrit internal group.
The new owner group must be provided in the request body.
The new owner can be specified by name, by group UUID or by the legacy
numeric group ID.
.Request
----
PUT /groups/9999c971bb4ab872aab759d8c49833ee6b9ff320/description HTTP/1.0

View File

@ -15,7 +15,7 @@
package com.google.gerrit.server.group;
import com.google.common.base.Strings;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.data.GroupDescription;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
@ -25,13 +25,12 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.server.ReviewDb;
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.account.GroupControl;
import com.google.gerrit.server.group.PutOwner.Input;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.Collections;
@ -41,15 +40,15 @@ public class PutOwner implements RestModifyView<GroupResource, Input> {
String owner;
}
private final GroupBackend groupBackend;
private final Provider<GroupsCollection> groupsCollection;
private final GroupCache groupCache;
private final GroupControl.Factory controlFactory;
private final ReviewDb db;
@Inject
PutOwner(GroupBackend groupBackend, GroupCache groupCache,
PutOwner(Provider<GroupsCollection> groupsCollection, GroupCache groupCache,
GroupControl.Factory controlFactory, ReviewDb db) {
this.groupBackend = groupBackend;
this.groupsCollection = groupsCollection;
this.groupCache = groupCache;
this.controlFactory = controlFactory;
this.db = db;
@ -70,21 +69,22 @@ public class PutOwner implements RestModifyView<GroupResource, Input> {
throw new BadRequestException("owner is required");
}
GroupReference owner =
GroupBackends.findExactSuggestion(groupBackend, input.owner);
if (owner == null) {
GroupDescription.Basic owner;
try {
owner = groupsCollection.get().parse(input.owner);
} catch (ResourceNotFoundException e) {
throw new BadRequestException(String.format("No such group: %s", input.owner));
}
try {
GroupControl c = controlFactory.validateFor(owner.getUUID());
GroupControl c = controlFactory.validateFor(owner.getGroupUUID());
group = db.accountGroups().get(group.getId());
if (group == null) {
throw new ResourceNotFoundException();
}
if (!group.getOwnerGroupUUID().equals(owner.getUUID())) {
group.setOwnerGroupUUID(owner.getUUID());
if (!group.getOwnerGroupUUID().equals(owner.getGroupUUID())) {
group.setOwnerGroupUUID(owner.getGroupUUID());
db.accountGroups().update(Collections.singleton(group));
groupCache.evict(group);
}