Perform actions on task id instead of story id

It makes more sense to link gerrit changes to tasks than to stories.
However storyboard tasks do not contain comments, stories do, therefore
we still want to add comments to the stories object. To do that we
we look up the story using the task id.

Change-Id: I00e07e3f2ad23358457fcfa9f47b8ffd8dbe6d9f
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/storyboard/StoryboardClient.java b/src/main/java/com/googlesource/gerrit/plugins/its/storyboard/StoryboardClient.java
index f5f52d7..062c3fd 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/storyboard/StoryboardClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/storyboard/StoryboardClient.java
@@ -14,11 +14,9 @@
 
 package com.googlesource.gerrit.plugins.its.storyboard;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.HttpURLConnection;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+
 import org.apache.http.StatusLine;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
@@ -29,14 +27,21 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.HttpURLConnection;
+
 
 public class StoryboardClient {
 
-  private static final Logger log = LoggerFactory.getLogger(
-      StoryboardClient.class);
+  private static final Logger log =
+      LoggerFactory.getLogger(StoryboardClient.class);
 
   public static final String STORIES_ENDPOINT = "/api/v1/stories";
   public static final String SYS_INFO_ENDPOINT = "/api/v1/systeminfo";
+  public static final String TASKS_ENDPOINT = "/api/v1/tasks";
 
   private final String baseUrl;
   private final String password;
@@ -51,7 +56,7 @@
 
     HttpGet httpget = new HttpGet(url);
     try (CloseableHttpClient client = HttpClients.createDefault();
-      CloseableHttpResponse response = client.execute(httpget)) {
+        CloseableHttpResponse response = client.execute(httpget)) {
       log.debug("Making request for " + httpget.getRequestLine());
       StatusLine sl = response.getStatusLine();
       int responseCode = sl.getStatusCode();
@@ -66,16 +71,15 @@
         log.debug("Data retreived: " + responseJson);
         return responseJson;
       } else {
-        log.error("Failed request: " + httpget.getRequestLine() +
-            " with response: " + responseCode);
+        log.error("Failed request: " + httpget.getRequestLine()
+            + " with response: " + responseCode);
       }
     }
     return null;
   }
 
   // generic method to POST data with a REST endpoint
-  public void postData(final String url, final String data)
-      throws IOException {
+  public void postData(final String url, final String data) throws IOException {
 
     HttpPost httpPost = new HttpPost(url);
     httpPost.addHeader("Authorization", "Bearer " + password);
@@ -88,8 +92,8 @@
       if (responseCode == HttpURLConnection.HTTP_OK) {
         log.info("Updated " + url + " with " + data);
       } else {
-        log.error("Failed to post, response: " + responseCode +
-            " (" + response.getStatusLine().getReasonPhrase() + ")");
+        log.error("Failed to post, response: " + responseCode + " ("
+            + response.getStatusLine().getReasonPhrase() + ")");
       }
     }
   }
@@ -98,18 +102,29 @@
     return getData(this.baseUrl + SYS_INFO_ENDPOINT);
   }
 
-  public String getStory(final String issueId) throws IOException {
-    return getData(this.baseUrl + STORIES_ENDPOINT + "/" + issueId);
+  public String getStory(final String id) throws IOException {
+    return getData(this.baseUrl + STORIES_ENDPOINT + "/" + getStoryId(id));
+  }
+
+  public int getStoryId(final String issueId) throws IOException {
+    String taskJson = getTask(issueId);
+    JsonObject jobj = new Gson().fromJson(taskJson, JsonObject.class);
+    return jobj.get("story_id").getAsInt();
+  }
+
+  public String getTask(final String issueId) throws IOException {
+    return getData(this.baseUrl + TASKS_ENDPOINT + "/" + issueId);
   }
 
   public void addComment(final String issueId, final String comment)
       throws IOException {
-    log.debug("Posting comment with data: ({},{})", issueId, comment);
-    final String url = baseUrl+STORIES_ENDPOINT+"/"+issueId+"/comments";
+    int story_id = getStoryId(issueId);
+    log.debug("Posting comment with data: ({},{})", story_id, comment);
+    final String url =
+        baseUrl + STORIES_ENDPOINT + "/" + story_id + "/comments";
     final String escapedComment = comment.replace("\n", "\\n");
-    final String json =
-        "{\"story_id\":\"" + issueId + "\",\"content\":\"" +
-        escapedComment + "\"}";
+    final String json = "{\"story_id\":\"" + issueId + "\",\"content\":\""
+        + escapedComment + "\"}";
 
     postData(url, json);
   }