diff options
13 files changed, 147 insertions, 58 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java index b0e79f88490..faf3c67c183 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java @@ -24,22 +24,25 @@ import javax.inject.Inject; import org.sonar.api.utils.MessageException; import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder; import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.server.project.DefaultBranchNameResolver; -import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; import static org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType.UNSET; public class BranchLoader { private final MutableAnalysisMetadataHolder metadataHolder; private final BranchLoaderDelegate delegate; + private final DefaultBranchNameResolver defaultBranchNameResolver; - public BranchLoader(MutableAnalysisMetadataHolder metadataHolder) { - this(metadataHolder, null); + public BranchLoader(MutableAnalysisMetadataHolder metadataHolder, DefaultBranchNameResolver defaultBranchNameResolver) { + this(metadataHolder, null, defaultBranchNameResolver); } @Inject - public BranchLoader(MutableAnalysisMetadataHolder metadataHolder, @Nullable BranchLoaderDelegate delegate) { + public BranchLoader(MutableAnalysisMetadataHolder metadataHolder, @Nullable BranchLoaderDelegate delegate, + DefaultBranchNameResolver defaultBranchNameResolver) { this.metadataHolder = metadataHolder; this.delegate = delegate; + this.defaultBranchNameResolver = defaultBranchNameResolver; } public void load(ScannerReport.Metadata metadata) { @@ -48,7 +51,7 @@ public class BranchLoader { } else if (hasBranchProperties(metadata)) { throw MessageException.of("Current edition does not support branch feature"); } else { - metadataHolder.setBranch(new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME)); + metadataHolder.setBranch(new DefaultBranchImpl(defaultBranchNameResolver.getEffectiveMainBranchName())); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java index fd0787729ac..5f1dc0ab1ad 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java @@ -24,14 +24,16 @@ import org.junit.Test; import org.sonar.api.utils.MessageException; import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule; import org.sonar.ce.task.projectanalysis.analysis.Branch; -import org.sonar.db.component.BranchDto; import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.server.project.DefaultBranchNameResolver; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; public class BranchLoaderTest { @@ -44,7 +46,7 @@ public class BranchLoaderTest { .setBranchName("bar") .build(); - assertThatThrownBy(() -> new BranchLoader(metadataHolder).load(metadata)) + assertThatThrownBy(() -> new BranchLoader(metadataHolder, mock(DefaultBranchNameResolver.class)).load(metadata)) .isInstanceOf(MessageException.class) .hasMessage("Current edition does not support branch feature"); } @@ -53,14 +55,16 @@ public class BranchLoaderTest { public void regular_analysis_of_project_is_enabled_if_delegate_is_absent() { ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder() .build(); + DefaultBranchNameResolver branchNameResolver = mock(DefaultBranchNameResolver.class); + when(branchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); - new BranchLoader(metadataHolder).load(metadata); + new BranchLoader(metadataHolder, branchNameResolver).load(metadata); assertThat(metadataHolder.getBranch()).isNotNull(); Branch branch = metadataHolder.getBranch(); assertThat(branch.isMain()).isTrue(); - assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME); + assertThat(branch.getName()).isEqualTo(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test @@ -69,7 +73,7 @@ public class BranchLoaderTest { .build(); BranchLoaderDelegate delegate = mock(BranchLoaderDelegate.class); - new BranchLoader(metadataHolder, delegate).load(metadata); + new BranchLoader(metadataHolder, delegate, mock(DefaultBranchNameResolver.class)).load(metadata); verify(delegate, times(1)).load(metadata); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java index 05131c002c9..39e22822707 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java @@ -45,6 +45,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.Project; import static java.util.Arrays.stream; @@ -297,7 +298,7 @@ public class LoadReportAnalysisMetadataHolderStepTest { private LoadReportAnalysisMetadataHolderStep createStep(CeTask ceTask) { return new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder, - dbClient, new BranchLoader(analysisMetadataHolder), pluginRepository); + dbClient, new BranchLoader(analysisMetadataHolder, mock(DefaultBranchNameResolver.class)), pluginRepository); } private static ScannerReport.Metadata.Builder newBatchReportBuilder() { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/project/DefaultBranchNameResolverTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/project/DefaultBranchNameResolverTest.java new file mode 100644 index 00000000000..a8189e10d68 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/project/DefaultBranchNameResolverTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.project; + +import java.util.Optional; +import org.junit.Test; +import org.sonar.api.config.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.core.config.CorePropertyDefinitions.SONAR_PROJECTCREATION_MAINBRANCHNAME; + +public class DefaultBranchNameResolverTest { + + @Test + public void getEffectiveMainBranchName_givenEmptyConfiguration_returnMain() { + Configuration config = mock(Configuration.class); + DefaultBranchNameResolver defaultBranchNameResolver = new DefaultBranchNameResolver(config); + String effectiveMainBranchName = defaultBranchNameResolver.getEffectiveMainBranchName(); + + assertThat(effectiveMainBranchName).isEqualTo("main"); + } + + @Test + public void getEffectiveMainBranchName_givenDevelopInConfiguration_returnDevelop() { + Configuration config = mock(Configuration.class); + when(config.get(SONAR_PROJECTCREATION_MAINBRANCHNAME)).thenReturn(Optional.of("develop")); + DefaultBranchNameResolver defaultBranchNameResolver = new DefaultBranchNameResolver(config); + String effectiveMainBranchName = defaultBranchNameResolver.getEffectiveMainBranchName(); + + assertThat(effectiveMainBranchName).isEqualTo("develop"); + } +} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index 8aeea98aa5a..baeb217f15a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -28,7 +28,6 @@ import javax.annotation.Nullable; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; -import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.i18n.I18n; import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; @@ -39,12 +38,11 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.portfolio.PortfolioDto; import org.sonar.db.portfolio.PortfolioDto.SelectionMode; import org.sonar.db.project.ProjectDto; -import org.sonar.db.property.PropertiesDao; -import org.sonar.db.property.PropertyDto; import org.sonar.server.es.ProjectIndexer.Cause; import org.sonar.server.es.ProjectIndexers; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import static java.util.Collections.singletonList; import static org.sonar.api.resources.Qualifiers.PROJECT; @@ -66,11 +64,11 @@ public class ComponentUpdater { private final FavoriteUpdater favoriteUpdater; private final ProjectIndexers projectIndexers; private final UuidFactory uuidFactory; - private final PropertiesDao propertiesDao; + private final DefaultBranchNameResolver defaultBranchNameResolver; public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2, PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, - ProjectIndexers projectIndexers, UuidFactory uuidFactory, PropertiesDao propertiesDao) { + ProjectIndexers projectIndexers, UuidFactory uuidFactory, DefaultBranchNameResolver defaultBranchNameResolver) { this.dbClient = dbClient; this.i18n = i18n; this.system2 = system2; @@ -78,7 +76,7 @@ public class ComponentUpdater { this.favoriteUpdater = favoriteUpdater; this.projectIndexers = projectIndexers; this.uuidFactory = uuidFactory; - this.propertiesDao = propertiesDao; + this.defaultBranchNameResolver = defaultBranchNameResolver; } /** @@ -91,7 +89,8 @@ public class ComponentUpdater { return create(dbSession, newComponent, userUuid, userLogin, null); } - public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName) { + public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin, + @Nullable String mainBranchName) { ComponentDto componentDto = createWithoutCommit(dbSession, newComponent, userUuid, userLogin, mainBranchName, c -> { }); commitAndIndex(dbSession, componentDto); @@ -228,16 +227,10 @@ public class ComponentUpdater { } private void createMainBranch(DbSession session, String componentUuid, @Nullable String mainBranch) { - - String branchKey = Optional.ofNullable(mainBranch) - .or(() -> Optional.ofNullable(propertiesDao.selectGlobalProperty(session, CorePropertyDefinitions.SONAR_PROJECTCREATION_MAINBRANCHNAME)) - .map(PropertyDto::getValue)) - .orElse(BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME); - BranchDto branch = new BranchDto() .setBranchType(BranchType.BRANCH) .setUuid(componentUuid) - .setKey(branchKey) + .setKey(Optional.ofNullable(mainBranch).orElse(defaultBranchNameResolver.getEffectiveMainBranchName())) .setMergeBranchUuid(null) .setExcludeFromPurge(true) .setProjectUuid(componentUuid); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionTest.java index 3b020a99fb3..bf7ffd17cfd 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionTest.java @@ -48,6 +48,7 @@ import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.l18n.I18nRule; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -80,9 +81,11 @@ public class ImportAzureProjectActionTest { private final AzureDevOpsHttpClient azureDevOpsHttpClient = mock(AzureDevOpsHttpClient.class); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); + private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, - mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), - new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(), + defaultBranchNameResolver); private final Encryption encryption = mock(Encryption.class); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); @@ -96,6 +99,7 @@ public class ImportAzureProjectActionTest { public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test @@ -111,7 +115,7 @@ public class ImportAzureProjectActionTest { GsonAzureRepo repo = getGsonAzureRepo(); when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption), "project-name", "repo-name")) - .thenReturn(repo); + .thenReturn(repo); Projects.CreateWsResponse response = ws.newRequest() .setParam("almSetting", almSetting.getKey()) @@ -156,7 +160,7 @@ public class ImportAzureProjectActionTest { GsonAzureRepo repo = getEmptyGsonAzureRepo(); when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption), "project-name", "repo-name")) - .thenReturn(repo); + .thenReturn(repo); Projects.CreateWsResponse response = ws.newRequest() .setParam("almSetting", almSetting.getKey()) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java index 9d7b2f9888e..8a245996d3d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java @@ -48,6 +48,7 @@ import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.l18n.I18nRule; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -82,8 +83,8 @@ public class ImportBitbucketCloudRepoActionTest { private final BitbucketCloudRestClient bitbucketCloudRestClient = mock(BitbucketCloudRestClient.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, - mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), - new TestProjectIndexers(), new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(), + mock(DefaultBranchNameResolver.class)); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java index a1c5c348ecc..634a9c098f1 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java @@ -53,6 +53,7 @@ import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.l18n.I18nRule; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -86,10 +87,11 @@ public class ImportBitbucketServerProjectActionTest { private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, - mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), - new TestProjectIndexers(), new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(), + defaultBranchNameResolver); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); @@ -108,6 +110,7 @@ public class ImportBitbucketServerProjectActionTest { public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionTest.java index e53fb8a3469..d4268f54c2c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionTest.java @@ -43,6 +43,7 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -56,6 +57,7 @@ import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING; import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_ORGANIZATION; import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_REPOSITORY_KEY; @@ -70,13 +72,14 @@ public class ImportGithubProjectActionTest { private final System2 system2 = mock(System2.class); private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); @Rule public DbTester db = DbTester.create(system2); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE, - mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), - new TestProjectIndexers(), new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(), + defaultBranchNameResolver); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); @@ -87,6 +90,7 @@ public class ImportGithubProjectActionTest { @Before public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionTest.java index 4f4204eb81c..dff38677e51 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionTest.java @@ -41,6 +41,7 @@ import org.sonar.server.component.ComponentUpdater; import org.sonar.server.es.TestProjectIndexers; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -72,9 +73,11 @@ public class ImportGitLabProjectActionTest { @Rule public DbTester db = DbTester.create(system2); + DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); + private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE, - mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), - new TestProjectIndexers(), new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(), + defaultBranchNameResolver); private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); @@ -87,6 +90,7 @@ public class ImportGitLabProjectActionTest { @Before public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java index ebf18e57fde..5e39f299427 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java @@ -39,7 +39,6 @@ import org.sonar.db.DbTester; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.GlobalPermission; -import org.sonar.db.property.PropertiesDao; import org.sonar.db.user.UserDto; import org.sonar.server.component.ComponentUpdater; import org.sonar.server.es.TestProjectIndexers; @@ -47,6 +46,7 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.tester.UserSessionRule; @@ -67,6 +67,7 @@ import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; +import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS; import static org.sonar.db.permission.GlobalPermission.SCAN; @@ -84,15 +85,14 @@ public class ReportSubmitterTest { public final DbTester db = DbTester.create(); private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); private final CeQueue queue = mock(CeQueueImpl.class); private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); - - private final PropertiesDao propertiesDao = mock(PropertiesDao.class); - private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), mock(System2.class), permissionTemplateService, - new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory(), propertiesDao); + new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver + ); private final BranchSupport ossEditionBranchSupport = new BranchSupport(null); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), ossEditionBranchSupport, @@ -101,6 +101,7 @@ public class ReportSubmitterTest { @Before public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java index 09263f5340a..fc0bb839c53 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java @@ -19,14 +19,13 @@ */ package org.sonar.server.component; -import java.util.Map; import java.util.Optional; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; -import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.util.SequenceUuidFactory; import org.sonar.db.DbSession; import org.sonar.db.DbTester; @@ -40,6 +39,7 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.l18n.I18nRule; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import static java.util.stream.IntStream.rangeClosed; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; @@ -52,6 +52,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.api.resources.Qualifiers.APP; import static org.sonar.api.resources.Qualifiers.VIEW; +import static org.sonar.db.component.BranchDto.DEFAULT_APPLICATION_MAIN_BRANCH_NAME; +import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; public class ComponentUpdaterTest { @@ -67,10 +69,17 @@ public class ComponentUpdaterTest { private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); + private final ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), - projectIndexers, new SequenceUuidFactory(), db.getDbClient().propertiesDao()); + projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver); + + @Before + public void before() { + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); + } @Test public void persist_and_index_when_creating_project() { @@ -99,7 +108,7 @@ public class ComponentUpdaterTest { Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isPresent(); - assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME); + assertThat(branch.get().getKey()).isEqualTo(DEFAULT_PROJECT_MAIN_BRANCH_NAME); assertThat(branch.get().getMergeBranchUuid()).isNull(); assertThat(branch.get().getBranchType()).isEqualTo(BranchType.BRANCH); assertThat(branch.get().getUuid()).isEqualTo(returned.uuid()); @@ -107,26 +116,23 @@ public class ComponentUpdaterTest { } @Test - public void create_project_with_main_branch_global_property(){ - String globalBranchName = "main-branch-global"; - db.getDbClient().propertiesDao().saveGlobalProperties(Map.of(CorePropertyDefinitions.SONAR_PROJECTCREATION_MAINBRANCHNAME, globalBranchName)); + public void create_project_with_main_branch_global_property() { + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("main-branch-global"); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .setPrivate(true) .build(); - ComponentDto returned = underTest.create(db.getSession(), project, null, null, null); + ComponentDto returned = underTest.create(db.getSession(), project, null, null); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.branchUuid()); - assertThat(branch).get().extracting(BranchDto::getBranchKey).isEqualTo(globalBranchName); - + assertThat(branch).get().extracting(BranchDto::getBranchKey).isEqualTo("main-branch-global"); } @Test - public void create_project_with_main_branch_param(){ + public void create_project_with_main_branch_param() { String customBranchName = "main-branch-custom"; - db.getDbClient().propertiesDao().saveGlobalProperties(Map.of(CorePropertyDefinitions.SONAR_PROJECTCREATION_MAINBRANCHNAME, customBranchName)); NewComponent project = NewComponent.newComponentBuilder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) @@ -137,7 +143,6 @@ public class ComponentUpdaterTest { Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.branchUuid()); assertThat(branch).get().extracting(BranchDto::getBranchKey).isEqualTo(customBranchName); - } @Test @@ -200,7 +205,7 @@ public class ComponentUpdaterTest { assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue(); Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid()); assertThat(branch).isPresent(); - assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME); + assertThat(branch.get().getKey()).isEqualTo(DEFAULT_PROJECT_MAIN_BRANCH_NAME); assertThat(branch.get().getMergeBranchUuid()).isNull(); assertThat(branch.get().getBranchType()).isEqualTo(BranchType.BRANCH); assertThat(branch.get().getUuid()).isEqualTo(returned.uuid()); @@ -331,4 +336,15 @@ public class ComponentUpdaterTest { .isInstanceOf(BadRequestException.class) .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", newKey, existingKey); } + + @Test + public void createApplicationOrPortfolio_createsComponentWithMasterBranchName() { + String componentNameAndKey = "createApplicationOrPortfolio"; + ComponentDto app = underTest.createApplicationOrPortfolio(db.getSession(), + NewComponent.newComponentBuilder().setName(componentNameAndKey).setKey(componentNameAndKey).setQualifier("APP").build(), null, null); + + Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), app.branchUuid()); + assertThat(branch).isPresent(); + assertThat(branch.get().getBranchKey()).isEqualTo(DEFAULT_APPLICATION_MAIN_BRANCH_NAME); + } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java index 4517ec9ada3..632d55617a8 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java @@ -39,6 +39,7 @@ import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.favorite.FavoriteUpdater; import org.sonar.server.l18n.I18nRule; import org.sonar.server.permission.PermissionTemplateService; +import org.sonar.server.project.DefaultBranchNameResolver; import org.sonar.server.project.ProjectDefaultVisibility; import org.sonar.server.project.Visibility; import org.sonar.server.project.ws.CreateAction.Builder; @@ -56,6 +57,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME; import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS; import static org.sonar.server.project.Visibility.PRIVATE; import static org.sonar.test.JsonAssert.assertJson; @@ -80,19 +82,21 @@ public class CreateActionTest { @Rule public final I18nRule i18n = new I18nRule().put("qualifier.TRK", "Project"); + private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); private final TestProjectIndexers projectIndexers = new TestProjectIndexers(); private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); private final WsActionTester ws = new WsActionTester( new CreateAction( db.getDbClient(), userSession, - new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), projectIndexers, - new SequenceUuidFactory(), db.getDbClient().propertiesDao()), + new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), + projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver), projectDefaultVisibility)); @Before public void before() { when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); + when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME); } @Test |