]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6377 SONAR-6379 add update center refresh date to JSON response
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Apr 2015 20:12:16 +0000 (22:12 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 27 Apr 2015 07:33:38 +0000 (09:33 +0200)
add the date to the reponse of the plugin WS: available and updates

server/sonar-server/src/main/java/org/sonar/server/plugins/ws/AvailablePluginsWsAction.java
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatePluginsWsAction.java
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/UpdatesPluginsWsAction.java
server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-available_plugins.json
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AbstractUpdateCenterBasedPluginsWsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailablePluginsWsActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/plugins/ws/AvailablePluginsWsActionTest/properties_per_plugin.json

index 17e1eb7df4d49344313ba0a8b675f7097ac48380..8789ee043edcb0c7608091150ed26e7292d0f5ec 100644 (file)
@@ -21,14 +21,14 @@ package org.sonar.server.plugins.ws;
 
 import com.google.common.collect.ImmutableSortedSet;
 import com.google.common.io.Resources;
+import java.util.Collection;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.updatecenter.common.PluginUpdate;
-
-import java.util.Collection;
+import org.sonar.updatecenter.common.UpdateCenter;
 
 import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_PLUGIN_UPDATE_ORDERING;
 
@@ -49,6 +49,8 @@ public class AvailablePluginsWsAction implements PluginsWsAction {
   public void define(WebService.NewController controller) {
     controller.createAction("available")
       .setDescription("Get the list of all the plugins available for installation on the SonarQube instance, sorted by plugin name." +
+        "<br/>" +
+        "Plugin information is retrieved from Update Center. Date and time at which Update Center was last refreshed is provided in the response." +
         "<br/>" +
         "Update status values are: [COMPATIBLE, INCOMPATIBLE, REQUIRES_UPGRADE, DEPS_REQUIRE_UPGRADE]")
       .setSince("5.2")
@@ -61,25 +63,29 @@ public class AvailablePluginsWsAction implements PluginsWsAction {
     JsonWriter jsonWriter = response.newJsonWriter();
     jsonWriter.beginObject();
 
-    writePlugins(jsonWriter);
+    UpdateCenter updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH);
+
+    writePlugins(jsonWriter, updateCenter);
 
+    pluginWSCommons.writeUpdateCenterProperties(jsonWriter, updateCenter);
+
+    jsonWriter.endObject();
     jsonWriter.close();
   }
 
-  private void writePlugins(JsonWriter jsonWriter) {
-    jsonWriter.name(ARRAY_PLUGINS);
-    jsonWriter.beginArray();
-    for (PluginUpdate pluginUpdate : retrieveAvailablePlugins()) {
+  private void writePlugins(JsonWriter jsonWriter, UpdateCenter updateCenter) {
+    jsonWriter.name(ARRAY_PLUGINS).beginArray();
+    for (PluginUpdate pluginUpdate : retrieveAvailablePlugins(updateCenter)) {
       pluginWSCommons.writePluginUpdate(jsonWriter, pluginUpdate);
     }
     jsonWriter.endArray();
-    jsonWriter.endObject();
   }
 
-  private Collection<PluginUpdate> retrieveAvailablePlugins() {
+  private Collection<PluginUpdate> retrieveAvailablePlugins(UpdateCenter updateCenter) {
     return ImmutableSortedSet.copyOf(
-        NAME_KEY_PLUGIN_UPDATE_ORDERING,
-        updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH).findAvailablePlugins()
-    );
+      NAME_KEY_PLUGIN_UPDATE_ORDERING,
+      updateCenter.findAvailablePlugins()
+      );
   }
+
 }
index a1367b4e66db6e296838291b14a2c1eb3b1ed655..e4cfcba045aeb70b643708b4ffe5e3d4c7549e3e 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.updatecenter.common.Artifact;
 import org.sonar.updatecenter.common.Plugin;
 import org.sonar.updatecenter.common.PluginUpdate;
 import org.sonar.updatecenter.common.Release;
+import org.sonar.updatecenter.common.UpdateCenter;
 
 import javax.annotation.Nonnull;
 import java.util.Comparator;
@@ -55,15 +56,16 @@ public class PluginWSCommons {
   static final String OBJECT_UPDATE = "update";
   static final String OBJECT_RELEASE = "release";
   static final String ARRAY_REQUIRES = "requires";
+  static final String PROPERTY_UPDATE_CENTER_REFRESH = "updateCenterRefresh";
 
   public static final Ordering<PluginMetadata> NAME_KEY_PLUGIN_METADATA_COMPARATOR = Ordering.natural()
     .onResultOf(PluginMetadataToName.INSTANCE)
     .compound(Ordering.natural().onResultOf(PluginMetadataToKey.INSTANCE));
   public static final Comparator<Plugin> NAME_KEY_PLUGIN_ORDERING = Ordering.from(CASE_INSENSITIVE_ORDER)
-      .onResultOf(PluginToName.INSTANCE)
-      .compound(
-          Ordering.from(CASE_INSENSITIVE_ORDER).onResultOf(PluginToKey.INSTANCE)
-      );
+    .onResultOf(PluginToName.INSTANCE)
+    .compound(
+      Ordering.from(CASE_INSENSITIVE_ORDER).onResultOf(PluginToKey.INSTANCE)
+    );
   public static final Comparator<PluginUpdate> NAME_KEY_PLUGIN_UPDATE_ORDERING = Ordering.from(NAME_KEY_PLUGIN_ORDERING)
     .onResultOf(PluginUpdateToPlugin.INSTANCE);
 
@@ -212,6 +214,16 @@ public class PluginWSCommons {
     }
   }
 
+  /**
+   * Write properties of the specified UpdateCenter to the specified JsonWriter.
+   * <pre>
+   * "updateCenterRefresh": "2015-04-24T16:08:36+0200"
+   * </pre>
+   */
+  public void writeUpdateCenterProperties(JsonWriter jsonWriter, UpdateCenter updateCenter) {
+    jsonWriter.propDateTime(PROPERTY_UPDATE_CENTER_REFRESH, updateCenter.getDate());
+  }
+
   private enum ReleaseToArtifact implements Function<Release, Artifact> {
     INSTANCE;
 
index 888d9bead36a9ecbeb0481bf5b17408c1c5ba7c8..6b513d0d62bdcd73a772d74c16de4b1cdf5d7842 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.server.plugins.ws;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -30,9 +32,6 @@ import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.server.user.UserSession;
 import org.sonar.updatecenter.common.PluginUpdate;
 
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-
 import static java.lang.String.format;
 
 /**
@@ -56,8 +55,8 @@ public class UpdatePluginsWsAction implements PluginsWsAction {
     WebService.NewAction action = controller.createAction("update")
       .setPost(true)
       .setDescription("Updates a plugin specified by its key to the latest version compatible with the SonarQube instance." +
-          "<br/>" +
-          "Requires user to be authenticated with Administer System permissions")
+        "<br/>" +
+        "Requires user to be authenticated with Administer System permissions")
       .setHandler(this);
 
     action.createParam(PARAM_KEY)
index 16821425eec8d538d04b7f3fc2b25c803ce555eb..c361ca64141e693547390bd51b53d7fe82250b03 100644 (file)
  */
 package org.sonar.server.plugins.ws;
 
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableSortedSet;
-import com.google.common.collect.Ordering;
-import com.google.common.io.Resources;
+import java.util.Collection;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -31,10 +32,12 @@ import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.server.plugins.ws.PluginUpdateAggregator.PluginUpdateAggregate;
 import org.sonar.updatecenter.common.Plugin;
 import org.sonar.updatecenter.common.PluginUpdate;
+import org.sonar.updatecenter.common.UpdateCenter;
 
-import javax.annotation.Nonnull;
-import java.util.Collection;
-import java.util.List;
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableSortedSet;
+import com.google.common.collect.Ordering;
+import com.google.common.io.Resources;
 
 /**
  * Implementation of the {@code updates} action for the Plugins WebService.
@@ -73,6 +76,8 @@ public class UpdatesPluginsWsAction implements PluginsWsAction {
         "br/>" +
         "Each newer version is listed, ordered from the oldest to the newest, with its own update/compatibility status." +
         "<br/>" +
+        "Plugin information is retrieved from Update Center. Date and time at which Update Center was last refreshed is provided in the response." +
+        "<br/>" +
         "Update status values are: [COMPATIBLE, INCOMPATIBLE, REQUIRES_UPGRADE, DEPS_REQUIRE_UPGRADE]")
       .setSince("5.2")
       .setHandler(this)
@@ -84,19 +89,23 @@ public class UpdatesPluginsWsAction implements PluginsWsAction {
     JsonWriter jsonWriter = response.newJsonWriter();
     jsonWriter.beginObject();
 
-    writePlugins(jsonWriter);
+    UpdateCenter updateCenter = updateCenterMatrixFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH);
+
+    writePlugins(jsonWriter, updateCenter);
+
+    pluginWSCommons.writeUpdateCenterProperties(jsonWriter, updateCenter);
 
+    jsonWriter.endObject();
     jsonWriter.close();
   }
 
-  private void writePlugins(JsonWriter jsonWriter) {
+  private void writePlugins(JsonWriter jsonWriter, UpdateCenter updateCenter) {
     jsonWriter.name(ARRAY_PLUGINS);
     jsonWriter.beginArray();
     for (PluginUpdateAggregate aggregate : retrieveUpdatablePlugins()) {
       writePluginUpdateAggregate(jsonWriter, aggregate);
     }
     jsonWriter.endArray();
-    jsonWriter.endObject();
   }
 
   private void writePluginUpdateAggregate(JsonWriter jsonWriter, PluginUpdateAggregate aggregate) {
index 2cfc80ca7ae1f9078f2bf61fc756df137f02857e..b2c89f438d1400bbfe2f9b9fab44363201dbefbd 100644 (file)
@@ -49,5 +49,6 @@
         ]
       }
     }
-  ]
+  ],
+  "updateCenterRefresh": "2015-04-24T16:08:36+0200"
 }
\ No newline at end of file
index c570497b297e60172cabd49afce2f186d30212b4..fd4ab2ffb9c8bc7d0982024d66b06c68be755ded 100644 (file)
  */
 package org.sonar.server.plugins.ws;
 
+import java.net.URL;
 import org.junit.Before;
 import org.sonar.api.server.ws.Request;
+import org.sonar.api.utils.DateUtils;
 import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.server.ws.WsTester;
 import org.sonar.updatecenter.common.Plugin;
@@ -29,8 +31,6 @@ import org.sonar.updatecenter.common.Release;
 import org.sonar.updatecenter.common.UpdateCenter;
 import org.sonar.updatecenter.common.Version;
 
-import java.net.URL;
-
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -46,7 +46,6 @@ public class AbstractUpdateCenterBasedPluginsWsActionTest {
   protected static final Plugin PLUGIN_1 = new Plugin("p_key_1").setName("p_name_1");
   protected static final Plugin PLUGIN_2 = new Plugin("p_key_2").setName("p_name_2").setDescription("p_desc_2");
 
-
   protected UpdateCenterMatrixFactory updateCenterFactory = mock(UpdateCenterMatrixFactory.class);
   protected UpdateCenter updateCenter = mock(UpdateCenter.class);
   protected Request request = mock(Request.class);
@@ -75,5 +74,6 @@ public class AbstractUpdateCenterBasedPluginsWsActionTest {
   @Before
   public void wireMocksTogether() throws Exception {
     when(updateCenterFactory.getUpdateCenter(anyBoolean())).thenReturn(updateCenter);
+    when(updateCenter.getDate()).thenReturn(DateUtils.parseDateTime("2015-04-24T16:08:36+0200"));
   }
 }
index 27a1f24568676583c030af0ad2ed6488eb5827d2..44128dfdea64e509efdb14e2f2c12d175c3e0762 100644 (file)
@@ -39,18 +39,18 @@ import static org.sonar.updatecenter.common.PluginUpdate.Status.REQUIRE_SONAR_UP
 public class AvailablePluginsWsActionTest extends AbstractUpdateCenterBasedPluginsWsActionTest {
 
   private static final Plugin FULL_PROPERTIES_PLUGIN = new Plugin("p_key")
-      .setName("p_name")
-      .setCategory("p_category")
-      .setDescription("p_description")
-      .setLicense("p_license")
-      .setOrganization("p_orga_name")
-      .setOrganizationUrl("p_orga_url")
-      .setTermsConditionsUrl("p_t_and_c_url");
+    .setName("p_name")
+    .setCategory("p_category")
+    .setDescription("p_description")
+    .setLicense("p_license")
+    .setOrganization("p_orga_name")
+    .setOrganizationUrl("p_orga_url")
+    .setTermsConditionsUrl("p_t_and_c_url");
   private static final Release FULL_PROPERTIES_PLUGIN_RELEASE = release(FULL_PROPERTIES_PLUGIN, "1.12.1")
-      .setDate(DateUtils.parseDate("2015-04-16"))
-      .setDownloadUrl("http://p_file.jar")
-      .addOutgoingDependency(release(PLUGIN_1, "0.3.6"))
-      .addOutgoingDependency(release(PLUGIN_2, "1.0.0"));
+    .setDate(DateUtils.parseDate("2015-04-16"))
+    .setDownloadUrl("http://p_file.jar")
+    .addOutgoingDependency(release(PLUGIN_1, "0.3.6"))
+    .addOutgoingDependency(release(PLUGIN_2, "1.0.0"));
 
   private AvailablePluginsWsAction underTest = new AvailablePluginsWsAction(updateCenterFactory, new PluginWSCommons());
 
@@ -117,26 +117,26 @@ public class AvailablePluginsWsActionTest extends AbstractUpdateCenterBasedPlugi
     underTest.handle(request, response);
 
     assertJson(response.outputAsString()).isSimilarTo(
-        "{" +
-            "  \"plugins\": [" +
-            "    {" +
-            "      \"update\": {" +
-            "        \"status\": \"" + expectedValue + "\"" +
-            "      }" +
-            "    }" +
-            "  ]" +
-            "}"
-    );
+      "{" +
+        "  \"plugins\": [" +
+        "    {" +
+        "      \"update\": {" +
+        "        \"status\": \"" + expectedValue + "\"" +
+        "      }" +
+        "    }" +
+        "  ]" +
+        "}"
+      );
   }
 
   @Test
   public void plugins_are_sorted_by_name_then_key_and_made_unique() throws Exception {
     when(updateCenter.findAvailablePlugins()).thenReturn(of(
-        pluginUpdate("key2", "name2"),
-        pluginUpdate("key1", "name2"),
-        pluginUpdate("key2", "name2"),
-        pluginUpdate("key0", "name0"),
-        pluginUpdate("key1", "name1")
-    ));
+      pluginUpdate("key2", "name2"),
+      pluginUpdate("key1", "name2"),
+      pluginUpdate("key2", "name2"),
+      pluginUpdate("key0", "name0"),
+      pluginUpdate("key1", "name1")
+      ));
   }
 }