diff options
Diffstat (limited to 'gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java')
-rw-r--r-- | gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java b/gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java index 0c82b9d3a5..20e1dbe94b 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/group/PutOwner.java @@ -16,6 +16,7 @@ package com.google.gerrit.server.group; import com.google.common.base.Strings; import com.google.gerrit.common.data.GroupDescription; +import com.google.gerrit.common.errors.NoSuchGroupException; import com.google.gerrit.extensions.common.GroupInfo; import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.BadRequestException; @@ -26,14 +27,12 @@ import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.UnprocessableEntityException; import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.server.ReviewDb; -import com.google.gerrit.server.account.GroupCache; 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 com.google.inject.Singleton; import java.io.IOException; -import java.util.Collections; @Singleton public class PutOwner implements RestModifyView<GroupResource, Input> { @@ -42,18 +41,18 @@ public class PutOwner implements RestModifyView<GroupResource, Input> { } private final GroupsCollection groupsCollection; - private final GroupCache groupCache; + private final Provider<GroupsUpdate> groupsUpdateProvider; private final Provider<ReviewDb> db; private final GroupJson json; @Inject PutOwner( GroupsCollection groupsCollection, - GroupCache groupCache, + @UserInitiated Provider<GroupsUpdate> groupsUpdateProvider, Provider<ReviewDb> db, GroupJson json) { this.groupsCollection = groupsCollection; - this.groupCache = groupCache; + this.groupsUpdateProvider = groupsUpdateProvider; this.db = db; this.json = json; } @@ -62,10 +61,9 @@ public class PutOwner implements RestModifyView<GroupResource, Input> { public GroupInfo apply(GroupResource resource, Input input) throws ResourceNotFoundException, MethodNotAllowedException, AuthException, BadRequestException, UnprocessableEntityException, OrmException, IOException { - AccountGroup group = resource.toAccountGroup(); - if (group == null) { - throw new MethodNotAllowedException(); - } else if (!resource.getControl().isOwner()) { + GroupDescription.Internal internalGroup = + resource.asInternalGroup().orElseThrow(MethodNotAllowedException::new); + if (!resource.getControl().isOwner()) { throw new AuthException("Not group owner"); } @@ -73,16 +71,17 @@ public class PutOwner implements RestModifyView<GroupResource, Input> { throw new BadRequestException("owner is required"); } - group = db.get().accountGroups().get(group.getId()); - if (group == null) { - throw new ResourceNotFoundException(); - } - GroupDescription.Basic owner = groupsCollection.parse(input.owner); - if (!group.getOwnerGroupUUID().equals(owner.getGroupUUID())) { - group.setOwnerGroupUUID(owner.getGroupUUID()); - db.get().accountGroups().update(Collections.singleton(group)); - groupCache.evict(group); + if (!internalGroup.getOwnerGroupUUID().equals(owner.getGroupUUID())) { + AccountGroup.UUID groupUuid = internalGroup.getGroupUUID(); + try { + groupsUpdateProvider + .get() + .updateGroup( + db.get(), groupUuid, group -> group.setOwnerGroupUUID(owner.getGroupUUID())); + } catch (NoSuchGroupException e) { + throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid)); + } } return json.format(owner); } |