aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2024-07-25 11:00:44 +0200
committersonartech <sonartech@sonarsource.com>2024-07-25 20:02:51 +0000
commita3c94559aae2f372233e07074d6f6dd356d18203 (patch)
tree3b888dd46546e95ba2ec26df52fd5c0b8b5488c3 /sonar-scanner-engine
parent89c2d1a6426234d4baa8b9a0e01e45342a3e35d8 (diff)
downloadsonarqube-a3c94559aae2f372233e07074d6f6dd356d18203.tar.gz
sonarqube-a3c94559aae2f372233e07074d6f6dd356d18203.zip
SONAR-22538 Import SARIF related locations as secondary issue locations
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/ResultMapper.java10
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/sarif/ResultMapperTest.java24
2 files changed, 31 insertions, 3 deletions
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<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) {
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
@@ -116,6 +117,23 @@ public class ResultMapperTest {
}
@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);
when(result.getLocations()).thenReturn(List.of(location));