]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20580 fix gitlab synchro failures from crashing the analysis.
authorSteve Marion <steve.marion@sonarsource.com>
Thu, 28 Sep 2023 16:02:03 +0000 (18:02 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 29 Sep 2023 20:02:47 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PreMeasuresComputationCheck.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/PreMeasuresComputationChecksStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/PreMeasuresComputationChecksStepTest.java

index 909514b9d3f76c35a998dcd04ab4a353d5f04205..665d5d306d4c016a6513024a38a0bb7645abfc50 100644 (file)
@@ -36,9 +36,10 @@ import org.sonar.ce.task.projectanalysis.analysis.Branch;
 public interface PreMeasuresComputationCheck {
 
   /**
-   * This method can make the task fail by throwing a {@link RuntimeException}
+   * Throwing a {@link PreMeasuresComputationCheckException} will only produce an analysis warning.
+   * Any other exception will fail the analysis.
    */
-  void onCheck(Context context);
+  void onCheck(Context context) throws PreMeasuresComputationCheckException;
 
   interface Context {
 
@@ -47,4 +48,14 @@ public interface PreMeasuresComputationCheck {
     Branch getBranch();
 
   }
+
+  class PreMeasuresComputationCheckException extends Exception {
+    public PreMeasuresComputationCheckException(String message) {
+      super(message);
+    }
+
+    public PreMeasuresComputationCheckException(String message, Throwable cause) {
+      super(message, cause);
+    }
+  }
 }
index 79257403306788b592a44882d3a78917635a12f8..1c86e564d8786b391de48f53d0a78e1a43798534 100644 (file)
  */
 package org.sonar.ce.task.projectanalysis.measure;
 
-import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.utils.System2;
+import org.sonar.ce.task.log.CeTaskMessages;
 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
 import org.sonar.ce.task.projectanalysis.analysis.Branch;
 import org.sonar.ce.task.step.ComputationStep;
+import org.sonar.db.dismissmessage.MessageType;
+
+import static org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.PreMeasuresComputationCheckException;
 
 /**
  * Execute {@link PreMeasuresComputationCheck} instances in no specific order.
  * If an extension fails (throws an exception), consecutive extensions
  * won't be called.
  */
-@ComputeEngineSide
 public class PreMeasuresComputationChecksStep implements ComputationStep {
 
   private final AnalysisMetadataHolder analysisMetadataHolder;
   private final PreMeasuresComputationCheck[] extensions;
+  private final CeTaskMessages ceTaskMessages;
+
 
-  public PreMeasuresComputationChecksStep(AnalysisMetadataHolder analysisMetadataHolder, PreMeasuresComputationCheck... extensions) {
+  public PreMeasuresComputationChecksStep(AnalysisMetadataHolder analysisMetadataHolder, CeTaskMessages ceTaskMessages, PreMeasuresComputationCheck... extensions) {
     this.analysisMetadataHolder = analysisMetadataHolder;
+    this.ceTaskMessages = ceTaskMessages;
     this.extensions = extensions;
   }
 
@@ -44,7 +50,11 @@ public class PreMeasuresComputationChecksStep implements ComputationStep {
   public void execute(Context context) {
     PreMeasuresComputationCheck.Context extensionContext = new ContextImpl();
     for (PreMeasuresComputationCheck extension : extensions) {
-      extension.onCheck(extensionContext);
+      try {
+        extension.onCheck(extensionContext);
+      } catch (PreMeasuresComputationCheckException pmcce) {
+        ceTaskMessages.add(new CeTaskMessages.Message(pmcce.getMessage(), System2.INSTANCE.now(), MessageType.GENERIC));
+      }
     }
   }
 
index 5f2481025f05c0ebf497c3ccb0dafa3da18f58d4..25ee21b4d3b215a1141f29510d49b35924c91bc4 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.task.projectanalysis.measure;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.mockito.InOrder;
+import org.sonar.ce.task.log.CeTaskMessages;
 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
 import org.sonar.ce.task.projectanalysis.analysis.Branch;
 import org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.Context;
@@ -31,18 +32,21 @@ import org.sonar.server.project.Project;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.PreMeasuresComputationCheckException;
 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
 
 public class PreMeasuresComputationChecksStepTest {
 
-  public AnalysisMetadataHolderRule analysisMetadataHolder = mock(AnalysisMetadataHolderRule.class);
+  public AnalysisMetadataHolderRule analysisMetadataHolder = mock();
+  public CeTaskMessages ceTaskMessages = mock();
 
   @Test
-  public void execute_extensions() {
+  public void execute_extensions() throws PreMeasuresComputationCheckException {
     PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
     PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
 
@@ -54,7 +58,7 @@ public class PreMeasuresComputationChecksStepTest {
   }
 
   @Test
-  public void context_contains_project_uuid_from_analysis_metadata_holder() {
+  public void context_contains_project_uuid_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
     Project project = Project.from(newPrivateProjectDto());
     when(analysisMetadataHolder.getProject()).thenReturn(project);
     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
@@ -67,7 +71,7 @@ public class PreMeasuresComputationChecksStepTest {
   }
 
   @Test
-  public void context_contains_pullRequest_key_from_analysis_metadata_holder() {
+  public void context_contains_pullRequest_key_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
     mockPr("pr1");
     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
 
@@ -79,7 +83,7 @@ public class PreMeasuresComputationChecksStepTest {
   }
 
   @Test
-  public void context_contains_branch_from_analysis_metadata_holder() {
+  public void context_contains_branch_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
     mockBranch("branchName");
     PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
 
@@ -90,6 +94,19 @@ public class PreMeasuresComputationChecksStepTest {
     assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
   }
 
+  @Test
+  public void whenCheckThrows_thenLogCeMessage() throws PreMeasuresComputationCheckException {
+    PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
+    doThrow(new PreMeasuresComputationCheckException("error"))
+      .when(check).onCheck(any());
+
+    newStep(check).execute(new TestComputationStepContext());
+
+    var messageCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
+    verify(ceTaskMessages).add(messageCaptor.capture());
+    assertThat(messageCaptor.getValue().getText()).isEqualTo("error");
+  }
+
   @Test
   public void test_getDescription() {
     assertThat(newStep().getDescription()).isNotEmpty();
@@ -97,9 +114,9 @@ public class PreMeasuresComputationChecksStepTest {
 
   private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
     if (preMeasuresComputationChecks.length == 0) {
-      return new PreMeasuresComputationChecksStep(analysisMetadataHolder);
+      return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages);
     }
-    return new PreMeasuresComputationChecksStep(analysisMetadataHolder, preMeasuresComputationChecks);
+    return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages, preMeasuresComputationChecks);
   }
 
   private void mockBranch(String branchName) {