diff options
author | Philippe Perrin <philippe.perrin@sonarsource.com> | 2021-03-08 16:36:16 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-03-08 20:07:54 +0000 |
commit | d03ccfee3c9e9d5ab9583b02f1192a344d2b1147 (patch) | |
tree | b3853aec3f526e358a9d73192a8c08400910012f /server/sonar-webserver-core | |
parent | 25bbdfb327cdf34698a0dc999d169b499bb5b8d4 (diff) | |
download | sonarqube-d03ccfee3c9e9d5ab9583b02f1192a344d2b1147.tar.gz sonarqube-d03ccfee3c9e9d5ab9583b02f1192a344d2b1147.zip |
SONAR-14520 - Add Default Leak Period to System Info File
Diffstat (limited to 'server/sonar-webserver-core')
2 files changed, 64 insertions, 10 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java index facf3e6933b..b417929842d 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/SettingsSection.java @@ -19,16 +19,21 @@ */ 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(); + } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java index 6c6975f03b9..dab3c5f32ef 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/SettingsSectionTest.java @@ -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 |