String getPullRequestKey();
/**
- * The project as represented by the main branch. It is used to load settings
- * like Quality gates, webhooks and configuration.
- *
- * In case of analysis of main branch, the returned value is the main branch,
- * so its uuid and key are the same in
- * {@link org.sonar.ce.task.projectanalysis.component.TreeRootHolder#getRoot().
- *
- * In case of analysis of non-main branch or pull request, the returned value
- * is the main branch. Its uuid and key are different than
- * {@link org.sonar.ce.task.projectanalysis.component.TreeRootHolder#getRoot().
+ * The project being analyzed. It can be a project, application or portfolio.
+ * It is used to load settings like Quality gates, webhooks and configuration.
*
* @throws IllegalStateException if project has not been set
*/
import org.sonar.db.DbTester;
import org.sonar.db.audit.AuditPersister;
import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.ProjectData;
import org.sonar.db.project.ApplicationProjectDto;
import org.sonar.db.project.ProjectDto;
public class PortfolioDaoIT {
private final System2 system2 = new AlwaysIncreasingSystem2(1L, 1);
@Rule
- public DbTester db = DbTester.create(system2);
+ public DbTester db = DbTester.create(system2, true);
private final AuditPersister audit = mock(AuditPersister.class);
private final PortfolioDao portfolioDao = new PortfolioDao(system2, UuidFactoryFast.getInstance(), audit);
private final DbSession session = db.getSession();
PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
- ProjectDto app1 = db.components().insertPrivateApplication(c -> c.setUuid("app1")).getProjectDto();
+ ProjectDto app1 = db.components().insertPrivateApplication("app1").getProjectDto();
portfolioDao.addReference(session, "portfolio1", "portfolio2");
portfolioDao.addReference(session, "portfolio1", app1.getUuid());
portfolioDao.addReference(session, "portfolio2", app1.getUuid());
@Test
public void selectAllProjectsInHierarchy() {
- ProjectDto p1 = db.components().insertPrivateProject("p1").getProjectDto();
- ProjectDto p2 = db.components().insertPrivateProject("p2").getProjectDto();
- ProjectDto p3 = db.components().insertPrivateProject("p3").getProjectDto();
- ProjectDto p4 = db.components().insertPrivateProject("p4").getProjectDto();
+ ProjectData p1 = db.components().insertPrivateProject("p1");
+ ProjectData p2 = db.components().insertPrivateProject("p2");
+ ProjectData p3 = db.components().insertPrivateProject("p3");
+ ProjectData p4 = db.components().insertPrivateProject("p4");
PortfolioDto root = db.components().insertPrivatePortfolioDto("root");
PortfolioDto child1 = addPortfolio(root, "child1");
PortfolioDto child11 = addPortfolio(child1, "child11");
PortfolioDto root2 = db.components().insertPrivatePortfolioDto("root2");
- db.components().addPortfolioProject(root, p1);
- db.components().addPortfolioProject(child1, p2);
- db.components().addPortfolioProject(child11, p3);
- db.components().addPortfolioProject(root2, p4);
+ db.components().addPortfolioProject(root, p1.getProjectDto());
+ db.components().addPortfolioProject(child1, p2.getProjectDto());
+ db.components().addPortfolioProject(child11, p3.getProjectDto());
+ db.components().addPortfolioProject(root2, p4.getProjectDto());
- db.components().addPortfolioProjectBranch(root, p1, "branch1");
+ db.components().addPortfolioProjectBranch(root, p1.getProjectDto(), "branch1");
session.commit();
assertThat(portfolioDao.selectAllProjectsInHierarchy(session, root.getUuid()))
- .extracting(PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
- .containsExactlyInAnyOrder(tuple("p1", Set.of("branch1")), tuple("p2", emptySet()), tuple("p3", emptySet()));
+ .extracting(PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids, PortfolioProjectDto::getMainBranchUuid)
+ .containsExactlyInAnyOrder(
+ tuple("p1", Set.of("branch1"), p1.getMainBranchDto().getUuid()),
+ tuple("p2", emptySet(), p2.getMainBranchDto().getUuid()),
+ tuple("p3", emptySet(), p3.getMainBranchDto().getUuid()));
assertThat(portfolioDao.selectAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
}
if (branchKeys.isEmpty()) {
return emptyList();
}
- return executeLargeInputs(branchKeys, partition -> mapper(dbSession).selectByKeys(projectUuid, branchKeys));
+ return executeLargeInputs(branchKeys, partition -> mapper(dbSession).selectByKeys(projectUuid, partition));
}
/*
if (projectUuids.isEmpty()) {
return List.of();
}
- return mapper(dbSession).selectMainBranchesByProjectUuids(projectUuids);
+ return executeLargeInputs(projectUuids, partition -> mapper(dbSession).selectMainBranchesByProjectUuids(partition));
}
public List<PrBranchAnalyzedLanguageCountByProjectDto> countPrBranchAnalyzedLanguageByProjectUuid(DbSession dbSession) {
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);
+ List<BranchDto> selectByKeys(@Param("projectUuid") String projectUuid, @Param("keys") Collection<String> branchKeys);
BranchDto selectByUuid(@Param("uuid") String uuid);
Optional<BranchDto> selectMainBranchByProjectUuid(String projectUuid);
- List<BranchDto> selectMainBranchesByProjectUuids(@Param("projectUuids") Set<String> projectUuids);
+ List<BranchDto> selectMainBranchesByProjectUuids(@Param("projectUuids") Collection<String> projectUuids);
}
private String projectUuid;
private String projectKey;
private Set<String> branchUuids;
+ private String mainBranchUuid;
private long createdAt;
public String getUuid() {
return this;
}
+ public String getMainBranchUuid() {
+ return mainBranchUuid;
+ }
+
+ public void setMainBranchUuid(String mainBranchUuid) {
+ this.mainBranchUuid = mainBranchUuid;
+ }
+
public String getPortfolioUuid() {
return portfolioUuid;
}
pb.project_uuid = #{projectUuid, jdbcType=VARCHAR}
</select>
- <select id="selectMainBranchByProjectUuid" parameterType="string" resultType="org.sonar.db.component.BranchDto">
+ <select id="selectMainBranchByProjectUuid" resultType="org.sonar.db.component.BranchDto">
select <include refid="columns"/>
from project_branches pb
where
pf.kee as portfolioKey,
p.kee as projectKey,
pp.created_at as createdAt,
- ppb.branch_uuid as branchUuid
+ ppb.branch_uuid as branchUuid,
+ pb.uuid as mainBranchUuid
</sql>
<sql id="portfolioReferenceColumns">
<include refid="portfolioProjectColumns"/>
FROM portfolio_projects pp
INNER JOIN projects p on p.uuid = pp.project_uuid
+ INNER JOIN project_branches pb on pb.project_uuid = p.uuid and pb.is_main = ${_true}
INNER JOIN portfolios pf on pf.uuid = pp.portfolio_uuid
LEFT OUTER JOIN portfolio_proj_branches ppb on pp.uuid = ppb.portfolio_project_uuid
WHERE
<include refid="portfolioProjectColumns"/>
FROM portfolio_projects pp
INNER JOIN projects p on p.uuid = pp.project_uuid
+ INNER JOIN project_branches pb on pb.project_uuid = p.uuid and pb.is_main = ${_true}
INNER JOIN portfolios pf on pf.uuid = pp.portfolio_uuid
LEFT OUTER JOIN portfolio_proj_branches ppb on pp.uuid = ppb.portfolio_project_uuid
WHERE
<include refid="portfolioProjectColumns"/>
FROM portfolio_projects pp
INNER JOIN projects p on p.uuid = pp.project_uuid
+ INNER JOIN project_branches pb on pb.project_uuid = p.uuid and pb.is_main = ${_true}
INNER JOIN portfolios pf on pf.uuid = pp.portfolio_uuid
LEFT OUTER JOIN portfolio_proj_branches ppb on pp.uuid = ppb.portfolio_project_uuid
</select>
<include refid="portfolioProjectColumns"/>
FROM portfolio_projects pp
INNER JOIN projects p on p.uuid = pp.project_uuid
+ INNER JOIN project_branches pb on pb.project_uuid = p.uuid and pb.is_main = ${_true}
INNER JOIN portfolios pf on pp.portfolio_uuid = pf.uuid
LEFT OUTER JOIN portfolio_proj_branches ppb on pp.uuid = ppb.portfolio_project_uuid
where
import java.util.Arrays;
import java.util.List;
-import java.util.Optional;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
}
public ProjectData insertPublicProject(String uuid) {
- return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(uuid), false);
+ if (useDifferentUuids) {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(), defaults(), p -> p.setUuid(uuid));
+ } else {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(uuid), false);
+ }
}
public ProjectData insertPublicProject(String uuid, Consumer<ComponentDto> dtoPopulator) {
- return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(uuid), false, defaults(), dtoPopulator);
+ if (useDifferentUuids) {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(), false, defaults(), dtoPopulator, p -> p.setUuid(uuid));
+ } else {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(uuid), false, defaults(), dtoPopulator);
+ }
}
public ProjectData insertPublicProject(ComponentDto componentDto) {
}
public ProjectData insertPrivateProject(String uuid) {
- return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true);
+ if (useDifferentUuids) {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, defaults(), defaults(), p -> p.setUuid(uuid));
+ } else {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true);
+ }
}
public final ProjectData insertPrivateProject(Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication().setPrivate(false), false, defaults(), dtoPopulator);
}
+ public final ProjectData insertPrivateApplication(String uuid, Consumer<ComponentDto> dtoPopulator) {
+ return insertPrivateApplication(dtoPopulator, p -> p.setUuid(uuid));
+ }
+
+ public final ProjectData insertPrivateApplication(String uuid) {
+ return insertPrivateApplication(defaults(), p -> p.setUuid(uuid));
+ }
+
public final ProjectData insertPrivateApplication(Consumer<ComponentDto> dtoPopulator) {
return insertPrivateApplication(dtoPopulator, defaults());
}
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.portfolio.PortfolioDto;
import org.sonar.db.project.ProjectDto;
import static java.util.Collections.emptyList;
return new Project(project.getUuid(), project.getKey(), project.getName(), project.getDescription(), project.getTags());
}
+ public static Project from(PortfolioDto portfolio) {
+ return new Project(portfolio.getUuid(), portfolio.getKey(), portfolio.getName(), portfolio.getDescription(), emptyList());
+ }
+
/**
* Always links to a row that exists in database.
*/