]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19084 Sync Users from GitHub
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Fri, 21 Apr 2023 08:21:32 +0000 (10:21 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java
server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubModule.java
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 765372116e237de6b70f749a80d342e852a035b4..c08225a139da361eae12871a14af3cea4cbac22e 100644 (file)
@@ -30,6 +30,58 @@ public class GithubBinding {
     //nothing to do
   }
 
+  public static class GsonAppInstallation {
+    @SerializedName("id")
+    long id;
+    @SerializedName("account")
+    GsonAppOrgAccount account;
+
+    public GsonAppInstallation() {
+      // even if empty constructor is not required for Gson, it is strongly
+      // recommended:
+      // http://stackoverflow.com/a/18645370/229031
+    }
+
+    public GsonAppInstallation(long id, GsonAppOrgAccount account) {
+      this.id = id;
+      this.account = account;
+    }
+
+    public long getId() {
+      return id;
+    }
+
+    public GsonAppOrgAccount getAccount() {
+      return account;
+    }
+  }
+
+  public static class GsonAppOrgAccount {
+    @SerializedName("login")
+    String name;
+    @SerializedName("type")
+    String type;
+
+    public GsonAppOrgAccount() {
+      // even if empty constructor is not required for Gson, it is strongly
+      // recommended:
+      // http://stackoverflow.com/a/18645370/229031
+    }
+
+    public GsonAppOrgAccount(String name, String type) {
+      this.name = name;
+      this.type = type;
+    }
+
+    public String getName() {
+      return name;
+    }
+
+    public String getType() {
+      return type;
+    }
+  }
+
   public static class GsonInstallations {
     @SerializedName("total_count")
     int totalCount;
index 12385565b380e1290cdf02ca7fe8394a55a1c661..871ad324dc03da34f715aa12af5b9075c53cd3bd 100644 (file)
@@ -26,18 +26,18 @@ import org.sonar.core.platform.Module;
 import static org.sonar.auth.github.GitHubSettings.definitions;
 
 public class GitHubModule extends Module {
+  private static final List<Class<?>> COMPONENT_CLASSES = List.of(
+    GitHubIdentityProvider.class,
+    GitHubSettings.class,
+    GitHubRestClient.class,
+    UserIdentityFactoryImpl.class,
+    ScribeGitHubApi.class
+  );
 
   @Override
   protected void configureModule() {
-    add(
-      GitHubIdentityProvider.class,
-      GitHubSettings.class,
-      GitHubManagedInstanceService.class,
-      GitHubRestClient.class,
-      UserIdentityFactoryImpl.class,
-      ScribeGitHubApi.class);
+    add(COMPONENT_CLASSES);
     List<PropertyDefinition> definitions = definitions();
     add(definitions.toArray(new Object[definitions.size()]));
   }
-
 }
index 477ba26b84cfecb60ff06fe2c1687b757ff88f8d..d736e1e26ebfc94a5b8c673535084a8bd68e0208 100644 (file)
@@ -45,7 +45,9 @@ public class GitHubSettings {
   public static final String ALLOW_USERS_TO_SIGN_UP = "sonar.auth.github.allowUsersToSignUp";
   public static final String GROUPS_SYNC = "sonar.auth.github.groupsSync";
   public static final String API_URL = "sonar.auth.github.apiUrl";
+  public static final String DEFAULT_API_URL = "https://api.github.com/";
   public static final String WEB_URL = "sonar.auth.github.webUrl";
+  public static final String DEFAULT_WEB_URL = "https://github.com/";
   public static final String ORGANIZATIONS = "sonar.auth.github.organizations";
   @VisibleForTesting
   static final String PROVISIONING = "provisioning.github.enabled";
@@ -71,11 +73,11 @@ public class GitHubSettings {
     return configuration.get(CLIENT_SECRET).orElse("");
   }
 
-  String appId() {
+  public String appId() {
     return configuration.get(APP_ID).orElse("");
   }
 
-  String privateKey() {
+  public String privateKey() {
     return configuration.get(PRIVATE_KEY).orElse("");
   }
 
@@ -97,7 +99,7 @@ public class GitHubSettings {
   }
 
   @CheckForNull
-  String apiURL() {
+  public String apiURL() {
     return urlWithEndingSlash(configuration.get(API_URL).orElse(""));
   }
 
@@ -189,21 +191,20 @@ public class GitHubSettings {
         .build(),
       PropertyDefinition.builder(API_URL)
         .name("The API url for a GitHub instance.")
-        .description("The API url for a GitHub instance. https://api.github.com/ for Github.com, https://github.company.com/api/v3/ when using Github Enterprise")
+        .description(String.format("The API url for a GitHub instance. %s for Github.com, https://github.company.com/api/v3/ when using Github Enterprise", DEFAULT_API_URL))
         .category(CATEGORY)
         .subCategory(SUBCATEGORY)
         .type(STRING)
-        .defaultValue("https://api.github.com/")
+        .defaultValue(DEFAULT_API_URL)
         .index(index++)
         .build(),
       PropertyDefinition.builder(WEB_URL)
         .name("The WEB url for a GitHub instance.")
-        .description("The WEB url for a GitHub instance. " +
-          "https://github.com/ for Github.com, https://github.company.com/ when using GitHub Enterprise.")
+        .description(String.format("The WEB url for a GitHub instance. %s for Github.com, https://github.company.com/ when using GitHub Enterprise.", DEFAULT_WEB_URL))
         .category(CATEGORY)
         .subCategory(SUBCATEGORY)
         .type(STRING)
-        .defaultValue("https://github.com/")
+        .defaultValue(DEFAULT_WEB_URL)
         .index(index++)
         .build(),
       PropertyDefinition.builder(ORGANIZATIONS)
index 6cde4167c81e32c6b692169e08fad2830018ae42..635dbf1df54178edd0fffcdaab7fba574a37f174 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(16);
+    assertThat(container.getAddedObjects()).hasSize(15);
   }
 
 }
index 3f28bad430525ba290016a583e8a94885b46ba36..6d2ceaabc12d6ce24da3ef7c48b4866c024fdbcc 100644 (file)
@@ -90,7 +90,7 @@ public class GitHubSettingsTest {
 
   @Test
   public void isProvisioningEnabled_ifProvisioningEnabledAndGithubAuthEnabled_returnsTrue() {
-    enableGithubAuthentication();
+    enableGithubAuthenticationWithGithubApp();
     when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.TRUE.toString()));
     assertThat(underTest.isProvisioningEnabled()).isTrue();
   }
@@ -229,4 +229,10 @@ public class GitHubSettingsTest {
     settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
     settings.setProperty("sonar.auth.github.enabled", true);
   }
+
+  private void enableGithubAuthenticationWithGithubApp() {
+    enableGithubAuthentication();
+    settings.setProperty("sonar.auth.github.appId", "id");
+    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
+  }
 }