From: Duarte Meneses Date: Mon, 18 Oct 2021 21:21:38 +0000 (-0500) Subject: SONAR-15498 DB schema and WS supports project branches selection in portfolios X-Git-Tag: 9.2.0.49834~63 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8f6a40a2c3e066865f64955636683ac482b50526;p=sonarqube.git SONAR-15498 DB schema and WS supports project branches selection in portfolios --- diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java index 1feb4af67ad..15c4ff52275 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java @@ -74,6 +74,7 @@ public final class SqTables { "plugins", "portfolios", "portfolio_projects", + "portfolio_proj_branches", "portfolio_references", "projects", "project_alm_settings", diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 6ef83f82a46..298be87c731 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -203,7 +203,6 @@ public class MyBatis implements Startable { confBuilder.loadAlias("Portfolio", PortfolioDto.class); confBuilder.loadAlias("PortfolioProject", PortfolioProjectDto.class); confBuilder.loadAlias("PortfolioReference", PortfolioReferenceDto.class); - confBuilder.loadAlias("PrIssue", PrIssueDto.class); confBuilder.loadAlias("ProjectQgateAssociation", ProjectQgateAssociationDto.class); confBuilder.loadAlias("Project", ProjectDto.class); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java index 5a70462b650..4682d92edfd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java @@ -19,12 +19,9 @@ */ package org.sonar.db.portfolio; -import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactory; @@ -32,9 +29,9 @@ import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.ComponentNewValue; -import org.sonar.db.project.ProjectDto; import static com.google.common.base.Preconditions.checkArgument; +import static java.lang.String.format; import static java.util.Collections.emptyList; import static java.util.Collections.singleton; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -50,6 +47,9 @@ public class PortfolioDao implements Dao { this.auditPersister = auditPersister; } + /* + * Select portfolios + */ public List selectAllRoots(DbSession dbSession) { return mapper(dbSession).selectAllRoots(); } @@ -81,6 +81,9 @@ public class PortfolioDao implements Dao { return mapper(dbSession).selectByUuids(uuids); } + /* + * Modify portfolios + */ public void insert(DbSession dbSession, PortfolioDto portfolio) { checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid()))); mapper(dbSession).insert(portfolio); @@ -106,20 +109,9 @@ public class PortfolioDao implements Dao { auditPersister.updateComponent(dbSession, toComponentNewValue(portfolio)); } - private static ComponentNewValue toComponentNewValue(PortfolioDto portfolio) { - return new ComponentNewValue(portfolio.getUuid(), portfolio.getName(), portfolio.getKey(), portfolio.isPrivate(), - portfolio.getDescription(), qualifier(portfolio)); - } - - private static String qualifier(PortfolioDto portfolioDto) { - return portfolioDto.isRoot() ? Qualifiers.VIEW : Qualifiers.SUBVIEW; - } - - public Map selectKeysByUuids(DbSession dbSession, Collection uuids) { - return executeLargeInputs(uuids, uuids1 -> mapper(dbSession).selectByUuids(uuids1)).stream() - .collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey)); - } - + /* + * Portfolio references + */ public void addReference(DbSession dbSession, String portfolioUuid, String referenceUuid) { mapper(dbSession).insertReference(new PortfolioReferenceDto() .setUuid(uuidFactory.create()) @@ -168,8 +160,11 @@ public class PortfolioDao implements Dao { return mapper(dbSession).selectReference(portfolioUuid, referenceKey); } - public List selectProjects(DbSession dbSession, String portfolioUuid) { - return mapper(dbSession).selectProjects(portfolioUuid); + /* + * Manual selection of projects + */ + public List selectPortfolioProjects(DbSession dbSession, String portfolioUuid) { + return mapper(dbSession).selectPortfolioProjects(portfolioUuid); } public List selectAllProjectsInHierarchy(DbSession dbSession, String rootUuid) { @@ -180,12 +175,15 @@ public class PortfolioDao implements Dao { return mapper(dbSession).selectAllPortfolioProjects(); } - public void addProject(DbSession dbSession, String portfolioUuid, String projectUuid) { - mapper(dbSession).insertProject(new PortfolioProjectDto() - .setUuid(uuidFactory.create()) - .setPortfolioUuid(portfolioUuid) - .setProjectUuid(projectUuid) - .setCreatedAt(system2.now())); + public PortfolioProjectDto selectPortfolioProjectOrFail(DbSession dbSession, String portfolioUuid, String projectUuid) { + return Optional.ofNullable(mapper(dbSession).selectPortfolioProject(portfolioUuid, projectUuid)).orElseThrow(() -> + new IllegalArgumentException(format("Project '%s' not selected in portfolio '%s'", projectUuid, portfolioUuid))); + } + + public String addProject(DbSession dbSession, String portfolioUuid, String projectUuid) { + String uuid = uuidFactory.create(); + mapper(dbSession).insertProject(uuid, portfolioUuid, projectUuid, system2.now()); + return uuid; } public void deleteProjects(DbSession dbSession, String portfolioUuid) { @@ -200,8 +198,30 @@ public class PortfolioDao implements Dao { mapper(dbSession).deleteAllProjects(); } + public Set selectBranches(DbSession dbSession, String portfolioProjectUuid) { + return mapper(dbSession).selectBranches(portfolioProjectUuid); + } + + public void addBranch(DbSession dbSession, String portfolioProjectUuid, String branchKey) { + mapper(dbSession).insertBranch(uuidFactory.create(), portfolioProjectUuid, branchKey, system2.now()); + } + + public void deleteBranch(DbSession dbSession, String portfolioUuid, String projectUuid, String branchKey) { + mapper(dbSession).deleteBranch(portfolioUuid, projectUuid, branchKey); + } + + /* + * Utils + */ private static PortfolioMapper mapper(DbSession session) { return session.getMapper(PortfolioMapper.class); } + private static ComponentNewValue toComponentNewValue(PortfolioDto portfolio) { + return new ComponentNewValue(portfolio.getUuid(), portfolio.isPrivate(), portfolio.getName(), portfolio.getKey(), portfolio.getDescription(), qualifier(portfolio)); + } + + private static String qualifier(PortfolioDto portfolioDto) { + return portfolioDto.isRoot() ? Qualifiers.VIEW : Qualifiers.SUBVIEW; + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java index 6b6c790c9b8..48195cc50ea 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java @@ -33,6 +33,7 @@ public class PortfolioDto { private String name; private String description; private boolean isPrivate = false; + private String branchName; private String rootUuid; private String parentUuid; @@ -69,6 +70,15 @@ public class PortfolioDto { return this; } + @CheckForNull + public String getBranchName() { + return branchName; + } + + public void setBranchName(@Nullable String branchName) { + this.branchName = branchName; + } + public String getSelectionMode() { return selectionMode; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java index c337e8a1b46..02052e05db8 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java @@ -24,13 +24,12 @@ import java.util.List; import java.util.Set; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; -import org.sonar.db.project.ProjectDto; public interface PortfolioMapper { @CheckForNull PortfolioDto selectByKey(String key); - List selectByKeys(@Param("keys")List keys); + List selectByKeys(@Param("keys") List keys); @CheckForNull PortfolioDto selectByUuid(String uuid); @@ -47,7 +46,7 @@ public interface PortfolioMapper { void insertReference(PortfolioReferenceDto portfolioReference); - void insertProject(PortfolioProjectDto portfolioProject); + void insertProject(@Param("uuid") String uuid, @Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("createdAt") long createdAt); List selectTree(String portfolioUuid); @@ -55,7 +54,9 @@ public interface PortfolioMapper { List selectReferencers(String referenceUuid); - List selectProjects(String portfolioUuid); + List selectPortfolioProjects(String portfolioUuid); + + PortfolioProjectDto selectPortfolioProject(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid); List selectAllReferencesToPortfolios(); @@ -93,5 +94,12 @@ public interface PortfolioMapper { List selectAllReferencesInHierarchy(String rootUuid); + void deleteBranch(@Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchKey") String branchKey); + + void deleteBranch(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("branchKey") String branchKey); + + void insertBranch(@Param("uuid") String uuid, @Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchKey") String branchKey, + @Param("createdAt") long createdAt); + Set selectBranches(String portfolioProjectUuid); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioProjectDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioProjectDto.java index ccdf338235b..e78de8efbe6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioProjectDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioProjectDto.java @@ -19,10 +19,15 @@ */ package org.sonar.db.portfolio; +import java.util.Set; + public class PortfolioProjectDto { private String uuid; private String portfolioUuid; + private String portfolioKey; private String projectUuid; + private String projectKey; + private Set branchKeys; private long createdAt; public String getUuid() { @@ -60,4 +65,30 @@ public class PortfolioProjectDto { this.createdAt = createdAt; return this; } + + public String getPortfolioKey() { + return portfolioKey; + } + + public PortfolioProjectDto setPortfolioKey(String portfolioKey) { + this.portfolioKey = portfolioKey; + return this; + } + + public String getProjectKey() { + return projectKey; + } + + public PortfolioProjectDto setProjectKey(String projectKey) { + this.projectKey = projectKey; + return this; + } + + public Set getBranchKeys() { + return branchKeys; + } + + public void setBranchKeys(Set branchKeys) { + this.branchKeys = branchKeys; + } } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index e39f210d60f..519d19ea785 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -2,19 +2,20 @@ - - p.uuid as uuid, - p.kee as kee, - p.name as name, - p.description as description, - p.private as isPrivate, - p.root_uuid as rootUuid, - p.parent_uuid as parentUuid, - p.selection_mode as selectionMode, - p.selection_expression as selectionExpression, - p.created_at as createdAt, - p.updated_at as updatedAt - + + p.uuid as uuid, + p.kee as kee, + p.name as name, + p.description as description, + p.private as isPrivate, + p.branch_name as branchName, + p.root_uuid as rootUuid, + p.parent_uuid as parentUuid, + p.selection_mode as selectionMode, + p.selection_expression as selectionExpression, + p.created_at as createdAt, + p.updated_at as updatedAt + p.uuid as uuid, @@ -28,6 +29,24 @@ p.updated_at as updatedAt + + pp.uuid as portfolioProject_uuid, + pp.project_uuid as projectUuid, + pp.portfolio_uuid as portfolioUuid, + pf.kee as portfolioKey, + pp.project_uuid as projectUuid, + p.kee as projectKey, + pp.created_at as createdAt, + ppb.branch_key as branchKey + + + + + + + + + - + SELECT ppb.branch_key + FROM portfolio_proj_branches ppb + WHERE ppb.portfolio_project_uuid = #{id} + + + - + SELECT + + FROM portfolio_projects pp + INNER JOIN projects p on p.uuid = pp.project_uuid + 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 + pp.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR} + and pp.project_uuid=#{projectUuid,jdbcType=VARCHAR} + + + + - SELECT - pp.uuid, - pp.project_uuid as projectUuid, - pp.portfolio_uuid as portfolioUuid, - pp.project_uuid as projectUuid, - pp.created_at as createdAt + FROM portfolio_projects pp - INNER JOIN portfolios p - ON pp.portfolio_uuid = p.uuid + INNER JOIN projects p on p.uuid = pp.project_uuid + 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 - p.root_uuid = #{rootUuid,jdbcType=VARCHAR} + pf.root_uuid = #{rootUuid,jdbcType=VARCHAR}