]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8636 Show functional version of SonarQube
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 9 Feb 2017 18:54:42 +0000 (19:54 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 10 Feb 2017 16:41:31 +0000 (17:41 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/ws/UpgradesAction.java
server/sonar-server/src/main/java/org/sonar/server/ui/VersionFormatter.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java
server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json
server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java
server/sonar-server/src/test/java/org/sonar/server/ui/VersionFormatterTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java

index be213af165eb85e16899d485af3114d59dfd2a70..bba11b579b35bf24f1e7917dac3e0c8b3c05cc10 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.updatecenter.common.Release;
 import org.sonar.updatecenter.common.SonarUpdate;
 import org.sonar.updatecenter.common.UpdateCenter;
 
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+
 /**
  * Implementation of the {@code upgrades} action for the System WebService.
  */
@@ -116,7 +118,9 @@ public class UpgradesAction implements SystemWsAction {
   }
 
   private static void writeMetadata(JsonWriter jsonWriter, Release release) {
-    jsonWriter.prop(PROPERTY_VERSION, release.getVersion().getName());
+    String technicalVersion = release.getVersion().getName();
+    String functionalVersion = release.getDisplayVersion();
+    jsonWriter.prop(PROPERTY_VERSION, isNotBlank(functionalVersion) ? functionalVersion : technicalVersion);
     jsonWriter.prop(PROPERTY_DESCRIPTION, release.getDescription());
     jsonWriter.propDate(PROPERTY_RELEASE_DATE, release.getDate());
     jsonWriter.prop(PROPERTY_CHANGE_LOG_URL, release.getChangelogUrl());
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/VersionFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/ui/VersionFormatter.java
new file mode 100644 (file)
index 0000000..dce4689
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.ui;
+
+import com.google.common.base.Splitter;
+import java.util.List;
+
+public class VersionFormatter {
+  private static final String VERSION_SEQUENCE_SEPARATION = ".";
+
+  private VersionFormatter() {
+    // prevent instantiation
+  }
+
+  public static String format(String technicalVersion) {
+    List<String> elements = Splitter.on(VERSION_SEQUENCE_SEPARATION).splitToList(technicalVersion);
+    if (elements.size() != 4) {
+      return technicalVersion;
+    }
+
+    // version has the form 6.3.1.4563
+    StringBuilder builder = new StringBuilder();
+    builder
+      .append(elements.get(0))
+      .append(VERSION_SEQUENCE_SEPARATION)
+      .append(elements.get(1));
+    if (!"0".equals(elements.get(2))) {
+      builder.append(VERSION_SEQUENCE_SEPARATION)
+        .append(elements.get(2));
+    }
+
+    builder.append(" (build ").append(elements.get(3)).append(")");
+
+    return builder.toString();
+  }
+}
index fd465766dc7d1da64c65fc84744ac875e3b2b573..6846a35048682c2071c9097bfccf31105d0861cb 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.db.Database;
 import org.sonar.db.dialect.H2;
 import org.sonar.server.ui.PageRepository;
+import org.sonar.server.ui.VersionFormatter;
 
 import static org.sonar.api.CoreProperties.RATING_GRID;
 import static org.sonar.core.config.WebConstants.SONARQUBE_DOT_COM_ENABLED;
@@ -122,7 +123,8 @@ public class GlobalAction implements NavigationWsAction {
   }
 
   private void writeVersion(JsonWriter json) {
-    json.prop("version", server.getVersion());
+    String displayVersion = VersionFormatter.format(server.getVersion());
+    json.prop("version", displayVersion);
   }
 
   private void writeDatabaseProduction(JsonWriter json) {
index 43ca2a76e17801384dd9eb6754f0da504d2a65e6..9d0d7aa4ae8634f42cc676d7996ec742e0a8d261 100644 (file)
@@ -1,7 +1,7 @@
 {
   "upgrades": [
     {
-      "version": "5.1",
+      "version": "5.1 (build 5498)",
       "description": "New overall layout, merge Issues Drilldown [...]",
       "releaseDate": "2015-04-02",
       "changeLogUrl": "http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=11694&version=20666",
@@ -17,7 +17,7 @@
             "organizationName": "SonarSource",
             "organizationUrl": "http://www.sonarsource.com",
             "termsAndConditionsUrl": "http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf",
-            "version": "2.8"
+            "version": "2.8 (build 5498)"
           }
         ],
         "incompatible": [
index bdf1a0884beb5334eed8455e4e770e819c0cb287..5d66052f6735d07aaae20db9ba476e54d3b9e4ca 100644 (file)
@@ -48,6 +48,7 @@ public class UpgradesActionTest {
     "{" +
       "  \"upgrades\":" + "[]" +
       "}";
+  private static Release release;
 
   private UpdateCenterMatrixFactory updateCenterFactory = mock(UpdateCenterMatrixFactory.class);
   private UpdateCenter updateCenter = mock(UpdateCenter.class);
@@ -108,6 +109,17 @@ public class UpgradesActionTest {
       .isSimilarTo(getClass().getResource("example-upgrades_plugins.json"));
   }
 
+  @Test
+  public void technical_version_when_functional_is_blank() throws Exception {
+    SonarUpdate sonarUpdate = createSonar_51_update();
+    when(updateCenter.findSonarUpdates()).thenReturn(of(sonarUpdate));
+    release.setDisplayVersion("");
+
+    underTest.handle(request, response);
+
+    assertThat(response.outputAsString()).contains("5.42");
+  }
+
   private static SonarUpdate createSonar_51_update() {
     Plugin brandingPlugin = Plugin.factory("branding")
       .setCategory("Integration")
@@ -130,13 +142,13 @@ public class UpgradesActionTest {
       .setTermsConditionsUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf")
       .setIssueTrackerUrl("http://jira.sonarsource.com/browse/VIEWS");
 
-    SonarUpdate sonarUpdate = new SonarUpdate(
-      new Release(new Sonar(), Version.create("5.1"))
-        .setDate(DateUtils.parseDate("2015-04-02"))
-        .setDescription("New overall layout, merge Issues Drilldown [...]")
-        .setDownloadUrl("http://dist.sonar.codehaus.org/sonarqube-5.1.zip")
-        .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=11694&version=20666")
-    );
+    release = new Release(new Sonar(), Version.create("5.42"))
+      .setDisplayVersion("5.1 (build 5498)")
+      .setDate(DateUtils.parseDate("2015-04-02"))
+      .setDescription("New overall layout, merge Issues Drilldown [...]")
+      .setDownloadUrl("http://dist.sonar.codehaus.org/sonarqube-5.1.zip")
+      .setChangelogUrl("http://jira.sonarsource.com/secure/ReleaseNote.jspa?projectId=11694&version=20666");
+    SonarUpdate sonarUpdate = new SonarUpdate(release);
 
     sonarUpdate.addIncompatiblePlugin(brandingPlugin);
     sonarUpdate.addPluginToUpgrade(new Release(viewsPlugin, Version.create("2.8")));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/VersionFormatterTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/VersionFormatterTest.java
new file mode 100644 (file)
index 0000000..f19ce87
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.ui;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class VersionFormatterTest {
+  @Test
+  public void format_technical_version() {
+    assertThat(format("6.3")).isEqualTo("6.3");
+    assertThat(format("6.3.2")).isEqualTo("6.3.2");
+    assertThat(format("6.3.2.5498")).isEqualTo("6.3.2 (build 5498)");
+    assertThat(format("6.3.0.5499")).isEqualTo("6.3 (build 5499)");
+  }
+
+  private static String format(String technicalVersion) {
+    return VersionFormatter.format(technicalVersion);
+  }
+}
index 9691696dc5652befc0b8633ae75776f8511b73a1..fe2adb8554c7c42045d485bbf71834e65b3f9096 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ui.PageRepository;
 import org.sonar.server.ws.WsActionTester;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -120,6 +121,26 @@ public class GlobalActionTest {
     executeAndVerify("version.json");
   }
 
+  @Test
+  public void functional_version_when_4_digits() throws Exception {
+    init();
+    when(server.getVersion()).thenReturn("6.3.1.1234");
+
+    String result = call();
+
+    assertThat(result).contains("6.3.1 (build 1234)");
+  }
+
+  @Test
+  public void functional_version_when_third_digit_is_0() throws Exception {
+    init();
+    when(server.getVersion()).thenReturn("6.3.0.1234");
+
+    String result = call();
+
+    assertThat(result).contains("6.3 (build 1234)");
+  }
+
   @Test
   public void return_if_production_database_or_not() throws Exception {
     init();
@@ -151,7 +172,7 @@ public class GlobalActionTest {
     when(server.getVersion()).thenReturn("6.2");
     when(database.getDialect()).thenReturn(new MySql());
 
-    String result = ws.newRequest().execute().getInput();
+    String result = call();
     assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
   }
 
@@ -161,6 +182,7 @@ public class GlobalActionTest {
 
   private void init(org.sonar.api.web.page.Page[] pages, ResourceTypeTree[] resourceTypeTrees) {
     when(database.getDialect()).thenReturn(new H2());
+    when(server.getVersion()).thenReturn("6.42");
     PluginRepository pluginRepository = mock(PluginRepository.class);
     when(pluginRepository.hasPlugin(anyString())).thenReturn(true);
     PageRepository pageRepository = new PageRepository(pluginRepository, new PageDefinition[] {context -> {
@@ -173,7 +195,11 @@ public class GlobalActionTest {
   }
 
   private void executeAndVerify(String json) {
-    assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource(GlobalActionTest.class.getSimpleName() + "/" + json));
+    assertJson(call()).isSimilarTo(getClass().getResource(GlobalActionTest.class.getSimpleName() + "/" + json));
+  }
+
+  private String call() {
+    return ws.newRequest().execute().getInput();
   }
 
   private Page[] createPages() {