]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18856 Fix RenameAction endpoint to support project uuid
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Thu, 11 May 2023 07:49:56 +0000 (09:49 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/taskprocessor/IgnoreOrphanBranchStepIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/RenameActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/SetAutomaticDeletionProtectionActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/RenameAction.java

index b2e3feff4babff959ace0e63688d1e422dc84eba..9bb14e07ee9d5f8d7f99fbe98c900ae28782b13b 100644 (file)
@@ -37,7 +37,7 @@ public class IgnoreOrphanBranchStepIT {
   private String BRANCH_UUID = "branch_uuid";
 
   @Rule
-  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+  public DbTester dbTester = DbTester.create(System2.INSTANCE, true);
 
   private CeTask.Component component = new CeTask.Component(BRANCH_UUID, "component key", "component name");
   private CeTask ceTask = new CeTask.Builder()
index 51dcbd6b2e723c632e07caa7dbb85f53b20acb71..40ceee56eea23451b157640dae8948968eb09cdd 100644 (file)
@@ -30,6 +30,7 @@ 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.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -43,7 +44,7 @@ import static org.sonar.api.resources.Qualifiers.PROJECT;
 
 public class RenameActionIT {
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
 
@@ -102,8 +103,8 @@ public class RenameActionIT {
   @Test
   public void rename() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
     userSession.addProjectPermission(UserRole.ADMIN, project);
 
     tester.newRequest()
@@ -112,18 +113,18 @@ public class RenameActionIT {
       .execute();
 
     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
-    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid());
+    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
     assertThat(mainBranch.get().getKey()).isEqualTo("master");
 
-    Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid());
+    Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.getUuid());
     assertThat(unchangedBranch.get().getKey()).isEqualTo("branch");
   }
 
   @Test
   public void rename_with_same_name() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
     userSession.addProjectPermission(UserRole.ADMIN, project);
 
     tester.newRequest()
@@ -137,17 +138,17 @@ public class RenameActionIT {
       .execute();
 
     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
-    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid());
+    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
     assertThat(mainBranch.get().getKey()).isEqualTo("master");
 
-    Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid());
+    Optional<BranchDto> unchangedBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.getUuid());
     assertThat(unchangedBranch.get().getKey()).isEqualTo("branch");
   }
 
   @Test
   public void fail_if_name_already_used() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
     userSession.addProjectPermission(UserRole.ADMIN, project);
     db.components().insertProjectBranch(project, b -> b.setKey("branch"));
 
index d2d7fecf1cda0305b8e27d91583829444cb47945..6c9f91fbadd0c3fb209f019b2ba06a46414138e7 100644 (file)
@@ -28,8 +28,8 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 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.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -45,7 +45,7 @@ import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
 public class SetAutomaticDeletionProtectionActionIT {
 
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
 
@@ -105,7 +105,7 @@ public class SetAutomaticDeletionProtectionActionIT {
   @Test
   public void fail_if_no_administer_permission() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
 
     assertThatThrownBy(() -> tester.newRequest()
       .setParam("project", project.getKey())
@@ -119,8 +119,8 @@ public class SetAutomaticDeletionProtectionActionIT {
   @Test
   public void fail_when_attempting_to_set_main_branch_as_included_in_purge() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
     userSession.addProjectPermission(UserRole.ADMIN, project);
 
     assertThatThrownBy(() -> tester.newRequest()
@@ -135,8 +135,8 @@ public class SetAutomaticDeletionProtectionActionIT {
   @Test
   public void set_purge_exclusion() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
     userSession.addProjectPermission(UserRole.ADMIN, project);
 
     tester.newRequest()
@@ -146,11 +146,11 @@ public class SetAutomaticDeletionProtectionActionIT {
       .execute();
 
     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
-    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid());
+    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), project.getUuid());
     assertThat(mainBranch.get().getKey()).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
     assertThat(mainBranch.get().isExcludeFromPurge()).isTrue();
 
-    Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid());
+    Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.getUuid());
     assertThat(branchDto.get().getKey()).isEqualTo("branch1");
     assertThat(branchDto.get().isExcludeFromPurge()).isTrue();
   }
@@ -158,7 +158,7 @@ public class SetAutomaticDeletionProtectionActionIT {
   @Test
   public void fail_on_non_boolean_value_parameter() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
 
     assertThatThrownBy(() -> tester.newRequest()
       .setParam("project", project.getKey())
@@ -185,7 +185,7 @@ public class SetAutomaticDeletionProtectionActionIT {
   @Test
   public void fail_if_branch_does_not_exist() {
     userSession.logIn();
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
     userSession.addProjectPermission(UserRole.ADMIN, project);
 
     assertThatThrownBy(() -> tester.newRequest()
index b8b6ae5e0870747107b0d84d81bb7f772dd72ecf..f4f2440338309e88a14f00268ae1008fcc374067 100644 (file)
@@ -76,7 +76,7 @@ public class CreateActionIT {
   private final System2 system2 = System2.INSTANCE;
 
   @Rule
-  public final DbTester db = DbTester.create(system2);
+  public final DbTester db = DbTester.create(system2, true);
   @Rule
   public final UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
index 5d8346560dd609ff0fca16e6422031f2f82d797a..b9f70b079530e4c05ba8be4b3daedeb17d470e2d 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -81,7 +82,10 @@ public class RenameAction implements BranchWsAction {
       checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
         "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
 
-      dbClient.branchDao().updateBranchName(dbSession, project.getUuid(), 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);
       dbSession.commit();
       response.noContent();
     }