]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19850 Fix new code period bug on project creation
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Tue, 11 Jul 2023 15:03:29 +0000 (17:03 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 19 Jul 2023 20:03:06 +0000 (20:03 +0000)
14 files changed:
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolverTest.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/almintegration/ws/azure/ImportAzureProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolver.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java

index a5002f75844d76915d97e9bb6af0ed34b7d26c14..3c9c3338e67ce82ed68be81b8b9038f1beb851e5 100644 (file)
@@ -225,14 +225,15 @@ public class ImportAzureProjectActionIT {
     Projects.CreateWsResponse.Project result = response.getProject();
 
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
+    BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
     assertThat(projectDto).isPresent();
 
     String projectUuid = projectDto.get().getUuid();
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
-      .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
+      .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
   }
 
   @Test
index 4dad88d801a24ad1533c26083408bcec0f22ad9d..79053120c5264b149fddb0d7dc2b1483071cbb2e 100644 (file)
@@ -206,13 +206,14 @@ public class ImportBitbucketCloudRepoActionIT {
 
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
     assertThat(projectDto).isPresent();
+    BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
 
     String projectUuid = projectDto.get().getUuid();
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
-      .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
+      .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
   }
 
   @Test
index 19bd876d6c641257b6711381d5cb05744d5093c7..a615c9a50beba861c0e83a4b498cf8a38394b18a 100644 (file)
@@ -220,14 +220,15 @@ public class ImportBitbucketServerProjectActionIT {
 
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
     assertThat(projectDto).isPresent();
+    BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
 
 
     String projectUuid = projectDto.get().getUuid();
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
-      .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
+      .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
   }
 
   @Test
@@ -296,7 +297,6 @@ public class ImportBitbucketServerProjectActionIT {
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
     assertThat(projectDto).isPresent();
 
-
     String projectUuid = projectDto.get().getUuid();
     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectUuid))
       .isPresent()
index c51345fe7b7a04c7e9dc29f7b000c944121520ec..df056b47dd6f41aa92cbe6255763c93120a5b6f3 100644 (file)
@@ -202,13 +202,14 @@ public class ImportGithubProjectActionIT {
 
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
     assertThat(projectDto).isPresent();
+    BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
 
     String projectUuid = projectDto.get().getUuid();
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
-      .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
+      .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
   }
 
   @Test
index d1eb836920e67bad950924383f002f5a095de5e1..db88cbdc36e50c38940c527ac076b4c0494c8a3a 100644 (file)
@@ -171,13 +171,14 @@ public class ImportGitLabProjectActionIT {
     Projects.CreateWsResponse.Project result = response.getProject();
 
     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
+    BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
 
     String projectUuid = projectDto.get().getUuid();
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
-      .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
+      .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
   }
 
   @Test
index f90d6a095df806067a1d0dd0591bdb267a220444..f8be1cc69b63d2e85088c508104c78cff1e01920 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
+import org.sonar.server.component.ComponentCreationData;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatNoException;
@@ -40,6 +41,7 @@ import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
 
 public class NewCodeDefinitionResolverTest {
 
+  private static final String MAIN_BRANCH_UUID = "main-branch-uuid";
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
@@ -54,49 +56,49 @@ public class NewCodeDefinitionResolverTest {
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_no_valid_type() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, "nonValid", null))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, "nonValid", null))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("Invalid type: nonValid");
   }
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_type_is_not_allowed() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, SPECIFIC_ANALYSIS.name(), null))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, SPECIFIC_ANALYSIS.name(), null))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. `newCodeDefinitionType` can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]");
   }
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_no_value_for_days() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), null))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), null))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("New code definition type 'NUMBER_OF_DAYS' requires a newCodeDefinitionValue");
   }
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_days_is_invalid() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), "unknown"))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID,  MAIN_BRANCH, NUMBER_OF_DAYS.name(), "unknown"))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("Failed to parse number of days: unknown");
   }
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_value_is_set_for_reference_branch() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), "feature/zw"))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, REFERENCE_BRANCH.name(), "feature/zw"))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("Unexpected value for newCodeDefinitionType 'REFERENCE_BRANCH'");
   }
 
   @Test
   public void createNewCodeDefinition_throw_IAE_if_previous_version_type_and_value_provided() {
-    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), "10.2.3"))
+    assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, PREVIOUS_VERSION.name(), "10.2.3"))
       .isInstanceOf(IllegalArgumentException.class)
       .hasMessageContaining("Unexpected value for newCodeDefinitionType 'PREVIOUS_VERSION'");
   }
 
   @Test
   public void createNewCodeDefinition_persist_previous_version_type() {
-    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
+    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
 
     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
     assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
@@ -106,7 +108,7 @@ public class NewCodeDefinitionResolverTest {
   public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
     String numberOfDays = "30";
 
-    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
+    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
 
     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
 
@@ -119,15 +121,15 @@ public class NewCodeDefinitionResolverTest {
 
   @Test
   public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
-    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
+    newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
 
     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
 
     assertThat(newCodePeriodDto)
       .isPresent()
       .get()
-      .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
-      .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH);
+      .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid, NewCodePeriodDto::getProjectUuid)
+      .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH, null, DEFAULT_PROJECT_ID);
   }
 
   @Test
