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) {
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);
}
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);
}
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));
return parentUuid;
}
+ public boolean isRoot() {
+ return parentUuid == null;
+ }
+
public PortfolioDto setParentUuid(@Nullable String parentUuid) {
this.parentUuid = parentUuid;
return this;
void insert(PortfolioDto portfolio);
- void deleteByUuids(String portfolioUuid);
-
void deleteByUuids(@Param("uuids") Set<String> uuids);
void deletePortfoliosByUuids(@Param("uuids") Set<String> uuids);
List<ReferenceDto> selectAllReferencesToPortfolios();
+ List<ReferenceDto> selectAllReferencesToApplications();
+
Set<PortfolioProjectDto> selectAllProjectsInHierarchy(String rootUuid);
List<PortfolioDto> selectByUuids(@Param("uuids") Collection<String> uuids);
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);
}
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,
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);
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
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
}
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) {
}
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() {
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);
}
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);
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;
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(
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);
}
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;
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;