From: Léo Geoffroy Date: Tue, 11 Jul 2023 15:03:29 +0000 (+0200) Subject: SONAR-19850 Fix new code period bug on project creation X-Git-Tag: 10.2.0.77647~332 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2fb03df7cc014ab68faeadf983f5800597aa5cb1;p=sonarqube.git SONAR-19850 Fix new code period bug on project creation --- diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java index a5002f75844..3c9c3338e67 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java @@ -225,14 +225,15 @@ public class ImportAzureProjectActionIT { Projects.CreateWsResponse.Project result = response.getProject(); Optional 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 diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java index 4dad88d801a..79053120c52 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java @@ -206,13 +206,14 @@ public class ImportBitbucketCloudRepoActionIT { Optional 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 diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java index 19bd876d6c6..a615c9a50be 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java @@ -220,14 +220,15 @@ public class ImportBitbucketServerProjectActionIT { Optional 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 = 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() diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java index c51345fe7b7..df056b47dd6 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java @@ -202,13 +202,14 @@ public class ImportGithubProjectActionIT { Optional 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 diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java index d1eb836920e..db88cbdc36e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java @@ -171,13 +171,14 @@ public class ImportGitLabProjectActionIT { Projects.CreateWsResponse.Project result = response.getProject(); Optional 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 diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolverTest.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolverTest.java index f90d6a095df..f8be1cc69b6 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolverTest.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolverTest.java @@ -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 = 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 = 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 = 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 diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java index d92ff88457b..df1fe14ba12 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java @@ -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) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectAction.java index de64a6458c6..ffd00262e30 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectAction.java @@ -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.
" + - "Autoconfigure pull request decoration mechanism.
" + - "Requires the 'Create Projects' permission") + "Autoconfigure pull request decoration mechanism.
" + + "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); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java index 793fc5b4299..f0a4ea25979 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java @@ -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); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java index cf6b6350710..23d6c592aa1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java @@ -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.
" + - "Autoconfigure pull request decoration mechanism.
" + - "Requires the 'Create Projects' permission") + "Autoconfigure pull request decoration mechanism.
" + + "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); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java index e51a980b06f..f59b4cf84c6 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java @@ -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); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.java index 3d9ab26f8df..850c6bf3c05 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.java @@ -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
" + - "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); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolver.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolver.java index 11100f8b8fa..64085be3fd2 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolver.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/NewCodeDefinitionResolver.java @@ -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); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java index 8efa5b4218f..1eddb0b29fe 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java @@ -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); } }