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) {
} 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()));
}
}
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 {
.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");
}
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
.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);
}
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;
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() {
--- /dev/null
+/*
+ * 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");
+ }
+}
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;
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;
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;
this.favoriteUpdater = favoriteUpdater;
this.projectIndexers = projectIndexers;
this.uuidFactory = uuidFactory;
- this.propertiesDao = propertiesDao;
+ this.defaultBranchNameResolver = defaultBranchNameResolver;
}
/**
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);
}
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);
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;
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);
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
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())
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())
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;
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);
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;
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);
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
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;
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;
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);
@Before
public void before() {
when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
+ when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
}
@Test
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;
@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);
@Before
public void before() {
when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
+ when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
}
@Test
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;
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;
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;
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,
@Before
public void before() {
when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC);
+ when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
}
@Test
*/
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;
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;
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 {
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() {
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());
}
@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)
Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.branchUuid());
assertThat(branch).get().extracting(BranchDto::getBranchKey).isEqualTo(customBranchName);
-
}
@Test
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());
.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);
+ }
}
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;
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;
@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