From: Matteo Mara Date: Tue, 29 Aug 2023 09:36:56 +0000 (+0200) Subject: SONAR-19839 branch renaming gets propagated to new code periods branch references X-Git-Tag: 10.2.0.77647~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4adb0c6df2f24190bdc216e1d305132e433e1afe;p=sonarqube.git SONAR-19839 branch renaming gets propagated to new code periods branch references --- diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java index 06777152e46..01c710e2c57 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java @@ -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 updatedNewCodePeriod = underTest.selectByUuid(dbSession, "1"); + Optional 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); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java index f3d680ed095..316d7fd7bed 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java @@ -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 selectByProject(DbSession dbSession, String projectUuid) { requireNonNull(projectUuid, MSG_PROJECT_UUID_NOT_SPECIFIED); return ofNullable(mapper(dbSession).selectByProject(projectUuid)); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java index db3b1f096d8..2dd1c24d9eb 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java @@ -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 mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid()); + assertThat(mainBranch.get().getKey()).isEqualTo("newBranchName"); + + Optional updatedCodeReference = db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), project.getUuid()); + assertThat(updatedCodeReference.get().getValue()).isEqualTo("newBranchName"); + } + + private NewCodePeriodDto getBranchReferenceNewCodePeriod(ProjectDto project) { + Optional 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(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java index 7b9b61d66ed..5a2c115f76f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java @@ -79,13 +79,16 @@ public class RenameAction implements BranchWsAction { checkPermission(project); Optional 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(); }