]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Fix generation of subview key
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 19 Aug 2021 16:09:29 +0000 (11:09 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 8 Sep 2021 20:03:34 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/AbstractMockUserSession.java
server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java

index aaa87d1624dff9daa0997397f507742a79007c80..d9c49ae21784baadbc9ab6df1d2dec1d15a43cdc 100644 (file)
@@ -51,14 +51,16 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectAllRoots();
   }
 
+  public List<PortfolioDto> selectAll(DbSession dbSession) {
+    return mapper(dbSession).selectAll();
+  }
+
   public Optional<PortfolioDto> selectByKey(DbSession dbSession, String key) {
     return Optional.ofNullable(mapper(dbSession).selectByKey(key));
-
   }
 
   public Optional<PortfolioDto> selectByUuid(DbSession dbSession, String uuid) {
     return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
-
   }
 
   public void insert(DbSession dbSession, PortfolioDto portfolio) {
@@ -85,6 +87,10 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectAllReferencesToPortfolios();
   }
 
+  public List<ReferenceDto> selectAllReferencesToApplications(DbSession dbSession) {
+    return mapper(dbSession).selectAllReferencesToApplications();
+  }
+
   public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
     return mapper(dbSession).selectTree(portfolioUuid);
   }
@@ -105,6 +111,10 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectReferencersByKey(referenceKey);
   }
 
+  public List<PortfolioDto> selectRootOfReferencers(DbSession dbSession, String referenceUuid) {
+    return mapper(dbSession).selectRootOfReferencers(referenceUuid);
+  }
+
   public List<ProjectDto> getProjects(DbSession dbSession, String portfolioUuid) {
     return mapper(dbSession).selectProjects(portfolioUuid);
   }
@@ -138,6 +148,14 @@ public class PortfolioDao implements Dao {
     mapper(dbSession).deleteProjects(portfolioUuid);
   }
 
+  public void deleteProject(DbSession dbSession, String portfolioUuid, String projectUuid) {
+    mapper(dbSession).deleteProject(portfolioUuid, projectUuid);
+  }
+
+  public void deleteReferencesTo(DbSession dbSession, String referenceUuid) {
+    mapper(dbSession).deleteReferencesTo(referenceUuid);
+  }
+
   public Map<String, String> selectKeysByUuids(DbSession dbSession, Collection<String> uuids) {
     return executeLargeInputs(uuids, uuids1 -> mapper(dbSession).selectByUuids(uuids1)).stream()
       .collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey));
index 9a06cc38d65b593fb21fd7ece8e58755e6b823b1..6b6c790c9b896db61d47ca2ecb47ce50f34c3072 100644 (file)
@@ -60,6 +60,10 @@ public class PortfolioDto {
     return parentUuid;
   }
 
