aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core
diff options
context:
space:
mode:
authorPierre <pierre.guillot@sonarsource.com>2021-03-02 10:52:52 +0100
committersonartech <sonartech@sonarsource.com>2021-03-08 20:07:54 +0000
commit129ca5f3534a69799f5ea95975c3f0a49fbb2804 (patch)
treea47445e1d0f0e84b2a6a6d4aa85e517a474a77d7 /server/sonar-webserver-core
parentd03ccfee3c9e9d5ab9583b02f1192a344d2b1147 (diff)
downloadsonarqube-129ca5f3534a69799f5ea95975c3f0a49fbb2804.tar.gz
sonarqube-129ca5f3534a69799f5ea95975c3f0a49fbb2804.zip
SONAR-14519 add ALM info to system info file
Diffstat (limited to 'server/sonar-webserver-core')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java4
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java76
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java4
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java88
4 files changed, 169 insertions, 3 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
index ca2bc9a8c2b..ee41ff6c490 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
@@ -22,6 +22,7 @@ package org.sonar.server.platform;
import org.sonar.core.platform.Module;
import org.sonar.process.systeminfo.JvmPropertiesSection;
import org.sonar.process.systeminfo.JvmStateSection;
+import org.sonar.server.platform.monitoring.AlmConfigurationSection;
import org.sonar.server.platform.monitoring.DbConnectionSection;
import org.sonar.server.platform.monitoring.DbSection;
import org.sonar.server.platform.monitoring.EsIndexesSection;
@@ -58,7 +59,8 @@ public class SystemInfoWriterModule extends Module {
EsIndexesSection.class,
LoggingSection.class,
PluginsSection.class,
- SettingsSection.class
+ SettingsSection.class,
+ AlmConfigurationSection.class
);
if (standalone) {
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java
new file mode 100644
index 00000000000..5af5cfa6f52
--- /dev/null
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java
@@ -0,0 +1,76 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.platform.monitoring;
+
+import java.util.List;
+import org.sonar.api.server.ServerSide;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.process.systeminfo.SystemInfoSection;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+
+import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
+
+@ServerSide
+public class AlmConfigurationSection implements SystemInfoSection {
+ private final DbClient dbClient;
+
+ public AlmConfigurationSection(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public ProtobufSystemInfo.Section toProtobuf() {
+ ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
+ protobuf.setName("ALMs");
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ List<AlmSettingDto> almSettingDtos = dbClient.almSettingDao().selectAll(dbSession);
+
+ for (AlmSettingDto almSettingDto : almSettingDtos) {
+ setAttribute(protobuf, almSettingDto.getKey(), buildValue(almSettingDto));
+ }
+ }
+
+ return protobuf.build();
+ }
+
+ private static String buildValue(AlmSettingDto almSettingDto) {
+ String value = String.format("alm:%s", almSettingDto.getRawAlm());
+ if (almSettingDto.getUrl() != null) {
+ value += String.format(", url:%s", almSettingDto.getUrl());
+ }
+ switch (almSettingDto.getAlm()) {
+ case GITHUB:
+ // add APP_ID and CLIENT_ID
+ value += String.format(", appId:%s, clientId:%s", almSettingDto.getAppId(), almSettingDto.getClientId());
+ break;
+ case BITBUCKET_CLOUD:
+ // WORKSPACE ID & OAuth key
+ value += String.format(", workspace id:%s, OAuth Key:%s", almSettingDto.getAppId(), almSettingDto.getClientId());
+ break;
+ default:
+ // no additional information for the other ALMs
+ break;
+ }
+ return value;
+ }
+}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
index 1965b19940f..4c190bebb06 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
@@ -42,7 +42,7 @@ public class SystemInfoWriterModuleTest {
Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters();
assertThat(adapters)
- .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 17);
+ .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 18);
}
@Test
@@ -54,7 +54,7 @@ public class SystemInfoWriterModuleTest {
Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters();
assertThat(adapters)
- .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11);
+ .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 12);
}
}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java
new file mode 100644
index 00000000000..08b858c2ee2
--- /dev/null
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java
@@ -0,0 +1,88 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.platform.monitoring;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Attribute;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class AlmConfigurationSectionTest {
+
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private AlmConfigurationSection underTest = new AlmConfigurationSection(db.getDbClient());
+
+ @Test
+ public void alm_are_listed() {
+ AlmSettingDto azure = db.almSettings().insertAzureAlmSetting();
+ AlmSettingDto github = db.almSettings().insertGitHubAlmSetting();
+ AlmSettingDto gitlab = db.almSettings().insertGitlabAlmSetting();
+ AlmSettingDto bitbucket = db.almSettings().insertBitbucketAlmSetting();
+ AlmSettingDto bitbucketCloud = db.almSettings().insertBitbucketCloudAlmSetting();
+
+ ProtobufSystemInfo.Section section = underTest.toProtobuf();
+
+ assertThat(section.getAttributesList()).hasSize(5);
+ assertThat(section.getAttributesList())
+ .extracting(Attribute::getKey, Attribute::getStringValue)
+ .containsExactlyInAnyOrder(
+ tuple(azure.getKey(), String.format("alm:%s, url:%s", azure.getRawAlm(), azure.getUrl())),
+ tuple(github.getKey(), String.format("alm:%s, url:%s, appId:%s, clientId:%s", github.getRawAlm(), github.getUrl(), github.getAppId(), github.getClientId())),
+ tuple(gitlab.getKey(), String.format("alm:%s, url:%s", gitlab.getRawAlm(), gitlab.getUrl())),
+ tuple(bitbucket.getKey(), String.format("alm:%s, url:%s", bitbucket.getRawAlm(), bitbucket.getUrl())),
+ tuple(bitbucketCloud.getKey(), String.format("alm:%s, workspace id:%s, OAuth Key:%s", bitbucketCloud.getRawAlm(), bitbucketCloud.getAppId(), bitbucketCloud.getClientId())));
+ }
+
+ @Test
+ public void several_alm_same_type() {
+ AlmSettingDto gitlab1 = db.almSettings().insertGitlabAlmSetting();
+ AlmSettingDto gitlab2 = db.almSettings().insertGitlabAlmSetting();
+
+ ProtobufSystemInfo.Section section = underTest.toProtobuf();
+
+ assertThat(section.getAttributesList()).hasSize(2);
+ assertThat(section.getAttributesList())
+ .extracting(Attribute::getKey, Attribute::getStringValue)
+ .containsExactlyInAnyOrder(
+ tuple(gitlab1.getKey(), String.format("alm:%s, url:%s", gitlab1.getRawAlm(), gitlab1.getUrl())),
+ tuple(gitlab2.getKey(), String.format("alm:%s, url:%s", gitlab2.getRawAlm(), gitlab2.getUrl())));
+ }
+
+ @Test
+ public void null_url_are_ignored() {
+ AlmSettingDto azure = db.almSettings().insertAzureAlmSetting(a -> a.setUrl(null));
+
+ ProtobufSystemInfo.Section section = underTest.toProtobuf();
+
+ assertThat(section.getAttributesList()).hasSize(1);
+ assertThat(section.getAttributesList())
+ .extracting(Attribute::getKey, Attribute::getStringValue)
+ .containsExactlyInAnyOrder(
+ tuple(azure.getKey(), String.format("alm:%s", azure.getRawAlm())));
+ }
+
+}