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;
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";
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();
}
}
public static List<PropertyDefinition> definitions() {
+ int index = 1;
return Arrays.asList(
PropertyDefinition.builder(ENABLED)
.name("Enabled")
.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")
.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")
.subCategory(SUBCATEGORY)
.type(BOOLEAN)
.defaultValue(valueOf(true))
- .index(4)
+ .index(index++)
.build(),
PropertyDefinition.builder(GROUPS_SYNC)
.name("Synchronize teams as groups")
.subCategory(SUBCATEGORY)
.type(BOOLEAN)
.defaultValue(valueOf(false))
- .index(6)
+ .index(index++)
.build(),
PropertyDefinition.builder(API_URL)
.name("The API url for a GitHub instance.")
.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.")
.subCategory(SUBCATEGORY)
.type(STRING)
.defaultValue("https://github.com/")
- .index(8)
+ .index(index++)
.build(),
PropertyDefinition.builder(ORGANIZATIONS)
.name("Organizations")
.multiValues(true)
.category(CATEGORY)
.subCategory(SUBCATEGORY)
- .index(9)
+ .index(index)
.build());
}
}
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;
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");
@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() {