+  public boolean isRoot() {
+    return parentUuid == null;
+  }
+
   public PortfolioDto setParentUuid(@Nullable String parentUuid) {
     this.parentUuid = parentUuid;
     return this;
index 720ecd99e7656791e5275217d79fe974d9c34fbe..f019dbaed05c47b9c0027bdb5d53ff9492263966 100644 (file)
@@ -35,8 +35,6 @@ public interface PortfolioMapper {
 
   void insert(PortfolioDto portfolio);
 
-  void deleteByUuids(String portfolioUuid);
-
   void deleteByUuids(@Param("uuids") Set<String> uuids);
 
   void deletePortfoliosByUuids(@Param("uuids") Set<String> uuids);
@@ -59,6 +57,8 @@ public interface PortfolioMapper {
 
   List<ReferenceDto> selectAllReferencesToPortfolios();
 
+  List<ReferenceDto> selectAllReferencesToApplications();
+
   Set<PortfolioProjectDto> selectAllProjectsInHierarchy(String rootUuid);
 
   List<PortfolioDto> selectByUuids(@Param("uuids") Collection<String> uuids);
@@ -67,7 +67,13 @@ public interface PortfolioMapper {
 
   List<PortfolioDto> selectAllRoots();
 
+  List<PortfolioDto> selectAll();
+
+  List<PortfolioDto> selectRootOfReferencers(String referenceUuid);
+
   void deleteReferencesTo(String referenceUuid);
 
   void deleteProjects(String portfolioUuid);
+
+  void deleteProject(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid);
 }
index 663f69e003fd113c2788393a027e1211a0ec2613..3e58120500401156cd6a90f80a1252ab87c3f2c3 100644 (file)
       p.parent_uuid is null
   </select>
 
+  <select id="selectAll" resultType="Portfolio">
+    SELECT
+      <include refid="portfolioColumns"/>
+    FROM portfolios p
+  </select>
+
    <select id="selectProjects" resultType="Project">
      SELECT
      <include refid="projectColumns"/>
       p.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
+  <select id="selectRootOfReferencers" resultType="Portfolio">
+    SELECT distinct <include refid="portfolioColumns"/>
+    FROM portfolios c
+    INNER JOIN portfolio_references pr on pr.portfolio_uuid = c.uuid
+    INNER JOIN portfolios p on p.uuid = c.root_uuid
+    where
+      pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
+  </select>
+
  <select id="selectTree" parameterType="String" resultType="Portfolio">
     SELECT <include refid="portfolioColumns"/>
     FROM portfolios p
   <select id="selectAllReferencesToPortfolios" resultType="org.sonar.db.portfolio.ReferenceDto">
     SELECT
       source.uuid as sourceUuid,
-      source.rootUuid as sourceRootUuid,
-      target.uuid as sourceUuid,
-      target.rootUuid as sourceRootUuid
+      source.root_uuid as sourceRootUuid,
+      target.uuid as targetUuid,
+      target.root_uuid as targetRootUuid
     FROM portfolio_references pr
     INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
     INNER JOIN portfolios target ON pr.reference_uuid = target.uuid
   </select>
 
+  <select id="selectAllReferencesToApplications" resultType="org.sonar.db.portfolio.ReferenceDto">
+    SELECT
+    source.uuid as sourceUuid,
+    source.root_uuid as sourceRootUuid,
+    target.uuid as targetUuid,
+    target.uuid as targetRootUuid
+    FROM portfolio_references pr
+    INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
+    INNER JOIN projects target ON pr.reference_uuid = target.uuid where target.qualifier = 'APP'
+  </select>
+
   <delete id="deletePortfoliosByUuids" parameterType="String">
     DELETE FROM portfolios WHERE uuid in
     <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
     )
   </insert>
 
+  <select id="selectReferencesTo" parameterType="String">
+    SELECT
+      <include refid="portfolioColumns"/>
+    FROM portfolios p
+    INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
+    where pr.reference_uuid =  #{referenceUuid,jdbcType=VARCHAR}
+  </select>
 
   <delete id="deleteReferencesTo" parameterType="String">
     delete from portfolio_references
     where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
   </delete>
 
+    <delete id="deleteProject" parameterType="map">
+    delete from portfolio_projects
+    where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+      and
+      project_uuid = #{projectUuid,jdbcType=VARCHAR}
+  </delete>
+
     <insert id="insertProject" parameterType="PortfolioProject">
     INSERT INTO portfolio_projects (
       uuid,
index 17cffd59a4aed68f1ed5a7fe1ebf96c29ab3261a..d921b1477deb4b4b6881074392d817e17422a874 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.project.ProjectDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
 
 public class PortfolioDaoTest {
   private final System2 system2 = new AlwaysIncreasingSystem2(1L, 1);
@@ -143,7 +144,22 @@ public class PortfolioDaoTest {
 
     assertThat(portfolioDao.selectReferencersByKey(db.getSession(), "app1"))
       .extracting("uuid").containsOnly("portfolio1", "portfolio2");
+  }
+
+  @Test
+  public void selectAllReferences() {
+    createPortfolio("portfolio1");
+    createPortfolio("portfolio2");
+    createPortfolio("portfolio3");
+    ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
+
+    portfolioDao.addReference(db.getSession(), "portfolio1", "portfolio2");
+    portfolioDao.addReference(db.getSession(), "portfolio2", "portfolio3");
+    portfolioDao.addReference(db.getSession(), "portfolio3", "app1");
 
+    assertThat(portfolioDao.selectAllReferencesToPortfolios(db.getSession()))
+      .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+      .containsOnly(tuple("portfolio1", "portfolio2"), tuple("portfolio2", "portfolio3"));
   }
 
   @Test
@@ -196,7 +212,7 @@ public class PortfolioDaoTest {
     portfolioDao.deleteProjects(db.getSession(), "portfolio1");
     assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).isEmpty();
     assertThat(portfolioDao.getProjects(db.getSession(), "portfolio2")).extracting(ProjectDto::getUuid)
-        .containsExactlyInAnyOrder("project2");
+      .containsExactlyInAnyOrder("project2");
   }
 
   @Test
index 2f330626468b744b21068139211027291aa64b8b..905d329773146fa383541aa251a50e6f630b241b 100644 (file)
@@ -92,8 +92,7 @@ public class ComponentDbTester {
   }
 
   public final ComponentDto insertPrivateProject(Consumer<ComponentDto> dtoPopulator) {
-    return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, defaults(),
-      dtoPopulator);
+    return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, defaults(), dtoPopulator);
   }
 
   public final ComponentDto insertPrivateProject(Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
@@ -102,13 +101,11 @@ public class ComponentDbTester {
   }
 
   public final ComponentDto insertPublicProject(Consumer<ComponentDto> dtoPopulator) {
-    return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(),
-      dtoPopulator);
+    return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(), dtoPopulator);
   }
 
   public final ComponentDto insertPublicProject(Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
-    return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(), componentDtoPopulator,
-      projectDtoPopulator);
+    return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(), componentDtoPopulator, projectDtoPopulator);
   }
 
   public ProjectDto insertPublicProjectDto() {
@@ -135,8 +132,7 @@ public class ComponentDbTester {
     return getProjectDto(componentDto);
   }
 
-  public ProjectDto insertPrivateProjectDto(Consumer<BranchDto> branchPopulator, Consumer<ComponentDto> componentDtoPopulator,
-    Consumer<ProjectDto> projectDtoPopulator) {
+  public ProjectDto insertPrivateProjectDto(Consumer<BranchDto> branchPopulator, Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
     ComponentDto componentDto = insertPrivateProjectWithCustomBranch(branchPopulator, componentDtoPopulator, projectDtoPopulator);
     return getProjectDto(componentDto);
   }
@@ -178,6 +174,41 @@ public class ComponentDbTester {
     return insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(false), false, dtoPopulator, portfolioPopulator);
   }
 
+  public final PortfolioDto insertPublicPortfolioDto(Consumer<ComponentDto> dtoPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(false), false, dtoPopulator, defaults());
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPrivatePortfolioDto(Consumer<ComponentDto> dtoPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator, defaults());
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPrivatePortfolioDto(Consumer<ComponentDto> dtoPopulator, Consumer<PortfolioDto> portfolioPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator,portfolioPopulator);
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPublicPortfolioDto(String uuid, Consumer<ComponentDto> dtoPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio(uuid).setPrivate(false), false, dtoPopulator, defaults());
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPublicPortfolioDto(String uuid, Consumer<ComponentDto> dtoPopulator, Consumer<PortfolioDto> portfolioPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio(uuid).setPrivate(false), false, dtoPopulator, portfolioPopulator);
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPublicPortfolioDto(Consumer<ComponentDto> dtoPopulator, Consumer<PortfolioDto> portfolioPopulator) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(false), false, dtoPopulator, portfolioPopulator);
+    return getPortfolioDto(component);
+  }
+
+  public PortfolioDto getPortfolioDto(ComponentDto portfolio) {
+    return db.getDbClient().portfolioDao().selectByUuid(dbSession, portfolio.uuid())
+      .orElseThrow(() -> new IllegalStateException("Portfolio has invalid configuration"));
+  }
+
   public ComponentDto insertComponentAndPortfolio(ComponentDto componentDto, boolean isPrivate, Consumer<ComponentDto> componentPopulator,
     Consumer<PortfolioDto> portfolioPopulator) {
     insertComponentImpl(componentDto, isPrivate, componentPopulator);
index 8125df70de196b6440b3435356c38e036580d373..b46b99ffe27120ecd848980b7593bdb43e2aa395 100644 (file)
@@ -30,6 +30,7 @@ import java.util.Set;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.permission.GlobalPermission;
+import org.sonar.db.portfolio.PortfolioDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.user.AbstractUserSession;
 
@@ -92,6 +93,20 @@ public abstract class AbstractMockUserSession<T extends AbstractMockUserSession>
     return clazz.cast(this);
   }
 
