]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19839 branch renaming gets propagated to new code periods branch references
authorMatteo Mara <matteo.mara@sonarsource.com>
Tue, 29 Aug 2023 09:36:56 +0000 (11:36 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 30 Aug 2023 20:03:06 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java

index 06777152e46a70ccbb837bd33e0682d246ae2078..01c710e2c577a813520ed79a0e9970df92d1ec60 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.core.util.UuidFactory;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ProjectData;
 import org.sonar.db.project.ProjectDto;
 
@@ -118,6 +119,52 @@ public class NewCodePeriodDaoIT {
     assertNewCodePeriodRowCount(1);
   }
 
+  @Test
+  public void update_referenceBranchPeriod_value() {
+    insert("1", "proj-uuid", null, REFERENCE_BRANCH, "oldBranchName", null);
+    insert("2", "proj-uuid-2", null, REFERENCE_BRANCH, "anotherBranch", null);
+
+    underTest.updateBranchReferenceValues(dbSession, new BranchDto()
+      .setBranchType(BranchType.BRANCH)
+      .setUuid("branch-uuid")
+      .setProjectUuid("proj-uuid")
+      .setKey("oldBranchName"), "newBranchName");
+
+    //second project reference branch renaming should not affect the reference branch
+    underTest.updateBranchReferenceValues(dbSession, new BranchDto()
+      .setBranchType(BranchType.BRANCH)
+      .setUuid("branch-uuid")
+      .setProjectUuid("proj-uuid-2")
+      .setKey("oldBranchName"), "newBranchName");
+
+    db.commit();
+
+    Optional<NewCodePeriodDto> updatedNewCodePeriod = underTest.selectByUuid(dbSession, "1");
+    Optional<NewCodePeriodDto> unmodifiedNewCodePeriod = underTest.selectByUuid(dbSession, "2");
+
+    assertThat(updatedNewCodePeriod).isNotNull()
+      .isNotEmpty();
+
+    assertThat(unmodifiedNewCodePeriod).isNotNull()
+      .isNotEmpty();
+
+    assertNewCodePeriodRowCount(2);
+
+    NewCodePeriodDto updatedResult = updatedNewCodePeriod.get();
+    assertThat(updatedResult.getUuid()).isEqualTo("1");
+    assertThat(updatedResult.getProjectUuid()).isEqualTo("proj-uuid");
+    assertThat(updatedResult.getBranchUuid()).isNull();
+    assertThat(updatedResult.getType()).isEqualTo(REFERENCE_BRANCH);
+    assertThat(updatedResult.getValue()).isEqualTo("newBranchName");
+
+    NewCodePeriodDto unmodifiedResult = unmodifiedNewCodePeriod.get();
+    assertThat(unmodifiedResult.getUuid()).isEqualTo("2");
+    assertThat(unmodifiedResult.getProjectUuid()).isEqualTo("proj-uuid-2");
+    assertThat(unmodifiedResult.getBranchUuid()).isNull();
+    assertThat(unmodifiedResult.getType()).isEqualTo(REFERENCE_BRANCH);
+    assertThat(unmodifiedResult.getValue()).isEqualTo("anotherBranch");
+  }
+
   @Test
   public void insert_with_upsert() {
     insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);
index f3d680ed0951be35c8fadf0788bfcf5e44da2cd6..316d7fd7bed654464a0811bbd29b303c2ebec6de 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
 
 import static java.util.Objects.requireNonNull;
 import static java.util.Optional.ofNullable;
@@ -74,6 +75,15 @@ public class NewCodePeriodDao implements Dao {
     mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
   }
 
+  public void updateBranchReferenceValues(DbSession dbSession, BranchDto branchDto, String newBranchName) {
+    requireNonNull(branchDto, "Original referenced branch must be specified.");
+    requireNonNull(branchDto.getProjectUuid(), MSG_PROJECT_UUID_NOT_SPECIFIED);
+    requireNonNull(newBranchName, "New branch name must be specified.");
+    selectAllByProject(dbSession, branchDto.getProjectUuid()).stream()
+      .filter(newCP -> NewCodePeriodType.REFERENCE_BRANCH.equals(newCP.getType()) && branchDto.getBranchKey().equals(newCP.getValue()))
+      .forEach(newCodePeriodDto -> update(dbSession, newCodePeriodDto.setValue(newBranchName)));
+  }
+
   public Optional<NewCodePeriodDto> selectByProject(DbSession dbSession, String projectUuid) {
     requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED);
     return ofNullable(mapper(dbSession).selectByProject(projectUuid));
index db3b1f096d8f3a9eb11d584c8ad64ccffc87231d..2dd1c24d9eb8f95b0ebb06ee8bd9fb86c7a60631 100644 (file)
@@ -30,6 +30,8 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ResourceTypesRule;
+import org.sonar.db.newcodeperiod.NewCodePeriodDto;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -120,6 +122,34 @@ public class RenameActionIT {
     assertThat(unchangedBranch.get().getKey()).isEqualTo("branch");
   }
 
+  @Test
+  public void whenRenameIsTriggered_eventualBranchReferencesPeriods_areUpdatedToo() {
+    userSession.logIn();
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    db.newCodePeriods().insert(getBranchReferenceNewCodePeriod(project));
+    userSession.addProjectPermission(UserRole.ADMIN, project);
+
+    tester.newRequest()
+      .setParam("project", project.getKey())
+      .setParam("name", "newBranchName")
+      .execute();
+
+    assertThat(db.countRowsOfTable("project_branches")).isEqualTo(1);
+    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
+    assertThat(mainBranch.get().getKey()).isEqualTo("newBranchName");
+
+    Optional<NewCodePeriodDto> updatedCodeReference = db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), project.getUuid());
+    assertThat(updatedCodeReference.get().getValue()).isEqualTo("newBranchName");
+  }
+
+  private NewCodePeriodDto getBranchReferenceNewCodePeriod(ProjectDto project) {
+    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
+    return new NewCodePeriodDto()
+      .setProjectUuid(project.getUuid())
+      .setValue(mainBranch.get().getBranchKey())
+      .setType(NewCodePeriodType.REFERENCE_BRANCH);
+  }
+
   @Test
   public void rename_with_same_name() {
     userSession.logIn();
index 7b9b61d66ed2d95f106006b30512efffa9643ef7..5a2c115f76fa7231735ee03edf0fcab2ee445a01 100644 (file)
@@ -79,13 +79,16 @@ public class RenameAction implements BranchWsAction {
       checkPermission(project);
 
       Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), newBranchName);
-      checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
+      checkArgument(existingBranch.filter(b -> !b.isMain()).isEmpty(),
         "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
 
       BranchDto mainBranchDto = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, project.getUuid())
         .orElseThrow(() -> new NotFoundException("Cannot find main branch for project: " + project.getUuid()));
 
       dbClient.branchDao().updateBranchName(dbSession, mainBranchDto.getUuid(), newBranchName);
+
+      dbClient.newCodePeriodDao().updateBranchReferenceValues(dbSession, mainBranchDto, newBranchName);
+
       dbSession.commit();
       response.noContent();
     }