]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17559 Refine conditions under which we display a warning when using sonar.newCo...
authorWouter Admiraal <wouter.admiraal@sonarsource.com>
Fri, 4 Nov 2022 15:58:43 +0000 (16:58 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 7 Nov 2022 20:02:53 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadPeriodsStepTest.java

index e9985b31e76241f0fd9347e49731ce125d3d7c42..19853944a19e342270929b8d4a8e88a9fa0dd05a 100644 (file)
@@ -93,21 +93,21 @@ public class LoadPeriodsStep implements ComputationStep {
       .orElse(null);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      Optional<NewCodePeriodDto> specificSetting = firstPresent(Arrays.asList(
-        () -> getBranchSetting(dbSession, projectUuid, branchUuid),
-        () -> getProjectSetting(dbSession, projectUuid)
-      ));
+      Optional<NewCodePeriodDto> branchSpecificSetting = getBranchSetting(dbSession, projectUuid, branchUuid);
 
       if (newCodePeriod == null) {
-        newCodePeriod = specificSetting.or(() -> getGlobalSetting(dbSession)).orElse(NewCodePeriodDto.defaultInstance());
+        newCodePeriod = branchSpecificSetting
+          .or(() -> getProjectSetting(dbSession, projectUuid))
+          .or(() -> getGlobalSetting(dbSession))
+          .orElse(NewCodePeriodDto.defaultInstance());
 
         if (analysisMetadataHolder.isFirstAnalysis() && newCodePeriod.getType() != REFERENCE_BRANCH) {
           periodsHolder.setPeriod(null);
           return;
         }
-      } else if (specificSetting.isPresent()) {
-        ceTaskMessages.add(new Message("A scanner parameter is defining a new code reference branch but one is already defined in the New Code Period settings."
-          + " Please check your configuration to make sure it is expected.", system2.now()));
+      } else if (branchSpecificSetting.isPresent()) {
+        ceTaskMessages.add(new Message("A scanner parameter is defining a new code reference branch, but this conflicts with the New Code Period"
+          + " setting of your branch. Please check your project configuration. You should use either one or the other but not both.", system2.now()));
       }
 
       Period period = resolver.resolve(dbSession, branchUuid, newCodePeriod, projectVersion);
@@ -115,16 +115,6 @@ public class LoadPeriodsStep implements ComputationStep {
     }
   }
 
-  private static <T> Optional<T> firstPresent(Collection<Supplier<Optional<T>>> suppliers) {
-    for (Supplier<Optional<T>> supplier : suppliers) {
-      Optional<T> result = supplier.get();
-      if (result.isPresent()) {
-        return result;
-      }
-    }
-    return Optional.empty();
-  }
-
   private Optional<NewCodePeriodDto> getBranchSetting(DbSession dbSession, String projectUuid, String branchUuid) {
     return newCodePeriodDao.selectByBranch(dbSession, projectUuid, branchUuid);
   }
index f7170223ef26ce9a58b59da6b59e858828525637..f7a59c5ceb1ef6308b08aa3f266b3f36270ab1d2 100644 (file)
@@ -188,7 +188,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
   }
 
   @Test
-  public void add_analysis_warning_if_scanner_defines_reference_when_branch_setting_also_defines_reference() {
+  public void scanner_overrides_branch_new_code_reference_branch() {
     ComponentDto branch = dbTester.components().insertProjectBranch(project);
     setupRoot(branch);
 
@@ -216,7 +216,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
   }
 
   @Test
-  public void scanner_overrides_new_code_reference_branch() {
+  public void scanner_overrides_project_new_code_reference_branch() {
     ComponentDto branch = dbTester.components().insertProjectBranch(project);
     setupRoot(branch);
 
@@ -227,7 +227,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
 
     underTest.execute(new TestComputationStepContext());
     assertPeriod(NewCodePeriodType.REFERENCE_BRANCH, newCodeReferenceBranch, null);
-    verify(ceTaskMessages).add(any(CeTaskMessages.Message.class));
+    verifyNoInteractions(ceTaskMessages);
   }
 
   @Test