From d12c6b4a722595fd41b1fdfd08f388f86afbd8d1 Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Sat, 10 Mar 2018 00:13:44 +0000 Subject: [PATCH] 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 --- Documentation/cmd-create-project.txt | 9 ++++++- .../args4j/AccountGroupUUIDHandler.java | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Documentation/cmd-create-project.txt b/Documentation/cmd-create-project.txt index 4d1ea05450..e8c3857b60 100644 --- a/Documentation/cmd-create-project.txt +++ b/Documentation/cmd-create-project.txt @@ -60,7 +60,14 @@ This command is intended to be used in scripts. --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 79ab8c8726..56446688bc 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 @@ import org.kohsuke.args4j.spi.Setter; public class AccountGroupUUIDHandler extends OptionHandler { 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 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");