]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19084 Add AppId and PrivateKey field to the GitHub config
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Fri, 21 Apr 2023 10:02:20 +0000 (12:02 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java
server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubModuleTest.java
server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java

index 81db6cf05bf80484daa8ba35f4f6ef4781b44f9d..b7a1eec45d8ea8ffd8015b9e999e11bacb96af42 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Arrays;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+import org.sonar.api.PropertyType;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.config.PropertyDefinition;
 import org.sonar.server.property.InternalProperties;
@@ -37,6 +38,8 @@ public class GitHubSettings {
 
   public static final String CLIENT_ID = "sonar.auth.github.clientId.secured";
   public static final String CLIENT_SECRET = "sonar.auth.github.clientSecret.secured";
+  public static final String APP_ID = "sonar.auth.github.appId";
+  public static final String PRIVATE_KEY = "sonar.auth.github.privateKey.secured";
   public static final String ENABLED = "sonar.auth.github.enabled";
   public static final String ALLOW_USERS_TO_SIGN_UP = "sonar.auth.github.allowUsersToSignUp";
   public static final String GROUPS_SYNC = "sonar.auth.github.groupsSync";
@@ -67,6 +70,14 @@ public class GitHubSettings {
     return configuration.get(CLIENT_SECRET).orElse("");
   }
 
+  String appId() {
+    return configuration.get(APP_ID).orElse("");
+  }
+
+  String privateKey() {
+    return configuration.get(PRIVATE_KEY).orElse("");
+  }
+
   boolean isEnabled() {
     return configuration.getBoolean(ENABLED).orElse(false) && !clientId().isEmpty() && !clientSecret().isEmpty();
   }
@@ -110,6 +121,7 @@ public class GitHubSettings {
   }
 
   public static List<PropertyDefinition> definitions() {
+    int index = 1;
     return Arrays.asList(
       PropertyDefinition.builder(ENABLED)
         .name("Enabled")
@@ -118,14 +130,14 @@ public class GitHubSettings {
         .subCategory(SUBCATEGORY)
         .type(BOOLEAN)
         .defaultValue(valueOf(false))
-        .index(1)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(CLIENT_ID)
         .name("Client ID")
         .description("Client ID provided by GitHub when registering the application.")
         .category(CATEGORY)
         .subCategory(SUBCATEGORY)
-        .index(2)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(CLIENT_SECRET)
         .name("Client Secret")
@@ -133,7 +145,25 @@ public class GitHubSettings {
         .category(CATEGORY)
         .subCategory(SUBCATEGORY)
         .type(PASSWORD)
-        .index(3)
+        .index(index++)
+        .build(),
+      PropertyDefinition.builder(APP_ID)
+        .name("GitHub App ID")
+        .description("The App ID is found on your GitHub App's page on GitHub at Settings > Developer Settings > GitHub Apps.")
+        .category(CATEGORY)
+        .subCategory(SUBCATEGORY)
+        .type(STRING)
+        .index(index++)
+        .build(),
+      PropertyDefinition.builder(PRIVATE_KEY)
+        .name("Private Key")
+        .description("""
+          Your GitHub App's private key. You can generate a .pem file from your GitHub App's page under Private keys.
+          Copy and paste the whole contents of the file here.""")
+        .category(CATEGORY)
+        .subCategory(SUBCATEGORY)
+        .type(PropertyType.TEXT)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(ALLOW_USERS_TO_SIGN_UP)
         .name("Allow users to sign-up")
@@ -142,7 +172,7 @@ public class GitHubSettings {
         .subCategory(SUBCATEGORY)
         .type(BOOLEAN)
         .defaultValue(valueOf(true))
-        .index(4)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(GROUPS_SYNC)
         .name("Synchronize teams as groups")
@@ -151,7 +181,7 @@ public class GitHubSettings {
         .subCategory(SUBCATEGORY)
         .type(BOOLEAN)
         .defaultValue(valueOf(false))
-        .index(6)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(API_URL)
         .name("The API url for a GitHub instance.")
@@ -160,7 +190,7 @@ public class GitHubSettings {
         .subCategory(SUBCATEGORY)
         .type(STRING)
         .defaultValue("https://api.github.com/")
-        .index(7)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(WEB_URL)
         .name("The WEB url for a GitHub instance.")
@@ -170,7 +200,7 @@ public class GitHubSettings {
         .subCategory(SUBCATEGORY)
         .type(STRING)
         .defaultValue("https://github.com/")
-        .index(8)
+        .index(index++)
         .build(),
       PropertyDefinition.builder(ORGANIZATIONS)
         .name("Organizations")
@@ -179,7 +209,7 @@ public class GitHubSettings {
         .multiValues(true)
         .category(CATEGORY)
         .subCategory(SUBCATEGORY)
-        .index(9)
+        .index(index)
         .build());
   }
 }
index ebb9b1b1f2beb8b66ddf76ffcc1549467c4368c8..6cde4167c81e32c6b692169e08fad2830018ae42 100644 (file)
@@ -30,7 +30,7 @@ public class GitHubModuleTest {
   public void verify_count_of_added_components() {
     ListContainer container = new ListContainer();
     new GitHubModule().configure(container);
-    assertThat(container.getAddedObjects()).hasSize(14);
+    assertThat(container.getAddedObjects()).hasSize(16);
   }
 
 }
index 12dc3c63b1f7f57f5bbfd6629e6292090ec62542..870a2a90973fc81cc5ab0ab1826502e9a036b2e2 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.auth.github;
 
 import java.util.Optional;
 import org.junit.Test;
+import org.sonar.api.config.PropertyDefinition;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.utils.System2;
@@ -117,6 +118,18 @@ public class GitHubSettingsTest {
     assertThat(underTest.clientSecret()).isEqualTo("secret");
   }
 
+  @Test
+  public void return_app_id() {
+    settings.setProperty("sonar.auth.github.appId", "secret");
+    assertThat(underTest.appId()).isEqualTo("secret");
+  }
+
+  @Test
+  public void return_private_key() {
+    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
+    assertThat(underTest.privateKey()).isEqualTo("secret");
+  }
+
   @Test
   public void allow_users_to_sign_up() {
     settings.setProperty("sonar.auth.github.allowUsersToSignUp", "true");
@@ -187,7 +200,19 @@ public class GitHubSettingsTest {
 
   @Test
   public void definitions() {
-    assertThat(GitHubSettings.definitions()).hasSize(8);
+    assertThat(GitHubSettings.definitions().stream()
+      .map(PropertyDefinition::name))
+      .containsExactly(
+        "Enabled",
+        "Client ID",
+        "Client Secret",
+        "GitHub App ID",
+        "Private Key",
+        "Allow users to sign-up",
+        "Synchronize teams as groups",
+        "The API url for a GitHub instance.",
+        "The WEB url for a GitHub instance.",
+        "Organizations");
   }
 
   private void enableGithubAuthentication() {