From: Julien HENRY Date: Thu, 25 Jul 2024 09:00:44 +0000 (+0200) Subject: SONAR-22538 Import SARIF related locations as secondary issue locations X-Git-Tag: 10.7.0.96327~310 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a3c94559aae2f372233e07074d6f6dd356d18203;p=sonarqube.git SONAR-22538 Import SARIF related locations as secondary issue locations --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java index c81b1e33313..21e85505172 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java @@ -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 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) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java index 8c53ad8633d..80e75793223 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java @@ -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);