]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15866 Fix application master branch reference wrongly deselect when refresh
authorZipeng WU <zipeng.wu@sonarsource.com>
Thu, 30 Dec 2021 09:33:59 +0000 (10:33 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 30 Dec 2021 20:03:07 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java

index 35a56b845ac2489f7c77f889df90082316422857..43cd3f8f11c31980ce927a0dbd7703fe1dcdf7db 100644 (file)
@@ -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<ReferenceDto> selectAllReferencesToPortfolios(DbSession dbSession) {
index 45aebe2513c855fe2092d7587ae2de424f869ede..ed2ff995ea10899ce8cdc23baa83995584320330 100644 (file)
@@ -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()));
 
   }