From 4ed7fb20200db1c5e61f14564e86f43915d1eb50 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Thu, 10 Jul 2014 10:47:10 +0200 Subject: [PATCH] Use ban commit REST endpoint in ban-commit SSH command Change-Id: I1d32b6c64af35fad3768863b15c1cc37c5c3c886 Signed-off-by: Edwin Kempin --- gerrit-sshd/BUCK | 1 + .../sshd/commands/BanCommitCommand.java | 74 +++++++------------ lib/commons/BUCK | 4 - 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/gerrit-sshd/BUCK b/gerrit-sshd/BUCK index 4c59bcfd1a..fad371a340 100644 --- a/gerrit-sshd/BUCK +++ b/gerrit-sshd/BUCK @@ -18,6 +18,7 @@ java_library( '//lib:gwtorm', '//lib:jsch', '//lib/commons:codec', + '//lib/commons:collections', '//lib/guice:guice', '//lib/guice:guice-assistedinject', '//lib/guice:guice-servlet', # SSH should not depend on servlet diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/BanCommitCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/BanCommitCommand.java index 119be04845..647d28d650 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/BanCommitCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/BanCommitCommand.java @@ -16,22 +16,24 @@ package com.google.gerrit.sshd.commands; import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; -import com.google.gerrit.common.errors.PermissionDeniedException; -import com.google.gerrit.server.git.BanCommit; -import com.google.gerrit.server.git.BanCommitResult; -import com.google.gerrit.server.git.MergeException; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import com.google.gerrit.extensions.restapi.RestApiException; +import com.google.gerrit.server.project.BanCommit; +import com.google.gerrit.server.project.BanCommit.BanResultInfo; import com.google.gerrit.server.project.ProjectControl; +import com.google.gerrit.server.project.ProjectResource; import com.google.gerrit.sshd.CommandMetaData; import com.google.gerrit.sshd.SshCommand; import com.google.inject.Inject; -import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; +import org.apache.commons.collections.CollectionUtils; import org.eclipse.jgit.lib.ObjectId; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; import java.io.IOException; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -55,52 +57,30 @@ public class BanCommitCommand extends SshCommand { @Override protected void run() throws Failure { try { - final BanCommitResult result = - banCommit.ban(projectControl, commitsToBan, reason); + BanCommit.Input input = + BanCommit.Input.fromCommits(Lists.transform(commitsToBan, + new Function() { + @Override + public String apply(ObjectId oid) { + return oid.getName(); + } + })); + input.reason = reason; - final List newlyBannedCommits = - result.getNewlyBannedCommits(); - if (!newlyBannedCommits.isEmpty()) { - stdout.print("The following commits were banned:\n"); - printCommits(stdout, newlyBannedCommits); - } - - final List alreadyBannedCommits = - result.getAlreadyBannedCommits(); - if (!alreadyBannedCommits.isEmpty()) { - stdout.print("The following commits were already banned:\n"); - printCommits(stdout, alreadyBannedCommits); - } - - final List ignoredIds = result.getIgnoredObjectIds(); - if (!ignoredIds.isEmpty()) { - stdout.print("The following ids do not represent commits" - + " and were ignored:\n"); - printCommits(stdout, ignoredIds); - } - } catch (PermissionDeniedException e) { - throw die(e); - } catch (IOException e) { - throw die(e); - } catch (MergeException e) { - throw die(e); - } catch (InterruptedException e) { - throw die(e); - } catch (ConcurrentRefUpdateException e) { + BanResultInfo r = banCommit.apply(new ProjectResource(projectControl), input); + printCommits(r.newlyBanned, "The following commits were banned"); + printCommits(r.alreadyBanned, "The following commits were already banned"); + printCommits(r.ignored, "The following ids do not represent commits and were ignored"); + } catch (RestApiException | IOException | InterruptedException e) { throw die(e); } } - private static void printCommits(final PrintWriter stdout, - final List commits) { - boolean first = true; - for (final ObjectId c : commits) { - if (!first) { - stdout.print(",\n"); - } - stdout.print(c.getName()); - first = false; + private void printCommits(List commits, String message) { + if (CollectionUtils.isNotEmpty(commits)) { + stdout.print(message + ":\n"); + stdout.print(Joiner.on(",\n").join(commits)); + stdout.print("\n\n"); } - stdout.print("\n\n"); } } diff --git a/lib/commons/BUCK b/lib/commons/BUCK index b16362df2a..85e404ff30 100644 --- a/lib/commons/BUCK +++ b/lib/commons/BUCK @@ -15,10 +15,6 @@ maven_jar( license = 'Apache2.0', exclude = ['META-INF/LICENSE.txt', 'META-INF/NOTICE.txt'], attach_source = False, - visibility = [ - '//lib:velocity', - '//lib/solr:zookeeper', - ], ) maven_jar(