index d92ff88457bde40c23ef043caf945d723ceaab9c..df1fe14ba12aa4bf977b6c00cda5a3acacbc3789 100644 (file)
@@ -394,9 +394,9 @@ public class CreateActionIT {
       .setNewCodeDefinitionType(REFERENCE_BRANCH.name())
       .build());
 
-    ComponentDto component = db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
+    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
 
-    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), component.uuid()))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
@@ -414,9 +414,9 @@ public class CreateActionIT {
       .setNewCodeDefinitionType(REFERENCE_BRANCH.name())
       .build());
 
-    ComponentDto component = db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
+    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
 
-    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), component.uuid()))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
@@ -434,9 +434,9 @@ public class CreateActionIT {
       .setNewCodeDefinitionValue("30")
       .build());
 
-    ComponentDto component = db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
+    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
 
-    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), component.uuid()))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
@@ -457,9 +457,10 @@ public class CreateActionIT {
       .setNewCodeDefinitionValue("30")
       .build());
 
+    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
     ComponentDto component = db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
 
-    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), component.uuid(), component.uuid()))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectDto.getUuid(), component.uuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
@@ -519,9 +520,9 @@ public class CreateActionIT {
       .setNewCodeDefinitionValue("30")
       .build());
 
-    ComponentDto component = db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
+    ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get();
 
-    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), component.uuid()))
+    assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
       .isPresent()
       .get()
       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
index de64a6458c6a85a2d606d033485a7215bc5ff989..ffd00262e30dc8aa0afdc78f41b6ad4465e1d416 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.alm.pat.AlmPatDto;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
 import org.sonar.server.almintegration.ws.ImportHelper;
@@ -90,8 +91,8 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction {
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("import_azure_project")
       .setDescription("Create a SonarQube project with the information from the provided Azure DevOps project.<br/>" +
-        "Autoconfigure pull request decoration mechanism.<br/>" +
-        "Requires the 'Create Projects' permission")
+                      "Autoconfigure pull request decoration mechanism.<br/>" +
+                      "Requires the 'Create Projects' permission")
       .setPost(true)
       .setInternal(true)
       .setSince("8.6")
@@ -146,12 +147,13 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction {
 
       ComponentCreationData componentCreationData = createProject(dbSession, repo);
       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
       populatePRSetting(dbSession, repo, projectDto, almSettingDto);
 
       checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
 
       if (newCodeDefinitionType != null) {
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(),
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
           Optional.ofNullable(repo.getDefaultBranchName()).orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
           newCodeDefinitionType, newCodeDefinitionValue);
       }
index 793fc5b4299d5c2e9577f10217208ac07715674b..f0a4ea259797a67be8d18f3097ed5f15e53f9e8f 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.alm.pat.AlmPatDto;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
 import org.sonar.server.almintegration.ws.ImportHelper;
@@ -142,13 +143,14 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction {
 
       ComponentCreationData componentCreationData = createProject(dbSession, workspace, repo, repo.getMainBranch().getName());
       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
 
       populatePRSetting(dbSession, repo, projectDto, almSettingDto);
 
       checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
 
       if (newCodeDefinitionType != null) {
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(),
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
           Optional.ofNullable(repo.getMainBranch().getName()).orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
           newCodeDefinitionType, newCodeDefinitionValue);
       }
index cf6b63507108090c4b5bf6ab4292375ded618cda..23d6c592aa19c019039b06c8d9000d3d8cf26df8 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.alm.pat.AlmPatDto;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
 import org.sonar.server.almintegration.ws.ImportHelper;
@@ -96,8 +97,8 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("import_bitbucketserver_project")
       .setDescription("Create a SonarQube project with the information from the provided BitbucketServer project.<br/>" +
-        "Autoconfigure pull request decoration mechanism.<br/>" +
-        "Requires the 'Create Projects' permission")
+                      "Autoconfigure pull request decoration mechanism.<br/>" +
+                      "Requires the 'Create Projects' permission")
       .setPost(true)
       .setInternal(true)
       .setSince("8.2")
@@ -154,13 +155,14 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
 
       ComponentCreationData componentCreationData = createProject(dbSession, repo, defaultBranchName);
       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
 
       populatePRSetting(dbSession, repo, projectDto, almSettingDto);
 
       checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
 
       if (newCodeDefinitionType != null) {
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(),
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
           Optional.ofNullable(defaultBranchName).orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
           newCodeDefinitionType, newCodeDefinitionValue);
       }
index e51a980b06f7897e1257af1ae9e8aac0aded4c1b..f59b4cf84c617b970b07172fa60c8393a2f57460 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.alm.pat.AlmPatDto;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
 import org.sonar.server.almintegration.ws.ImportHelper;
