aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task-projectanalysis/src
diff options
context:
space:
mode:
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>2022-11-01 15:10:05 +0100
committersonartech <sonartech@sonarsource.com>2022-11-01 20:03:09 +0000
commite0b9018a1b4942f59ce5533cd9f2ca19ac54a583 (patch)
tree578e0e5ee2df372bbbdb4134f4afffc66bbc9e8c /server/sonar-ce-task-projectanalysis/src
parentb0be5da0c49b74538dbce98014298a104d4ac5b0 (diff)
downloadsonarqube-e0b9018a1b4942f59ce5533cd9f2ca19ac54a583.tar.gz
sonarqube-e0b9018a1b4942f59ce5533cd9f2ca19ac54a583.zip
SONAR-17524 using new config property to set default main branch
Diffstat (limited to 'server/sonar-ce-task-projectanalysis/src')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/BranchLoader.java13
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/BranchLoaderTest.java14
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java3
3 files changed, 19 insertions, 11 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() {