From: Teryk Bellahsene Date: Tue, 17 Oct 2017 13:08:05 +0000 (+0200) Subject: SONAR-9903 Ignore SCM accounts that are duplicates when adding a user X-Git-Tag: 6.7-RC1~175 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2f214e97c7fc833ba08615e62aa6d7f8bf5911e0;p=sonarqube.git SONAR-9903 Ignore SCM accounts that are duplicates when adding a user --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java index fc3dd43df6f..1213e5bc0b7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java @@ -26,7 +26,6 @@ import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.lang.StringUtils; import org.sonar.core.user.DefaultUser; import static java.util.Objects.requireNonNull; @@ -117,7 +116,7 @@ public class UserDto { return this; } - public UserDto setScmAccounts(@Nullable List list) { + public UserDto setScmAccounts(@Nullable List list) { this.scmAccounts = encodeScmAccounts(list); return this; } @@ -125,7 +124,7 @@ public class UserDto { @CheckForNull public static String encodeScmAccounts(@Nullable List scmAccounts) { if (scmAccounts != null && !scmAccounts.isEmpty()) { - return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, StringUtils.join(scmAccounts, SCM_ACCOUNTS_SEPARATOR), SCM_ACCOUNTS_SEPARATOR); + return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, String.join(String.valueOf(SCM_ACCOUNTS_SEPARATOR), scmAccounts), SCM_ACCOUNTS_SEPARATOR); } return null; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java index cc14249f719..b87273aee3b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java @@ -24,6 +24,7 @@ import com.google.common.base.Strings; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Random; @@ -33,7 +34,6 @@ import org.apache.commons.codec.digest.DigestUtils; import org.sonar.api.config.Configuration; import org.sonar.api.platform.NewUserHandler; import org.sonar.api.server.ServerSide; -import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationMemberDto; @@ -52,6 +52,7 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.collect.Lists.newArrayList; import static java.lang.String.format; import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS; +import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.user.UserDto.encryptPassword; import static org.sonar.server.ws.WsUtils.checkFound; import static org.sonar.server.ws.WsUtils.checkRequest; @@ -351,7 +352,11 @@ public class UserUpdater { private static List sanitizeScmAccounts(@Nullable List scmAccounts) { if (scmAccounts != null) { - return scmAccounts.stream().filter(s -> !Strings.isNullOrEmpty(s)).collect(MoreCollectors.toList()); + return new HashSet<>(scmAccounts).stream() + .map(Strings::emptyToNull) + .filter(Objects::nonNull) + .sorted(String::compareToIgnoreCase) + .collect(toList(scmAccounts.size())); } return Collections.emptyList(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java index 03d42b05245..c8926316b7c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.user.ws; +import java.util.Arrays; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -169,6 +170,32 @@ public class UpdateActionTest { assertThat(user.getScmAccountsAsList()).containsOnly("jon,snow"); } + @Test + public void update_scm_account_ignores_duplicates() throws Exception { + createUser(); + + ws.newRequest() + .setParam("login", "john") + .setMultiParam("scmAccount", Arrays.asList("jon.snow", "jon.snow", "jon.jon", "jon.snow")) + .execute(); + + UserDto user = dbClient.userDao().selectByLogin(dbSession, "john"); + assertThat(user.getScmAccountsAsList()).containsExactlyInAnyOrder("jon.jon", "jon.snow"); + } + + @Test + public void update_scm_account_ordered_case_insensitive() throws Exception { + createUser(); + + ws.newRequest() + .setParam("login", "john") + .setMultiParam("scmAccount", Arrays.asList("jon.3", "Jon.1", "JON.2")) + .execute(); + + UserDto user = dbClient.userDao().selectByLogin(dbSession, "john"); + assertThat(user.getScmAccountsAsList()).containsExactly("Jon.1", "JON.2", "jon.3"); + } + @Test public void update_only_scm_accounts_with_deprecated_scmAccounts_parameter() throws Exception { createUser();