@@ -157,13 +158,14 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction {
 
       ComponentCreationData componentCreationData = createProject(dbSession, repository, repository.getDefaultBranch());
       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
 
       populatePRSetting(dbSession, repository, projectDto, almSettingDto);
 
       checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
 
       if (newCodeDefinitionType != null) {
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(),
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
           Optional.ofNullable(repository.getDefaultBranch()).orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
           newCodeDefinitionType, newCodeDefinitionValue);
       }
index 3d9ab26f8dfa6b82b08415f867fbd49aec25f79f..850c6bf3c058cb1754008520458d71534ce14e8b 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.alm.pat.AlmPatDto;
 import org.sonar.db.alm.setting.AlmSettingDto;
 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
 import org.sonar.server.almintegration.ws.ImportHelper;
@@ -89,7 +90,7 @@ public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("import_gitlab_project")
       .setDescription("Import a GitLab project to SonarQube, creating a new project and configuring MR decoration<br/>" +
-        "Requires the 'Create Projects' permission")
+                      "Requires the 'Create Projects' permission")
       .setPost(true)
       .setSince("8.5")
       .setHandler(this);
@@ -135,13 +136,14 @@ public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
 
       ComponentCreationData componentCreationData = createProject(dbSession, gitlabProject, almMainBranchName.orElse(null));
       ProjectDto projectDto = Optional.ofNullable(componentCreationData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentCreationData.mainBranchDto()).orElseThrow();
       populateMRSetting(dbSession, gitlabProjectId, projectDto, almSettingDto);
 
       checkNewCodeDefinitionParam(newCodeDefinitionType, newCodeDefinitionValue);
 
       if (newCodeDefinitionType != null) {
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), almMainBranchName.orElse(defaultBranchNameResolver.getEffectiveMainBranchName()),
-          newCodeDefinitionType, newCodeDefinitionValue);
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(), mainBranchDto.getUuid(),
+          almMainBranchName.orElse(defaultBranchNameResolver.getEffectiveMainBranchName()), newCodeDefinitionType, newCodeDefinitionValue);
       }
 
       componentUpdater.commitAndIndex(dbSession, componentCreationData);
index 11100f8b8faae0a8bf394583da3a33e1b2e6711b..64085be3fd29b913f4e30f4cccc1f7ab6ba2c29d 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
 import org.sonar.db.newcodeperiod.NewCodePeriodParser;
 import org.sonar.db.newcodeperiod.NewCodePeriodType;
+import org.sonar.server.component.ComponentCreationData;
 
 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
 import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
@@ -70,7 +71,8 @@ public class NewCodeDefinitionResolver {
     this.editionProvider = editionProvider;
   }
 
-  public void createNewCodeDefinition(DbSession dbSession, String projectUuid, String defaultBranchName, String newCodeDefinitionType, String newCodeDefinitionValue) {
+  public void createNewCodeDefinition(DbSession dbSession, String projectUuid, String mainBranchUuid,
+    String defaultBranchName, String newCodeDefinitionType, String newCodeDefinitionValue) {
 
     boolean isCommunityEdition = editionProvider.get().filter(EditionProvider.Edition.COMMUNITY::equals).isPresent();
     NewCodePeriodType newCodePeriodType = parseNewCodeDefinitionType(newCodeDefinitionType);
@@ -80,7 +82,7 @@ public class NewCodeDefinitionResolver {
     dto.setProjectUuid(projectUuid);
 
     if (isCommunityEdition) {
-      dto.setBranchUuid(projectUuid);
+      dto.setBranchUuid(mainBranchUuid);
     }
 
     getNewCodeDefinitionValueProjectCreation(newCodePeriodType, newCodeDefinitionValue, defaultBranchName).ifPresent(dto::setValue);
index 8efa5b4218f537943e1b9ec0cbe75074609a8edd..1eddb0b29fe82b6f4373b5bbcae99742e31f2f97 100644 (file)
@@ -28,7 +28,9 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.entity.EntityDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
@@ -134,13 +136,17 @@ public class CreateAction implements ProjectsWsAction {
       userSession.checkPermission(PROVISION_PROJECTS);
       checkNewCodeDefinitionParam(request.getNewCodeDefinitionType(), request.getNewCodeDefinitionValue());
       ComponentCreationData componentData = createProject(request, dbSession);
+      ProjectDto projectDto = Optional.ofNullable(componentData.projectDto()).orElseThrow();
+      BranchDto mainBranchDto = Optional.ofNullable(componentData.mainBranchDto()).orElseThrow();
+
       if (request.getNewCodeDefinitionType() != null) {
         String defaultBranchName = Optional.ofNullable(request.getMainBranchKey()).orElse(defaultBranchNameResolver.getEffectiveMainBranchName());
-        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, componentData.mainBranchDto().getUuid(), defaultBranchName, request.getNewCodeDefinitionType(),
+        newCodeDefinitionResolver.createNewCodeDefinition(dbSession, projectDto.getUuid(),
+          mainBranchDto.getUuid(), defaultBranchName, request.getNewCodeDefinitionType(),
           request.getNewCodeDefinitionValue());
       }
       componentUpdater.commitAndIndex(dbSession, componentData);
-      return toCreateResponse(componentData.projectDto());
+      return toCreateResponse(projectDto);
     }
   }