]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19084 Add API endpoints github_provisioning/{enable/disable/status}
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Wed, 26 Apr 2023 08:09:06 +0000 (10:09 +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/GitHubSettingsTest.java

index b7a1eec45d8ea8ffd8015b9e999e11bacb96af42..477ba26b84cfecb60ff06fe2c1687b757ff88f8d 100644 (file)
@@ -33,6 +33,7 @@ import static java.lang.String.valueOf;
 import static org.sonar.api.PropertyType.BOOLEAN;
 import static org.sonar.api.PropertyType.PASSWORD;
 import static org.sonar.api.PropertyType.STRING;
+import static org.sonar.api.utils.Preconditions.checkState;
 
 public class GitHubSettings {
 
@@ -47,7 +48,7 @@ public class GitHubSettings {
   public static final String WEB_URL = "sonar.auth.github.webUrl";
   public static final String ORGANIZATIONS = "sonar.auth.github.organizations";
   @VisibleForTesting
-  static final String PROVISIONING = "provisioning.github"; //TODO
+  static final String PROVISIONING = "provisioning.github.enabled";
 
   private static final String CATEGORY = "authentication";
   private static final String SUBCATEGORY = "github";
@@ -78,7 +79,7 @@ public class GitHubSettings {
     return configuration.get(PRIVATE_KEY).orElse("");
   }
 
-  boolean isEnabled() {
+  public boolean isEnabled() {
     return configuration.getBoolean(ENABLED).orElse(false) && !clientId().isEmpty() && !clientSecret().isEmpty();
   }
 
@@ -113,6 +114,9 @@ public class GitHubSettings {
   }
 
   public void setProvisioning(boolean enableProvisioning) {
+    if (enableProvisioning) {
+      checkState(isEnabled(), "GitHub authentication must be enabled to enable GitHub provisioning.");
+    }
     internalProperties.write(PROVISIONING, String.valueOf(enableProvisioning));
   }
 
index 870a2a90973fc81cc5ab0ab1826502e9a036b2e2..3f28bad430525ba290016a583e8a94885b46ba36 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.server.property.InternalProperties;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -94,8 +95,16 @@ public class GitHubSettingsTest {
     assertThat(underTest.isProvisioningEnabled()).isTrue();
   }
 
+  @Test
+  public void setProvisioning_whenGitHubAuthDisabled_shouldThrow() {
+    assertThatIllegalStateException()
+      .isThrownBy(() -> underTest.setProvisioning(true))
+      .withMessage("GitHub authentication must be enabled to enable GitHub provisioning.");
+  }
+
   @Test
   public void setProvisioning_whenPassedTrue_delegatesToInternalPropertiesWrite() {
+    enableGithubAuthentication();
     underTest.setProvisioning(true);
     verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.TRUE.toString());
   }