]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17524 using new config property to set default main branch
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Tue, 1 Nov 2022 14:10:05 +0000 (15:10 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 1 Nov 2022 20:03:09 +0000 (20:03 +0000)
13 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java
server/sonar-server-common/src/test/java/org/sonar/server/project/DefaultBranchNameResolverTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/queue/ReportSubmitterTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java

index b0e79f884900e569a74d327ffe8423eb47e0e431..faf3c67c1831150d0b3a70900cb3ed57ee61ac47 100644 (file)
@@ -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()));
     }
   }
 
index fd0787729acde5cd8b262a298d1cb350c81c55c8..5f1dc0ab1ade36eb9a49996acf3d520ded29eed2 100644 (file)
@@ -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);
   }
index 05131c002c9e18e894fe6e8ddd33e1a068532b39..39e22822707874ba3fddb6a65d285b92942928fe 100644 (file)
@@ -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 (file)
index 0000000..a8189e1
--- /dev/null
@@ -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");
+  }
+}
index 8aeea98aa5ad960ac8ca984b384725c3cae4777a..baeb217f15a8b4fa304f901c54a54a2045009964 100644 (file)
@@ -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);
index 3b020a99fb344bf814aac43e31128a1a43e67f08..bf7ffd17cfd64c7e462182de90275eba25c31f39 100644 (file)
@@ -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())
index 9d7b2f9888ef6aa2d2e30b1ffd2cdc8778335a8a..8a245996d3d872878d63c977c1139b714fed9418 100644 (file)
@@ -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);
index a1c5c348ecc70c50c432c4a274cf3b5c24486e49..634a9c098f101440a95e7e78e12ee4030bd70549 100644 (file)
@@ -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
index e53fb8a3469e1c3c21e3340369a4d29050c033ce..d4268f54c2c987b60165609765a4f439983898d0 100644 (file)
@@ -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
index 4f4204eb81c0d542e78b936c0301d55923dc3c11..dff38677e51b344925df61b50e7147c1f92b43a5 100644 (file)
@@ -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
index ebf18e57fde72adbcfb9ad17f64bd55de4309844..5e39f299427432d3e457f6147785d6bc575050dd 100644 (file)
@@ -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
index 09263f5340a42fd794c4d6b2fa7adb3a6ed43d85..fc0bb839c5329ba8887fa238f2cc29d7d469a54a 100644 (file)
  */
 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);
+  }
 }
index 4517ec9ada30659267888b5b3af7456d66981bd1..632d55617a8d7d1545cafe48272b36d7fd10eca9 100644 (file)
@@ -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