Merge branch 'stable-2.16'

* stable-2.16:
  Support multiple Jira instances

Change-Id: Ibd8ba989f4ffca8479e0b1c96528feaeefd9c2ff
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
index c17affd..ab6cba2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
@@ -30,6 +30,7 @@
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraRestApiProvider;
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraServerInfo;
 import com.googlesource.gerrit.plugins.its.jira.restapi.JiraTransition;
+import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVersion;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
@@ -101,6 +102,13 @@
     }
   }
 
+  public void createVersion(String projectKey, String version) throws IOException {
+    log.debug("Trying to create version {} on project {}", version, projectKey);
+    JiraVersion jiraVersion = JiraVersion.builder().project(projectKey).name(version).build();
+    apiBuilder.getVersions().doPost("", gson.toJson(jiraVersion), HTTP_CREATED);
+    log.debug("Version {} created on project {}", version, projectKey);
+  }
+
   /**
    * @param issueKey Jira Issue key
    * @param transition JiraTransition.Item to perform
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
index 866a664..4480c08 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacade.java
@@ -90,6 +90,16 @@
   }
 
   @Override
+  public void createVersion(String projectKey, String version) throws IOException {
+    execute(
+        () -> {
+          log.debug("Creating version {} on project {}", version, projectKey);
+          jiraClient.createVersion(projectKey, version);
+          return projectKey;
+        });
+  }
+
+  @Override
   public boolean exists(String issueKey) throws IOException {
     return execute(() -> jiraClient.issueExists(getJiraServerInstance(), issueKey));
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
index 2e31d1b..17809ab 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
@@ -39,4 +39,8 @@
   public JiraRestApi<JiraProject[]> getProjects(JiraItsServerInfo serverInfo) {
     return get(serverInfo, JiraProject[].class, "/project");
   }
+
+  public JiraRestApi<JiraVersion[]> getVersions() {
+    return get(JiraVersion[].class, "/version");
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java
new file mode 100644
index 0000000..60a0b67
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java
@@ -0,0 +1,135 @@
+// Copyright (C) 2018 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.its.jira.restapi;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/** Represents a version in JIRA. */
+public class JiraVersion {
+
+  private final String description;
+  private final String name;
+  private final boolean archived;
+  private final boolean released;
+  private final String releaseDate;
+  private final String project;
+  private final Long projectId;
+
+  private JiraVersion(
+      String description,
+      String name,
+      boolean archived,
+      boolean released,
+      Date releaseDate,
+      String project,
+      Long projectId) {
+    this.description = description;
+    this.name = name;
+    this.archived = archived;
+    this.released = released;
+    if (releaseDate == null) {
+      this.releaseDate = null;
+    } else {
+      this.releaseDate = new SimpleDateFormat("yyyy-MM-dd").format(releaseDate);
+    }
+    this.project = project;
+    this.projectId = projectId;
+  }
+
+  public String getDescription() {
+    return description;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public boolean isArchived() {
+    return archived;
+  }
+
+  public boolean isReleased() {
+    return released;
+  }
+
+  public String getReleaseDate() {
+    return releaseDate;
+  }
+
+  public String getProject() {
+    return project;
+  }
+
+  public Long getProjectId() {
+    return projectId;
+  }
+
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  public static class Builder {
+    private String description;
+    private String name;
+    private boolean archived;
+    private boolean released;
+    private Date releaseDate;
+    private String project;
+    private Long projectId;
+
+    private Builder() {}
+
+    public Builder description(String description) {
+      this.description = description;
+      return this;
+    }
+
+    public Builder name(String name) {
+      this.name = name;
+      return this;
+    }
+
+    public Builder archived(boolean archived) {
+      this.archived = archived;
+      return this;
+    }
+
+    public Builder released(boolean released) {
+      this.released = released;
+      return this;
+    }
+
+    public Builder releaseDate(Date releaseDate) {
+      this.releaseDate = releaseDate;
+      return this;
+    }
+
+    public Builder project(String project) {
+      this.project = project;
+      return this;
+    }
+
+    public Builder projectId(Long projectId) {
+      this.projectId = projectId;
+      return this;
+    }
+
+    public JiraVersion build() {
+      return new JiraVersion(
+          description, name, archived, released, releaseDate, project, projectId);
+    }
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
index 5ec0021..e277f46 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraITTest.java
@@ -257,7 +257,7 @@
 
   private void createChangeWithIssue() throws Exception {
     pushFactory
-        .create(db, admin.getIdent(), testRepo, JIRA_ISSUE, "a.txt", "test")
+        .create(admin.getIdent(), testRepo, JIRA_ISSUE, "a.txt", "test")
         .to("refs/for/master");
   }
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
index 9241d36..56dfb09 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsFacadeTest.java
@@ -31,6 +31,7 @@
   private static final String ACTION = "action";
   private static final String COMMENT = "comment";
   private static final String ISSUE_KEY = "issueKey";
+  private static final String PROJECT_KEY = "projectKey";
 
   @Mock private JiraClient jiraClient;
   private JiraItsServerInfo server;
@@ -72,6 +73,13 @@
   }
 
   @Test
+  public void createVersion() throws IOException {
+    jiraFacade = new JiraItsFacade(jiraClient);
+    jiraFacade.createVersion(PROJECT_KEY, "1.0");
+    verify(jiraClient).createVersion(PROJECT_KEY, "1.0");
+  }
+
+  @Test
   public void exists() throws IOException {
     jiraFacade = new JiraItsFacade(jiraClient);
     jiraFacade.exists(ISSUE_KEY);