Adds the option “--include-batch-info” to ls-batches

This adds the option to output additional information when listing batches.

Change-Id: I1567ce6faf9d576a7b9acbfbacf7ae2fc5c84af6
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 749f8de..1e168b9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
@@ -29,8 +29,10 @@
 import java.util.ConcurrentModificationException;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import javax.inject.Singleton;
 import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 
 @Singleton
@@ -56,12 +58,12 @@
     this.refUpdater = refUpdater;
   }
 
-  /** Returns barebones batch objects for listings */
-  public List<Batch> find() throws IOException {
+  /** Returns a list of batch objects */
+  public List<Batch> find(boolean includeBatchInfo) throws IOException, NoSuchBatchException {
     List<Batch> batches = new ArrayList<>();
     try (Repository repo = repoManager.openRepository(project)) {
-      for (String batchId : repo.getRefDatabase().getRefs(BATCHES_REF).keySet()) {
-        batches.add(new Batch(batchId));
+      for (Map.Entry<String, Ref> entry : repo.getRefDatabase().getRefs(BATCHES_REF).entrySet()) {
+        batches.add((includeBatchInfo == true) ? read(entry.getKey()) : new Batch(entry.getKey()));
       }
     }
     return batches;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/ListBatches.java b/src/main/java/com/googlesource/gerrit/plugins/batch/ListBatches.java
index 4757f89..d183d98 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/ListBatches.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/ListBatches.java
@@ -19,8 +19,8 @@
 import com.google.gerrit.extensions.annotations.RequiresCapability;
 import com.google.gerrit.server.OutputFormat;
 import com.google.gson.reflect.TypeToken;
-import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.batch.exception.NoSuchBatchException;
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -28,9 +28,13 @@
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
+import org.kohsuke.args4j.Option;
 
 @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
 public class ListBatches {
+  @Option(name = "--include-batch-info", usage = "include additional information for every batch")
+  protected boolean includeBatchInfo;
+
   protected final BatchStore store;
 
   @Inject
@@ -38,7 +42,7 @@
     this.store = store;
   }
 
-  public void display(OutputStream displayOutputStream) throws IOException, OrmException {
+  public void display(OutputStream displayOutputStream) throws IOException, NoSuchBatchException {
     try {
       PrintWriter stdout =
           new PrintWriter(new BufferedWriter(new OutputStreamWriter(displayOutputStream, UTF_8)));
@@ -55,7 +59,7 @@
     }
   }
 
-  public List<Batch> getBatches() throws IOException {
-    return store.find();
+  public List<Batch> getBatches() throws IOException, NoSuchBatchException {
+    return store.find(includeBatchInfo);
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 617f3c1..75bf414 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -20,6 +20,7 @@
 merging changes to it, and closing the batch, all in one simple
 command like this:
 
+<a name="batchexample"></a>
 ```
 $ ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ merge-change 123,3 456,7 --close
   {
diff --git a/src/main/resources/Documentation/cmd-ls-batches.md b/src/main/resources/Documentation/cmd-ls-batches.md
index 45947cb..76c0071 100644
--- a/src/main/resources/Documentation/cmd-ls-batches.md
+++ b/src/main/resources/Documentation/cmd-ls-batches.md
@@ -9,8 +9,15 @@
 --------
 ```
 ssh -p @SSH_PORT@ @SSH_HOST@ @PLUGIN@ ls-batches
+  [--include-batch-info]
 ```
 
+OPTIONS
+-----------
+**\-\-include-batch-info**
+
+: Include additional information for every batch. ([example batch info](about.md#batchexample))
+
 DESCRIPTION
 -----------
 Displays the list of batches, in JSON.
@@ -40,3 +47,40 @@
     }
   ]
 ```
+
+List visible batches with info:
+```
+  ssh -p 29418 review.example.com batch ls-batches --include-batch-info
+  [
+    {
+      "destinations":[
+        {
+          "changes":[
+            {
+              "number":22,
+              "patch_set":1
+            },
+            {
+              "number":21,
+              "patch_set":1
+            },
+            {
+              "number":402,
+              "patch_set":1
+            }
+          ],
+          "download_ref":"refs/batch/users/jenkins/0644a132-5b79-4c88-bf22-9364a1d02deb/refs/heads/branchX",
+          "project":"projectA",
+          "ref":"refs/heads/branchX",
+          "sha1":"00de3cf878b8bd51fa56aa9a8d5e8631ae71ad60"
+        },
+      ],
+      "id":"0644a132-5b79-4c88-bf22-9364a1d02deb",
+      "last_modified":"July 22, 2014 10:43:28 AM",
+      "owner":{
+        "id":1000000
+      },
+      "state":"CLOSED"
+    }
+  ]
+```
diff --git a/test/test_batch_merge.sh b/test/test_batch_merge.sh
index 223d9e0..4f10234 100755
--- a/test/test_batch_merge.sh
+++ b/test/test_batch_merge.sh
@@ -221,6 +221,9 @@
 list=$(batchssh ls-batches)
 echo "$list"| grep '"id"'| grep -q "$(b_id)"
 result "$GROUP" "$list"
+listinfo=$(batchssh ls-batches --include-batch-info)
+echo "$listinfo"| grep -q '"last_modified'
+result "$GROUP last_modified" "listResult: $listinfo"
 
 
 setupGroup "independent clean" "Independent changes, clean merge" # ------------