From cf02910d825009883b2c741b80c4a7175c74bb74 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 31 Aug 2017 16:19:51 +0200 Subject: [PATCH] SONAR-9616 Allow usage of main branch in ws having branch parameter --- .../org/sonar/db/component/ComponentDao.java | 10 +++-- .../sonar/db/component/ComponentMapper.java | 7 +--- .../sonar/db/component/ComponentMapper.xml | 22 ++++++---- .../sonar/db/component/ComponentDaoTest.java | 8 ++-- .../org/sonar/server/issue/IssueQuery.java | 12 ++++++ .../sonar/server/issue/IssueQueryFactory.java | 24 +++++++---- .../sonar/server/issue/index/IssueIndex.java | 2 +- .../server/component/ComponentFinderTest.java | 9 +++- .../server/issue/IssueQueryFactoryTest.java | 41 +++++++++++++------ .../server/issue/index/IssueIndexTest.java | 40 ++++++++++++------ .../issue/ws/SearchActionComponentsTest.java | 25 +++++++++++ 11 files changed, 146 insertions(+), 54 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 5806296250b..d911187f8a0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -29,13 +29,13 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.stream.Stream; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; -import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; @@ -44,6 +44,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static java.util.Collections.emptyList; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang.StringUtils.isBlank; +import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.DaoDatabaseUtils.buildLikeValue; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeUpdates; @@ -172,8 +173,9 @@ public class ComponentDao implements Dao { } public List selectByKeysAndBranch(DbSession session, Collection keys, String branch) { - List dbKeys = keys.stream().map(k -> generateBranchKey(k, branch)).collect(MoreCollectors.toList()); - return executeLargeInputs(dbKeys, mapper(session)::selectByDbKeys); + List dbKeys = keys.stream().map(k -> generateBranchKey(k, branch)).collect(toList()); + List allKeys = Stream.of(keys, dbKeys) .flatMap(x -> x.stream()) .collect(toList()); + return executeLargeInputs(allKeys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, branch)); } public List selectComponentsHavingSameKeyOrderedById(DbSession session, String key) { @@ -222,7 +224,7 @@ public class ComponentDao implements Dao { } public java.util.Optional selectByKeyAndBranch(DbSession session, String key, String branch) { - return java.util.Optional.ofNullable(mapper(session).selectByDbKey(generateBranchKey(key, branch))); + return java.util.Optional.ofNullable(mapper(session).selectByKeyAndBranch(key, generateBranchKey(key, branch), branch)); } public List selectAllViewsAndSubViews(DbSession session) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java index 0f3f5d74c33..ca7da22e645 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -33,11 +33,8 @@ public interface ComponentMapper { @CheckForNull ComponentDto selectByKey(String key); - /** - * This method should be used to get a component by its key without filtering out branches - */ @CheckForNull - ComponentDto selectByDbKey(String dbKey); + ComponentDto selectByKeyAndBranch(@Param("key") String key, @Param("dbKey") String dbKey, @Param("branch") String branch); @CheckForNull ComponentDto selectById(long id); @@ -52,7 +49,7 @@ public interface ComponentMapper { List selectByKeys(@Param("keys") Collection keys); - List selectByDbKeys(@Param("keys") Collection keys); + List selectByKeysAndBranch(@Param("keys") Collection keys, @Param("branch") String branch); List selectByIds(@Param("ids") Collection ids); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 6031646c260..dec681a80fd 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -37,12 +37,15 @@ p.kee=#{key,jdbcType=VARCHAR} - SELECT FROM projects p - where - p.kee=#{key,jdbcType=VARCHAR} + INNER JOIN project_branches pb on pb.uuid = p.project_uuid + + (p.kee=#{dbKey,jdbcType=VARCHAR} OR p.kee=#{key,jdbcType=VARCHAR}) + AND pb.kee=#{branch,jdbcType=VARCHAR} + - + SELECT - from projects p - where + FROM projects p + INNER JOIN project_branches pb on pb.uuid = p.project_uuid + p.enabled=${_true} - and p.kee in + AND p.kee IN #{key,jdbcType=VARCHAR} + AND pb.kee=#{branch,jdbcType=VARCHAR} +