From: Zipeng WU Date: Thu, 30 Dec 2021 09:33:59 +0000 (+0100) Subject: SONAR-15866 Fix application master branch reference wrongly deselect when refresh X-Git-Tag: 9.3.0.51899~85 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=83b1e2d1b7aeee70413fb705eac2592f56b2ebe1;p=sonarqube.git SONAR-15866 Fix application master branch reference wrongly deselect when refresh --- 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 35a56b845ac..43cd3f8f11c 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 @@ -128,7 +128,12 @@ public class PortfolioDao implements Dao { } public void addReference(DbSession dbSession, String portfolioUuid, String referenceUuid) { - mapper(dbSession).insertReference(uuidFactory.create(), portfolioUuid, referenceUuid, null, system2.now()); + PortfolioDto portfolio = mapper(dbSession).selectByUuid(referenceUuid); + if (portfolio == null) { + mapper(dbSession).insertReference(uuidFactory.create(), portfolioUuid, referenceUuid, referenceUuid, system2.now()); + } else { + mapper(dbSession).insertReference(uuidFactory.create(), portfolioUuid, referenceUuid, null, system2.now()); + } } public List selectAllReferencesToPortfolios(DbSession dbSession) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java index 45aebe2513c..ed2ff995ea1 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java @@ -251,7 +251,7 @@ public class PortfolioDaoTest { .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid, ReferenceDto::getTargetRootUuid, ReferenceDto::getBranchUuids) .containsOnly( tuple("portfolio3", app1.getUuid(), app1.getUuid(), singleton("branch1")), - tuple("portfolio2", app1.getUuid(), app1.getUuid(), emptySet())); + tuple("portfolio2", app1.getUuid(), app1.getUuid(), singleton(app1.getUuid()))); } @Test @@ -384,7 +384,7 @@ public class PortfolioDaoTest { assertThat(appFromDb.get()) .extracting(ReferenceDto::getTargetKey, ReferenceDto::getTargetName, ReferenceDto::getBranchUuids) - .containsExactly("app", "app", Set.of(branch1.getUuid(), branch2.getUuid())); + .containsExactly("app", "app", Set.of(branch1.getUuid(), branch2.getUuid(), app.getUuid())); } @@ -541,14 +541,14 @@ public class PortfolioDaoTest { assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app.getKey())) .isPresent() .map(ReferenceDto::getBranchUuids) - .contains(Set.of(branch1.getUuid(), branch2.getUuid())); + .contains(Set.of(branch1.getUuid(), branch2.getUuid(), app.getUuid())); portfolioDao.deleteReferenceBranch(db.getSession(), portfolio.getUuid(), app.getUuid(), branch1.getUuid()); assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app.getKey())) .isPresent() .map(ReferenceDto::getBranchUuids) - .contains(Set.of(branch2.getUuid())); + .contains(Set.of(branch2.getUuid(), app.getUuid())); }