Honour `userNameCaseInsensitive` config

Fix an issue where the creation of new external ids was always hardcoded
to be done case insensitively.

Gerrit provides an `auth.userNameCaseInsensitive` configuration [1] that
specifies whether usernames need to be handled case insensitively.

Honour the `auth.userNameCaseInsensitive` when creating or getting
external-ids.

[1] https://gerrit.googlesource.com/gerrit/+/refs/heads/stable-3.5/Documentation/config-gerrit.txt#655

Bug: Issue 15447
Change-Id: I6ca54962a7bac70f6c32467d633e9da2366e989b
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
index 9395056..f706539 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -26,6 +26,7 @@
 import com.google.gerrit.server.account.AccountImporter;
 import com.google.gerrit.server.account.externalids.ExternalId;
 import com.google.gerrit.server.account.externalids.ExternalIds;
+import com.google.gerrit.server.config.AuthConfig;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.project.ProjectCache;
 import com.google.gerrit.server.project.ProjectState;
@@ -79,6 +80,7 @@
   private final String repoName;
   private final int prId;
   private final GitRepositoryManager repoMgr;
+  private final AuthConfig authConfig;
   private final int jobIndex;
   private final ExternalIds externalIds;
   private PullRequestCreateChange createChange;
@@ -96,10 +98,12 @@
       GitHubRepository.Factory gitHubRepoFactory,
       ScopedProvider<GitHubLogin> ghLoginProvider,
       ExternalIds externalIds,
+      AuthConfig authConfig,
       @Assisted("index") int jobIndex,
       @Assisted("organisation") String organisation,
       @Assisted("name") String repoName,
       @Assisted int pullRequestId) {
+    this.authConfig = authConfig;
     this.jobIndex = jobIndex;
     this.repoMgr = repoMgr;
     this.ghLogin = ghLoginProvider.get();
@@ -217,7 +221,8 @@
 
   private Optional<ExternalId> externalIdByScheme(String scheme, String id) {
     try {
-      return externalIds.get(ExternalId.Key.create(scheme, id, true));
+      return externalIds.get(
+          ExternalId.Key.create(scheme, id, authConfig.isUserNameCaseInsensitive()));
     } catch (IOException e) {
       LOG.error("Unable to get external id for " + scheme + ":" + id, e);
       return Optional.empty();
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
index 59604ef..23edcbc 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
@@ -34,6 +34,7 @@
 import com.google.gerrit.server.account.externalids.ExternalId;
 import com.google.gerrit.server.account.externalids.ExternalIdFactory;
 import com.google.gerrit.server.account.externalids.ExternalIds;
+import com.google.gerrit.server.config.AuthConfig;
 import com.google.gerrit.server.restapi.account.AddSshKey;
 import com.google.gerrit.server.restapi.account.GetSshKeys;
 import com.google.gerrit.server.restapi.account.PutName;
@@ -71,6 +72,7 @@
   private final ExternalIds externalIds;
   private final ExternalIdFactory externalIdFactory;
   private final AuthRequest.Factory authRequestFactory;
+  private final AuthConfig authConfig;
 
   @Inject
   public AccountController(
@@ -83,7 +85,8 @@
       @ServerInitiated final Provider<AccountsUpdate> accountsUpdateProvider,
       final ExternalIds externalIds,
       final ExternalIdFactory externalIdFactory,
-      final AuthRequest.Factory authRequestFactory) {
+      final AuthRequest.Factory authRequestFactory,
+      final AuthConfig authConfig) {
     this.restAddSshKey = restAddSshKey;
     this.restGetSshKeys = restGetSshKeys;
     this.accountManager = accountManager;
@@ -94,6 +97,7 @@
     this.externalIds = externalIds;
     this.externalIdFactory = externalIdFactory;
     this.authRequestFactory = authRequestFactory;
+    this.authConfig = authConfig;
   }
 
   @Override
@@ -135,7 +139,8 @@
       nameInput.name = fullName;
       putName.apply(user, nameInput);
 
-      ExternalId.Key key = ExternalId.Key.create(SCHEME_USERNAME, username, true);
+      ExternalId.Key key =
+          ExternalId.Key.create(SCHEME_USERNAME, username, authConfig.isUserNameCaseInsensitive());
       Optional<ExternalId> other;
       try {
         other = externalIds.get(key);