Use ban commit REST endpoint in ban-commit SSH command

Change-Id: I1d32b6c64af35fad3768863b15c1cc37c5c3c886
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2014-07-10 10:47:10 +02:00
parent 8c70bfa235
commit 4ed7fb2020
3 changed files with 28 additions and 51 deletions

View File

@ -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

View File

@ -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<ObjectId, String>() {
@Override
public String apply(ObjectId oid) {
return oid.getName();
}
}));
input.reason = reason;
final List<ObjectId> newlyBannedCommits =
result.getNewlyBannedCommits();
if (!newlyBannedCommits.isEmpty()) {
stdout.print("The following commits were banned:\n");
printCommits(stdout, newlyBannedCommits);
}
final List<ObjectId> alreadyBannedCommits =
result.getAlreadyBannedCommits();
if (!alreadyBannedCommits.isEmpty()) {
stdout.print("The following commits were already banned:\n");
printCommits(stdout, alreadyBannedCommits);
}
final List<ObjectId> 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<ObjectId> commits) {
boolean first = true;
for (final ObjectId c : commits) {
if (!first) {
stdout.print(",\n");
}
stdout.print(c.getName());
first = false;
private void printCommits(List<String> 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");
}
}

View File

@ -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(