]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15528 Support export project branches in portfolio in XML
authorJacek <jacek.poreda@sonarsource.com>
Tue, 2 Nov 2021 13:12:43 +0000 (14:12 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 9 Nov 2021 20:03:16 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java

index cfbc72cb9106b2c0724b6489b3c16e202767b9d4..e48ac2a5bf354c37c463ac160d4a5c2c18fc7f82 100644 (file)
@@ -24,6 +24,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
@@ -81,6 +82,13 @@ public class BranchDao implements Dao {
     return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, key, branchType));
   }
 
+  public List<BranchDto> selectByKeys(DbSession dbSession, String projectUuid, Set<String> branchKeys) {
+    if (branchKeys.isEmpty()) {
+      return emptyList();
+    }
+    return executeLargeInputs(branchKeys, partition -> mapper(dbSession).selectByKeys(projectUuid, branchKeys));
+  }
+
   public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) {
     String projectUuid = component.getMainBranchProjectUuid();
     if (projectUuid == null) {
index 62837316822a92285390302f1da8d3af0f59c867..b0b5ff2a23ad20e75808a59bee59e8de30dbfbaa 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.db.component;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 
@@ -40,6 +41,8 @@ public interface BranchMapper {
 
   BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("branchType") BranchType branchType);
 
+  List<BranchDto> selectByKeys(@Param("projectUuid") String projectUuid, @Param("keys") Set<String> branchKeys);
+
   BranchDto selectByUuid(@Param("uuid") String uuid);
 
   Collection<BranchDto> selectByProjectUuid(@Param("projectUuid") String projectUuid);
index 8cdc4b70ddc518b5c0f597c8bc1717b9e9552068..3cecf8644f916eebc32884e0a8c79df266c537e2 100644 (file)
       pb.branch_type = #{branchType, jdbcType=VARCHAR}
   </select>
 
+  <select id="selectByKeys" resultType="org.sonar.db.component.BranchDto">
+    select <include refid="columns"/>
+    from project_branches pb
+    where
+    pb.project_uuid = #{projectUuid, jdbcType=VARCHAR} and
+    pb.kee in
+    <foreach collection="keys" open="(" close=")" item="key" separator=",">
+      #{key,jdbcType=VARCHAR}
+    </foreach>
+  </select>
+
   <select id="selectByBranchKeys" resultType="org.sonar.db.component.BranchDto">
     select
     <include refid="columns"/>
index 2ddadd015ed352135192af983dfedd1f568189cf..fa598e927975becf79c34ccbe4e9775cf0d9967d 100644 (file)
@@ -23,10 +23,12 @@ import com.google.common.collect.Sets;
 import com.tngtech.java.junit.dataprovider.DataProvider;
 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import org.junit.Rule;
@@ -462,6 +464,40 @@ public class BranchDaoTest {
     assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty();
   }
 
+  @Test
+  public void selectByKeys() {
+    BranchDto mainBranch = new BranchDto()
+      .setProjectUuid("U1")
+      .setUuid("U1")
+      .setBranchType(BranchType.BRANCH)
+      .setKey("master");
+    underTest.insert(dbSession, mainBranch);
+
+    BranchDto featureBranch = new BranchDto()
+      .setProjectUuid("U1")
+      .setUuid("U2")
+      .setBranchType(BranchType.BRANCH)
+      .setKey("feature1");
+    underTest.insert(dbSession, featureBranch);
+
+    String pullRequestId = "123";
+    BranchDto pullRequest = new BranchDto()
+      .setProjectUuid("U1")
+      .setUuid("U3")
+      .setBranchType(BranchType.PULL_REQUEST)
+      .setKey(pullRequestId)
+      .setMergeBranchUuid("U4");
+    underTest.insert(dbSession, pullRequest);
+
+    assertThat(underTest.selectByKeys(dbSession, "U1", Collections.emptySet()))
+      .isEmpty();
+
+    List<BranchDto> loaded = underTest.selectByKeys(dbSession, "U1", Set.of(mainBranch.getKey(), featureBranch.getKey()));
+    assertThat(loaded)
+      .extracting(BranchDto::getUuid)
+      .containsExactlyInAnyOrder(mainBranch.getUuid(), featureBranch.getUuid());
+  }
+
   @Test
   public void selectByUuids() {
     ComponentDto project = db.components().insertPrivateProject();