]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14520 - Add Default Leak Period to System Info File
authorPhilippe Perrin <philippe.perrin@sonarsource.com>
Mon, 8 Mar 2021 15:36:16 +0000 (16:36 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 8 Mar 2021 20:07:54 +0000 (20:07 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java

index facf3e6933b7d699c44f79012872dbf0b760af79..b417929842dac0666ddafde4878fe80249b02edd 100644 (file)
  */
 package org.sonar.server.platform.monitoring;
 
-import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.TreeMap;
 import org.sonar.api.PropertyType;
 import org.sonar.api.config.PropertyDefinition;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.internal.Settings;
 import org.sonar.api.server.ServerSide;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.newcodeperiod.NewCodePeriodDto;
 import org.sonar.process.systeminfo.Global;
 import org.sonar.process.systeminfo.SystemInfoSection;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section.Builder;
 
 import static org.apache.commons.lang.StringUtils.abbreviate;
 import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
@@ -38,30 +43,44 @@ import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 @ServerSide
 public class SettingsSection implements SystemInfoSection, Global {
-
   private static final int MAX_VALUE_LENGTH = 500;
   private static final String PASSWORD_VALUE = "xxxxxxxx";
+
+  private final DbClient dbClient;
   private final Settings settings;
 
-  public SettingsSection(Settings settings) {
+  public SettingsSection(DbClient dbClient, Settings settings) {
+    this.dbClient = dbClient;
     this.settings = settings;
   }
 
   @Override
   public ProtobufSystemInfo.Section toProtobuf() {
-    ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
+    Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
     protobuf.setName("Settings");
 
     PropertyDefinitions definitions = settings.getDefinitions();
     TreeMap<String, String> orderedProps = new TreeMap<>(settings.getProperties());
-    for (Map.Entry<String, String> prop : orderedProps.entrySet()) {
-      String key = prop.getKey();
-      String value = obfuscateValue(definitions, key, prop.getValue());
-      setAttribute(protobuf, key, value);
+    for (Entry<String, String> prop : orderedProps.entrySet()) {
+      includeSetting(protobuf, definitions, prop);
     }
+    addDefaultNewCodeDefinition(protobuf);
     return protobuf.build();
   }
 
+  private static void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry<String, String> prop) {
+    String key = prop.getKey();
+    String value = obfuscateValue(definitions, key, prop.getValue());
+    setAttribute(protobuf, key, value);
+  }
+
+  private void addDefaultNewCodeDefinition(Builder protobuf) {
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      Optional<NewCodePeriodDto> period = dbClient.newCodePeriodDao().selectGlobal(dbSession);
+      setAttribute(protobuf, "Default New Code Definition", parseDefaultNewCodeDefinition(period));
+    }
+  }
+
   private static String obfuscateValue(PropertyDefinitions definitions, String key, String value) {
     PropertyDefinition def = definitions.get(key);
     if (def != null && def.type() == PropertyType.PASSWORD) {
@@ -75,4 +94,16 @@ public class SettingsSection implements SystemInfoSection, Global {
     }
     return abbreviate(value, MAX_VALUE_LENGTH);
   }
+
+  private static String parseDefaultNewCodeDefinition(Optional<NewCodePeriodDto> period) {
+    if (!period.isPresent()) {
+      return "PREVIOUS_VERSION";
+    }
+
+    if (period.get().getValue() == null) {
+      return period.get().getType().name();
+    }
+
+    return period.get().getType().name() + ": " + period.get().getValue();
+  }
 }
index 6c6975f03b95102fe040df27ae570faa448c5d0b..dab3c5f32eff4d98d8facf0c3aaac2bdae558024 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.platform.monitoring;
 
+import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.PropertyType;
 import org.sonar.api.config.PropertyDefinition;
@@ -26,6 +27,8 @@ import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.config.internal.Settings;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 
 import static org.apache.commons.lang.StringUtils.repeat;
@@ -37,9 +40,12 @@ public class SettingsSectionTest {
 
   private static final String PASSWORD_PROPERTY = "sonar.password";
 
+  @Rule
+  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
   private PropertyDefinitions defs = new PropertyDefinitions(System2.INSTANCE, PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build());
   private Settings settings = new MapSettings(defs);
-  private SettingsSection underTest = new SettingsSection(settings);
+  private SettingsSection underTest = new SettingsSection(dbTester.getDbClient(), settings);
 
   @Test
   public void return_properties_and_sort_by_key() {
@@ -49,11 +55,28 @@ public class SettingsSectionTest {
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
     assertThatAttributeIs(protobuf, "bar", "bar value");
     assertThatAttributeIs(protobuf, "foo", "foo value");
+    assertThatAttributeIs(protobuf, "Default New Code Definition", "PREVIOUS_VERSION");
 
     // keys are ordered alphabetically
     assertThat(protobuf.getAttributesList())
       .extracting(ProtobufSystemInfo.Attribute::getKey)
-      .containsExactly("bar", "foo");
+      .containsExactly("bar", "foo", "Default New Code Definition");
+  }
+
+  @Test
+  public void return_default_new_code_definition_with_no_specified_value() {
+    dbTester.newCodePeriods().insert(NewCodePeriodType.PREVIOUS_VERSION,null);
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeIs(protobuf, "Default New Code Definition", "PREVIOUS_VERSION");
+  }
+
+  @Test
+  public void return_default_new_code_definition_with_specified_value() {
+    dbTester.newCodePeriods().insert(NewCodePeriodType.NUMBER_OF_DAYS,"30");
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeIs(protobuf, "Default New Code Definition", "NUMBER_OF_DAYS: 30");
   }
 
   @Test