]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14519 add ALM info to system info file
authorPierre <pierre.guillot@sonarsource.com>
Tue, 2 Mar 2021 09:52:52 +0000 (10:52 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 8 Mar 2021 20:07:54 +0000 (20:07 +0000)
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java [new file with mode: 0644]
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/GetGithubClientIdActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ListDefinitionsActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java

index 02ea532af16ba263ac874e165c903b39322f6b10..3ece1b02483815366b0f1a7545e0a66bc5a3f06d 100644 (file)
@@ -34,6 +34,8 @@ public class AlmSettingsTesting {
       .setKey(randomAlphanumeric(200))
       .setUrl(randomAlphanumeric(2000))
       .setAppId(randomNumeric(8))
+      .setClientId(randomNumeric(8))
+      .setClientSecret(randomAlphanumeric(80))
       .setPrivateKey(randomAlphanumeric(2000))
       .setAlm(ALM.GITHUB);
   }
index ca2bc9a8c2bc07a753dee962e0119941dcd58dac..ee41ff6c490501784de4db88b2cd246f8fa858f5 100644 (file)
@@ -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 (file)
index 0000000..5af5cfa
--- /dev/null
@@ -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;
+  }
+}
index 1965b19940f755ca40cfdda6f79305f8febbff8e..4c190bebb06a0fb0ddc2518ae29380063aeafa88 100644 (file)
@@ -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 (file)
index 0000000..08b858c
--- /dev/null
@@ -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())));
+  }
+
+}
index 082c330b8f823410aa0b4a8608ad55a6c6827a01..c935483a22d5cb604204d425cc1c537e738eac96 100644 (file)
@@ -84,7 +84,7 @@ public class GetGithubClientIdActionTest {
   public void fail_when_client_id_does_not_exist() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
-    AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
+    AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(s -> s.setClientId(null));
 
     TestRequest request = ws.newRequest()
         .setParam(GetGithubClientIdAction.PARAM_ALM_SETTING, githubAlmSetting.getKey());
index ba8964dfe4b02f14c005e24ed1dbf77d6260e171..f0e2007c365370ef43b67a5ec6af09c0e8f4539e 100644 (file)
@@ -63,7 +63,7 @@ public class ListDefinitionsActionTest {
   public void list_github_settings() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user).setSystemAdministrator();
-    AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
+    AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting(s -> s.setClientId(""));
     AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_id").setClientSecret("client_secret"));
 
     ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
index 946d72f6a035da0efedd035428357ce10009c240..a5b6f47d11792d938d5e067e9759b416ad67d267 100644 (file)
@@ -138,7 +138,7 @@ public class ValidateActionTest {
 
   @Test
   public void github_validation_checks_missing_clientId() {
-    AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientSecret("clientSecret")));
+    AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(s -> s.setClientId(null)));
 
     assertThatThrownBy(() -> ws.newRequest()
       .setParam("key", almSetting.getKey())
@@ -147,7 +147,7 @@ public class ValidateActionTest {
 
   @Test
   public void github_validation_checks_missing_clientSecret() {
-    AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientId("clientId")));
+    AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(s -> s.setClientSecret(null)));
 
     assertThatThrownBy(() -> ws.newRequest()
       .setParam("key", almSetting.getKey())