Merge branch 'stable-2.15' into stable-2.16

* stable-2.15:
  Init: Default to yes only for currently configured providers

Change-Id: Ie6c0d2b4db52ccf1c80ce1a5867987fd0bee66d8
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
index 9c71792..540b65d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/InitOAuth.java
@@ -79,25 +79,33 @@
     ui.header("OAuth Authentication Provider");
 
     boolean configureGoogleOAuthProvider =
-        ui.yesno(true, "Use Google OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(googleOAuthProviderSection),
+            "Use Google OAuth provider for Gerrit login ?");
     if (configureGoogleOAuthProvider && configureOAuth(googleOAuthProviderSection)) {
       googleOAuthProviderSection.string(FIX_LEGACY_USER_ID_QUESTION, FIX_LEGACY_USER_ID, "false");
     }
 
     boolean configueGitHubOAuthProvider =
-        ui.yesno(true, "Use GitHub OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(githubOAuthProviderSection),
+            "Use GitHub OAuth provider for Gerrit login ?");
     if (configueGitHubOAuthProvider && configureOAuth(githubOAuthProviderSection)) {
       githubOAuthProviderSection.string(FIX_LEGACY_USER_ID_QUESTION, FIX_LEGACY_USER_ID, "false");
     }
 
     boolean configureBitbucketOAuthProvider =
-        ui.yesno(true, "Use Bitbucket OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(bitbucketOAuthProviderSection),
+            "Use Bitbucket OAuth provider for Gerrit login ?");
     if (configureBitbucketOAuthProvider && configureOAuth(bitbucketOAuthProviderSection)) {
       bitbucketOAuthProviderSection.string(
           FIX_LEGACY_USER_ID_QUESTION, FIX_LEGACY_USER_ID, "false");
     }
 
-    boolean configureCasOAuthProvider = ui.yesno(true, "Use CAS OAuth provider for Gerrit login ?");
+    boolean configureCasOAuthProvider =
+        ui.yesno(
+            isConfigured(casOAuthProviderSection), "Use CAS OAuth provider for Gerrit login ?");
     if (configureCasOAuthProvider && configureOAuth(casOAuthProviderSection)) {
       String rootUrl = casOAuthProviderSection.string("CAS Root URL", ROOT_URL, null);
       requireNonNull(rootUrl);
@@ -108,13 +116,17 @@
     }
 
     boolean configueFacebookOAuthProvider =
-        ui.yesno(true, "Use Facebook OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(facebookOAuthProviderSection),
+            "Use Facebook OAuth provider for Gerrit login ?");
     if (configueFacebookOAuthProvider) {
       configureOAuth(facebookOAuthProviderSection);
     }
 
     boolean configureGitLabOAuthProvider =
-        ui.yesno(true, "Use GitLab OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(gitlabOAuthProviderSection),
+            "Use GitLab OAuth provider for Gerrit login ?");
     if (configureGitLabOAuthProvider && configureOAuth(gitlabOAuthProviderSection)) {
       String rootUrl = gitlabOAuthProviderSection.string("GitLab Root URL", ROOT_URL, null);
       requireNonNull(rootUrl);
@@ -123,7 +135,9 @@
       }
     }
 
-    boolean configureDexOAuthProvider = ui.yesno(true, "Use Dex OAuth provider for Gerrit login ?");
+    boolean configureDexOAuthProvider =
+        ui.yesno(
+            isConfigured(dexOAuthProviderSection), "Use Dex OAuth provider for Gerrit login ?");
     if (configureDexOAuthProvider && configureOAuth(dexOAuthProviderSection)) {
       String rootUrl = dexOAuthProviderSection.string("Dex Root URL", ROOT_URL, null);
       requireNonNull(rootUrl);
@@ -133,7 +147,9 @@
     }
 
     boolean configureKeycloakOAuthProvider =
-        ui.yesno(true, "Use Keycloak OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(keycloakOAuthProviderSection),
+            "Use Keycloak OAuth provider for Gerrit login ?");
     if (configureKeycloakOAuthProvider && configureOAuth(keycloakOAuthProviderSection)) {
       String rootUrl = keycloakOAuthProviderSection.string("Keycloak Root URL", ROOT_URL, null);
       requireNonNull(rootUrl);
@@ -144,25 +160,39 @@
     }
 
     boolean configureOffice365OAuthProvider =
-        ui.yesno(true, "Use Office365 OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(office365OAuthProviderSection),
+            "Use Office365 OAuth provider for Gerrit login ?");
     if (configureOffice365OAuthProvider) {
       configureOAuth(office365OAuthProviderSection);
     }
 
     boolean configureAirVantageOAuthProvider =
-        ui.yesno(true, "Use AirVantage OAuth provider for Gerrit login ?");
+        ui.yesno(
+            isConfigured(airVantageOAuthProviderSection),
+            "Use AirVantage OAuth provider for Gerrit login ?");
     if (configureAirVantageOAuthProvider) {
       configureOAuth(airVantageOAuthProviderSection);
     }
   }
 
   /**
+   * Retrieve client id to check whether or not this provider was already configured.
+   *
+   * @param s OAuth provider section
+   * @return true if client id key is present, false otherwise
+   */
+  private static boolean isConfigured(Section s) {
+    return !Strings.isNullOrEmpty(s.get(CLIENT_ID));
+  }
+
+  /**
    * Configure OAuth provider section
    *
    * @param s section to configure
    * @return true if section is present, false otherwise
    */
-  private boolean configureOAuth(Section s) {
+  private static boolean configureOAuth(Section s) {
     if (!Strings.isNullOrEmpty(s.string("Application client id", CLIENT_ID, null))) {
       s.passwordForKey("Application client secret", CLIENT_SECRET);
       return true;