+  public T registerPortfolios(PortfolioDto... portfolios) {
+    Arrays.stream(portfolios)
+      .forEach(portfolio -> {
+        if (!portfolio.isPrivate()) {
+          this.projectUuidByPermission.put(UserRole.USER, portfolio.getUuid());
+          this.projectUuidByPermission.put(UserRole.CODEVIEWER, portfolio.getUuid());
+          this.projectPermissions.add(UserRole.USER);
+          this.projectPermissions.add(UserRole.CODEVIEWER);
+        }
+        this.projectUuidByComponentUuid.put(portfolio.getUuid(), portfolio.getUuid());
+      });
+    return clazz.cast(this);
+  }
+
   public T addProjectPermission(String permission, ComponentDto... components) {
     Arrays.stream(components).forEach(component -> {
       checkArgument(
@@ -114,7 +129,20 @@ public abstract class AbstractMockUserSession<T extends AbstractMockUserSession>
     registerProjects(projects);
     this.projectPermissions.add(permission);
     Arrays.stream(projects)
-      .forEach(component -> this.projectUuidByPermission.put(permission, component.getUuid()));
+      .forEach(project -> this.projectUuidByPermission.put(permission, project.getUuid()));
+    return clazz.cast(this);
+  }
+
+  public T addPortfolioPermission(String permission, PortfolioDto... portfolios) {
+    Arrays.stream(portfolios).forEach(component -> {
+      checkArgument(
+        component.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission),
+        "public component %s can't be granted public permission %s", component.getUuid(), permission);
+    });
+    registerPortfolios(portfolios);
+    this.projectPermissions.add(permission);
+    Arrays.stream(portfolios)
+      .forEach(portfolio -> this.projectUuidByPermission.put(permission, portfolio.getUuid()));
     return clazz.cast(this);
   }
 
index d85fa44e2c2541558d017fbd6b55c3f426954501..b0bdbce1cf22a164543b16ef0e09d04a5e797dae 100644 (file)
@@ -30,6 +30,7 @@ import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.permission.GlobalPermission;
+import org.sonar.db.portfolio.PortfolioDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
@@ -202,6 +203,11 @@ public class UserSessionRule implements TestRule, UserSession {
     return this;
   }
 
+  public UserSessionRule addPortfolioPermission(String portfolioPermission, PortfolioDto... portfolioDto) {
+    ensureAbstractMockUserSession().addPortfolioPermission(portfolioPermission, portfolioDto);
+    return this;
+  }
+
   public UserSessionRule addPermission(GlobalPermission permission) {
     ensureAbstractMockUserSession().addPermission(permission);
     return this;