Align SSH create-project with REST API

The way REST API and SSH command manage the creation of the project
is on paper aligned: they both should accept either owner group name or
UUID.

Align the way group UUID is parsed with its REST API counterpart so
that SSH and REST commands would do the same thing with the same
parameters.

Before this change, a create-project over SSH with a user:username UUID
failed while it was successful as REST API.

Bug: Issue 8389
Change-Id: Ieb04f758ea17fe30bbdae0f801c1b45e2afc0847
diff --git a/Documentation/cmd-create-project.txt b/Documentation/cmd-create-project.txt
index 4d1ea05..e8c3857 100644
--- a/Documentation/cmd-create-project.txt
+++ b/Documentation/cmd-create-project.txt
@@ -60,7 +60,14 @@
 
 --owner::
 -o::
-	Name of the group(s) which will initially own this repository.
+	Identifier of the group(s) which will initially own this repository.
+
+        This can be:
+
+        * the UUID of the group
+        * the legacy numeric ID of the group
+        * the name of the group if it is unique
+
 	The specified group(s) must already be defined within Gerrit.
 	Several groups can be specified on the command line.
 +
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/args4j/AccountGroupUUIDHandler.java b/gerrit-server/src/main/java/com/google/gerrit/server/args4j/AccountGroupUUIDHandler.java
index 79ab8c8..5644668 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/args4j/AccountGroupUUIDHandler.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/args4j/AccountGroupUUIDHandler.java
@@ -14,10 +14,13 @@
 
 package com.google.gerrit.server.args4j;
 
+import com.google.gerrit.common.data.GroupDescription;
 import com.google.gerrit.common.data.GroupReference;
+import com.google.gerrit.common.errors.NoSuchGroupException;
 import com.google.gerrit.reviewdb.client.AccountGroup;
 import com.google.gerrit.server.account.GroupBackend;
 import com.google.gerrit.server.account.GroupBackends;
+import com.google.gerrit.server.account.GroupControl;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import org.kohsuke.args4j.CmdLineException;
@@ -29,20 +32,44 @@
 
 public class AccountGroupUUIDHandler extends OptionHandler<AccountGroup.UUID> {
   private final GroupBackend groupBackend;
+  private final GroupControl.Factory groupControlFactory;
 
   @Inject
   public AccountGroupUUIDHandler(
       final GroupBackend groupBackend,
+      final GroupControl.Factory groupControlFactory,
       @Assisted final CmdLineParser parser,
       @Assisted final OptionDef option,
       @Assisted final Setter<AccountGroup.UUID> setter) {
     super(parser, option, setter);
     this.groupBackend = groupBackend;
+    this.groupControlFactory = groupControlFactory;
   }
 
   @Override
   public final int parseArguments(final Parameters params) throws CmdLineException {
     final String n = params.getParameter(0);
+    AccountGroup.UUID uuid = new AccountGroup.UUID(n);
+    if (groupBackend.handles(uuid)) {
+      GroupDescription.Basic d = groupBackend.get(uuid);
+      if (d != null) {
+        setter.addValue(uuid);
+        return 1;
+      }
+    }
+
+    // Might be a legacy AccountGroup.Id.
+    if (n.matches("^[1-9][0-9]*$")) {
+      try {
+        AccountGroup.Id legacyId = AccountGroup.Id.parse(n);
+        uuid = groupControlFactory.controlFor(legacyId).getGroup().getGroupUUID();
+        setter.addValue(uuid);
+        return 1;
+      } catch (IllegalArgumentException | NoSuchGroupException e) {
+        // Ignored
+      }
+    }
+
     GroupReference group = GroupBackends.findExactSuggestion(groupBackend, n);
     if (group == null) {
       throw new CmdLineException(owner, "Group \"" + n + "\" does not exist");