]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22538 Import SARIF related locations as secondary issue locations
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 25 Jul 2024 09:00:44 +0000 (11:00 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 25 Jul 2024 20:02:51 +0000 (20:02 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java

index c81b1e333131b654f814218f8d8b37e9067b439b..21e855051722de2397817cab78c937bae6dadac0 100644 (file)
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.api.batch.rule.Severity;
 import org.sonar.api.batch.sensor.SensorContext;
@@ -104,6 +105,15 @@ public class ResultMapper {
       NewIssueLocation primaryLocation = fillFileOrProjectLocation(result, newIssueLocation, firstLocation);
       newExternalIssue.at(primaryLocation);
     }
+
+    Set<Location> relatedLocations = result.getRelatedLocations();
+    if (relatedLocations != null && !relatedLocations.isEmpty()) {
+      relatedLocations.forEach(relatedLocation -> {
+        NewIssueLocation newRelatedLocation = newExternalIssue.newLocation();
+        fillFileOrProjectLocation(result, newRelatedLocation, relatedLocation);
+        newExternalIssue.addLocation(newRelatedLocation);
+      });
+    }
   }
 
   private NewIssueLocation fillFileOrProjectLocation(Result result, NewIssueLocation newIssueLocation, Location firstLocation) {
index 8c53ad8633d3f85f48cec3eded94341c6f49a230..80e757932239c93bfa1decd043e212216e2782cd 100644 (file)
@@ -23,6 +23,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider;
 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.util.List;
+import java.util.Set;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -62,7 +63,7 @@ public class ResultMapperTest {
   private SensorContext sensorContext;
 
   @Mock
-  private NewExternalIssue newExternalIssue;
+  private NewExternalIssue mockExternalIssue;
 
   @Mock
   private NewIssueLocation newExternalIssueLocation;
@@ -77,10 +78,10 @@ public class ResultMapperTest {
   public void setUp() {
     MockitoAnnotations.openMocks(this);
     when(result.getRuleId()).thenReturn(RULE_ID);
-    when(sensorContext.newExternalIssue()).thenReturn(newExternalIssue);
+    when(sensorContext.newExternalIssue()).thenReturn(mockExternalIssue);
     when(locationMapper.fillIssueInFileLocation(any(), any(), any())).thenReturn(newExternalIssueLocation);
     when(locationMapper.fillIssueInProjectLocation(any(), any())).thenReturn(newExternalIssueLocation);
-    when(newExternalIssue.newLocation()).thenReturn(newExternalIssueLocation);
+    when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation);
   }
 
   @Test
@@ -115,6 +116,23 @@ public class ResultMapperTest {
     verify(newExternalIssue, never()).addFlow(any());
   }
 
+  @Test
+  public void mapResult_whenRelatedLocationExists_createsSecondaryFileLocation() {
+    Location location = mock(Location.class);
+    when(result.getRelatedLocations()).thenReturn(Set.of(location));
+    var newIssueLocationCall2 = mock(NewIssueLocation.class);
+    when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
+
+    NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
+
+    verify(locationMapper).fillIssueInProjectLocation(result, newExternalIssueLocation);
+    verify(locationMapper).fillIssueInFileLocation(result, newIssueLocationCall2, location);
+    verifyNoMoreInteractions(locationMapper);
+    verify(newExternalIssue).at(newExternalIssueLocation);
+    verify(newExternalIssue).addLocation(newIssueLocationCall2);
+    verify(newExternalIssue, never()).addFlow(any());
+  }
+
   @Test
   public void mapResult_whenLocationExistsButLocationMapperReturnsNull_createsProjectLocation() {
     Location location = mock(Location.class);