Add delete command - delete an existing batch

Allows the user to delete a batch created with the batch plugin.

Change-Id: I331b2675597a8b342b950b37edc36ac29843e8f0
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/Batch.java b/src/main/java/com/googlesource/gerrit/plugins/batch/Batch.java
index ff34662..40ff843 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/Batch.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/Batch.java
@@ -26,7 +26,8 @@
 public class Batch {
   public enum State {
     OPEN,
-    CLOSED;
+    CLOSED,
+    DELETED;
   }
 
   public static class Change {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchRemover.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchRemover.java
new file mode 100644
index 0000000..ef37f86
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchRemover.java
@@ -0,0 +1,58 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.plugins.batch;
+
+import com.google.gerrit.reviewdb.client.Branch;
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.server.project.NoSuchProjectException;
+import com.google.gerrit.server.util.RefUpdater;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.batch.exception.NoSuchBatchException;
+import java.io.IOException;
+
+public class BatchRemover {
+  protected final RefUpdater refUpdater;
+  protected final BatchStore store;
+
+  @Inject
+  protected BatchRemover(RefUpdater refUpdater, BatchStore store) {
+    this.refUpdater = refUpdater;
+    this.store = store;
+  }
+
+  public Batch remove(String id)
+      throws IllegalStateException, IOException, NoSuchBatchException, NoSuchProjectException {
+    return remove(store.read(id));
+  }
+
+  public Batch remove(Batch batch)
+      throws IOException, IllegalStateException, NoSuchProjectException {
+    if (batch.state == Batch.State.OPEN) {
+      throw new IllegalStateException(
+          "Invalid Operation for Batch(" + batch.id + "): " + batch.state.toString());
+    }
+    removeDownloadRefs(batch);
+    batch.state = Batch.State.DELETED;
+    store.save(batch);
+    return batch;
+  }
+
+  protected void removeDownloadRefs(Batch batch) throws IOException, NoSuchProjectException {
+    for (Batch.Destination dest : batch.listDestinations()) {
+      Project.NameKey project = new Project.NameKey(dest.project);
+      Branch.NameKey branch = new Branch.NameKey(project, dest.downloadRef);
+      refUpdater.delete(branch);
+    }
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
index 92e52d8..2d34784 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
@@ -48,6 +48,10 @@
   }
 
   public void save(Batch batch) throws IOException, NoSuchProjectException {
+    if (batch.state == Batch.State.DELETED) {
+      refUpdater.delete(getBranch(batch.id));
+      return;
+    }
     if (batch.version != 0) {
       throw new ConcurrentModificationException();
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/DeleteCommand.java b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/DeleteCommand.java
new file mode 100644
index 0000000..aeec6b7
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/DeleteCommand.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.plugins.batch.ssh;
+
+import com.google.gerrit.common.data.GlobalCapability;
+import com.google.gerrit.extensions.annotations.RequiresCapability;
+import com.google.gerrit.server.OutputFormat;
+import com.google.gerrit.sshd.CommandMetaData;
+import com.google.gerrit.sshd.SshCommand;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.batch.Batch;
+import com.googlesource.gerrit.plugins.batch.BatchRemover;
+import com.googlesource.gerrit.plugins.batch.exception.NoSuchBatchException;
+import org.kohsuke.args4j.Argument;
+
+@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
+@CommandMetaData(name = "delete", description = "Delete a batch")
+public class DeleteCommand extends SshCommand {
+  @Argument(metaVar = "BATCH-ID", usage = "id of the batch to delete")
+  protected String batchId;
+
+  @Inject protected BatchRemover impl;
+
+  @Override
+  public void run() throws Exception {
+    try {
+      Batch batch = impl.remove(batchId);
+      out.write((OutputFormat.JSON.newGson().toJson(batch) + "\n").getBytes(ENC));
+    } catch (NoSuchBatchException | IllegalStateException e) {
+      throw new UnloggedFailure(1, e.getMessage());
+    }
+    out.flush();
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/SshModule.java b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/SshModule.java
index 0c84155..6c7d22d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/SshModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/SshModule.java
@@ -23,5 +23,6 @@
     install(new FactoryModuleBuilder().build(GitFile.Factory.class));
 
     command(MergeChangeCommand.class);
+    command(DeleteCommand.class);
   }
 }
diff --git a/src/main/resources/Documentation/cmd-delete.md b/src/main/resources/Documentation/cmd-delete.md
new file mode 100644
index 0000000..f59a6f5
--- /dev/null
+++ b/src/main/resources/Documentation/cmd-delete.md
@@ -0,0 +1,34 @@
+@PLUGIN@ delete
+===============
+
+NAME
+----
+@PLUGIN@ delete - Delete a batch.
+
+SYNOPSIS
+--------
+```
+ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ delete <BATCH-ID>
+```
+
+DESCRIPTION
+-----------
+Delete the batch and any refs it points to.
+
+ACCESS
+------
+Any user who has configured an SSH key, and is a member
+of the privileged 'Administrators' group.
+
+SCRIPTING
+---------
+This command is intended to be used in scripts.
+
+EXAMPLES
+--------
+
+Delete a batch:
+
+```
+ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ delete 0644a132-5b79-4c88-bf22-9364a1d02deb
+```
diff --git a/test/test_batch_merge.sh b/test/test_batch_merge.sh
index 17bdd49..50ee448 100755
--- a/test/test_batch_merge.sh
+++ b/test/test_batch_merge.sh
@@ -203,6 +203,17 @@
 result_out "$GROUP change_state" "NEW" "$(query_by "$qchange" "status")"
 
 
+setupGroup "delete" "Batch Delete" # -------------
+
+ch1=$(create_change "$REF_BRANCH" "$FILE_A") || exit
+bjson=$(batchssh merge-change --close "$ch1",1)
+id=$(b_id)
+delete=$(batchssh delete "$id")
+result "$GROUP" "$delete"
+! delete=$(batchssh delete "$id")
+result "$GROUP retry" "$delete"
+
+
 setupGroup "independent clean" "Independent changes, clean merge" # ------------
 
 ch1=$(create_change "$REF_BRANCH" "$FILE_A") || exit