Add support for just UI plugins (JS)

Change-Id: Ie0e466cf52606c1eaa4f793fd314db3cb20dc869
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/repository/JenkinsCiPluginsRepository.java b/src/main/java/com/googlesource/gerrit/plugins/manager/repository/JenkinsCiPluginsRepository.java
index 76254a6..22f78b4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/repository/JenkinsCiPluginsRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/repository/JenkinsCiPluginsRepository.java
@@ -123,11 +123,27 @@
       return Optional.empty();
     }
 
-    Optional<SmartJson> artifactJson = artifacts.flatMap(a -> findArtifact(a, ".jar"));
-    if (!artifactJson.isPresent()) {
-      return Optional.empty();
+    Optional<SmartJson> artifactJavaJson = artifacts.flatMap(a -> findArtifact(a, ".jar"));
+    if (artifactJavaJson.isPresent()) {
+      Optional<PluginInfo>javaArtifact = getJavaPluginArtifactInfo(buildExecution, artifacts, artifactJavaJson);
+      if (javaArtifact.isPresent()) {
+        return javaArtifact;
+      }
     }
 
+    Optional<SmartJson> artifactJsJson = artifacts.flatMap(a -> findArtifact(a, ".js"));
+    if (artifactJsJson.isPresent()) {
+      Optional<PluginInfo> jsArtifact = getJsPluginArtifactInfo(buildExecution, artifacts, artifactJsJson);
+      if (jsArtifact.isPresent()) {
+        return jsArtifact;
+      }
+    }
+
+    return Optional.empty();
+  }
+
+
+  private Optional<PluginInfo> getJavaPluginArtifactInfo(Optional<SmartJson> buildExecution, Optional<JsonArray> artifacts, Optional<SmartJson> artifactJson) {
     String pluginPath = artifactJson.get().getString("relativePath");
 
     String[] pluginPathParts = pluginPath.split("/");
@@ -160,6 +176,36 @@
     return Optional.empty();
   }
 
+  private Optional<PluginInfo> getJsPluginArtifactInfo(Optional<SmartJson> buildExecution, Optional<JsonArray> artifacts, Optional<SmartJson> artifactJson) {
+    String pluginPath = artifactJson.get().getString("relativePath");
+
+    String[] pluginPathParts = pluginPath.split("/");
+    String pluginName = pluginNameOfJs(pluginPathParts);
+
+    String pluginUrl =
+        String.format("%s/artifact/%s", buildExecution.get().getString("url"), pluginPath);
+
+    Optional<String> pluginVersion =
+        fetchArtifact(buildExecution.get(), artifacts.get(), ".js-version");
+    Optional<String> pluginDescription =
+        fetchArtifactJson(buildExecution.get(), artifacts.get(), ".json")
+            .flatMap(json -> json.getOptionalString("description"));
+
+    for (JsonElement elem : buildExecution.get().get("actions").get().getAsJsonArray()) {
+      SmartJson elemJson = SmartJson.of(elem);
+      Optional<SmartJson> lastBuildRevision = elemJson.getOptional("lastBuiltRevision");
+
+      if (lastBuildRevision.isPresent()) {
+        String sha1 = lastBuildRevision.get().getString("SHA1").substring(0, 8);
+        return pluginVersion.map(
+            version ->
+                new PluginInfo(pluginName, pluginDescription.orElse(""), version, sha1, pluginUrl));
+      }
+    }
+
+    return Optional.empty();
+  }
+
   private Optional<String> fetchArtifact(
       SmartJson buildExecution, JsonArray artifacts, String artifactSuffix) {
     StringBuilder artifactBody = new StringBuilder();
@@ -227,6 +273,18 @@
     return pluginJarParts[filePos].substring(0, jarExtPos);
   }
 
+  private String pluginNameOfJs(String[] pluginJsParts) {
+    int filePos = pluginJsParts.length - 1;
+    int pathPos = filePos - 1;
+
+    if (pluginJsParts[filePos].startsWith(pluginJsParts[pathPos])) {
+      return pluginJsParts[pathPos];
+    }
+
+    int jsExtPos = pluginJsParts[filePos].indexOf(".js");
+    return pluginJsParts[filePos].substring(0, jsExtPos);
+  }
+
   private boolean isMavenBuild(String[] pluginPathParts) {
     return pluginPathParts[pluginPathParts.length - 2].equals("target");
   }
diff --git a/src/main/resources/static/js/plugin-manager.js b/src/main/resources/static/js/plugin-manager.js
index d786e90..04a3cc3 100644
--- a/src/main/resources/static/js/plugin-manager.js
+++ b/src/main/resources/static/js/plugin-manager.js
@@ -129,7 +129,8 @@
         };
         $("button#" + id).addClass("hidden");
         $("span#installing-" + id).removeClass("hidden");
-        $http.put($scope.getBaseUrl() + '/a/plugins/' + id + ".jar", pluginInstallData).then(
+        var fileEnding = url.endsWith('.js') ? ".js" : ".jar";
+        $http.put($scope.getBaseUrl() + '/a/plugins/' + id + fileEnding, pluginInstallData).then(
             function successCallback(response) {
               $("span#installing-" + id).addClass("hidden");
               $("span#installed-" + id).removeClass("hidden");