private void setAccountIdentity()

in github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java [127:179]


  private void setAccountIdentity(IdentifiedUser user, HttpServletRequest req)
      throws ServletException, ConfigInvalidException {
    String fullName = req.getParameter("fullname");
    String email = req.getParameter("email");
    String username = req.getParameter("username");
    try {
      Id accountId = user.getAccountId();
      AuthResult result = accountManager.link(accountId, authRequestFactory.createForEmail(email));
      log.debug("Account {} linked to email {}: result = {}", accountId, email, result);

      putPreferred.apply(new AccountResource.Email(user, email), null);
      NameInput nameInput = new NameInput();
      nameInput.name = fullName;
      putName.apply(user, nameInput);

      ExternalId.Key key =
          ExternalId.Key.create(SCHEME_USERNAME, username, authConfig.isUserNameCaseInsensitive());
      Optional<ExternalId> other;
      try {
        other = externalIds.get(key);
      } catch (IOException e) {
        throw new IllegalArgumentException(
            "Internal error while fetching username='" + username + "'");
      }

      if (other.map(externalId -> externalId.accountId().equals(accountId)).orElse(false)) {
        // Current account has already an external id with SCHEME_USERNAME set to the username
        return;
      }

      try {
        accountsUpdateProvider
            .get()
            .update(
                "Set Username from GitHub",
                accountId,
                u -> u.addExternalId(externalIdFactory.create(key, accountId, null, null)));
      } catch (Exception e) {
        throw new IllegalArgumentException(
            "Internal error while trying to set username='" + username + "'");
      }

      log.debug(
          "Account {} updated with preferredEmail = {}, fullName = {}, username = {}",
          accountId,
          email,
          fullName,
          username);
    } catch (Exception e) {
      throw new ServletException(
          "Cannot associate email '" + email + "' to current user '" + user + "'", e);
    }
  }