From 83b1e2d1b7aeee70413fb705eac2592f56b2ebe1 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Thu, 30 Dec 2021 10:33:59 +0100 Subject: [PATCH] SONAR-15866 Fix application master branch reference wrongly deselect when refresh --- .../main/java/org/sonar/db/portfolio/PortfolioDao.java | 7 ++++++- .../java/org/sonar/db/portfolio/PortfolioDaoTest.java | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) 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())); } -- 2.39.5