Skip to content

Commit 883de3b

Browse files
SLLS-354 forward finding 'isOnNewCode' information to client
1 parent ad9e126 commit 883de3b

File tree

5 files changed

+101
-28
lines changed

5 files changed

+101
-28
lines changed

src/main/java/org/sonarsource/sonarlint/ls/DiagnosticPublisher.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.lsp4j.DiagnosticSeverity;
2828
import org.eclipse.lsp4j.PublishDiagnosticsParams;
2929
import org.sonarsource.sonarlint.core.rpc.protocol.backend.hotspot.HotspotStatus;
30+
import org.sonarsource.sonarlint.core.rpc.protocol.client.hotspot.RaisedHotspotDto;
3031
import org.sonarsource.sonarlint.core.rpc.protocol.common.RuleType;
3132
import org.sonarsource.sonarlint.ls.connected.DelegatingFinding;
3233
import org.sonarsource.sonarlint.ls.connected.DelegatingHotspot;
@@ -102,7 +103,11 @@ public void setFocusOnNewCode(boolean focusOnNewCode) {
102103
public static Diagnostic prepareDiagnostic(DelegatingFinding issue, String entryKey, boolean ignoreSecondaryLocations, boolean focusOnNewCode) {
103104
var diagnostic = new Diagnostic();
104105

105-
setSeverity(diagnostic, issue, focusOnNewCode);
106+
if (issue.getFinding() instanceof RaisedHotspotDto hotspotDto) {
107+
setVulnerabilityProbability(diagnostic, hotspotDto);
108+
} else {
109+
setSeverity(diagnostic, issue, focusOnNewCode);
110+
}
106111
var range = convert(issue);
107112
diagnostic.setRange(range);
108113
diagnostic.setCode(issue.getRuleKey());
@@ -113,6 +118,14 @@ public static Diagnostic prepareDiagnostic(DelegatingFinding issue, String entry
113118
return diagnostic;
114119
}
115120

121+
static void setVulnerabilityProbability(Diagnostic diagnostic, RaisedHotspotDto hotspot) {
122+
switch (hotspot.getVulnerabilityProbability()) {
123+
case MEDIUM -> diagnostic.setSeverity(DiagnosticSeverity.Warning);
124+
case HIGH -> diagnostic.setSeverity(DiagnosticSeverity.Error);
125+
default -> diagnostic.setSeverity(DiagnosticSeverity.Information);
126+
}
127+
}
128+
116129
static void setSeverity(Diagnostic diagnostic, DelegatingFinding issue, boolean focusOnNewCode) {
117130
if (focusOnNewCode) {
118131
var newCodeSeverity = issue.isOnNewCode() ? DiagnosticSeverity.Warning : DiagnosticSeverity.Hint;
@@ -131,6 +144,7 @@ public static class DiagnosticData {
131144
@Nullable
132145
HotspotStatus status;
133146
boolean isAiCodeFixable;
147+
boolean isOnNewCode;
134148

135149
public void setEntryKey(String entryKey) {
136150
this.entryKey = entryKey;
@@ -156,6 +170,14 @@ public String getServerIssueKey() {
156170
public void setAiCodeFixable(boolean aiCodeFixable) {
157171
isAiCodeFixable = aiCodeFixable;
158172
}
173+
174+
public void setOnNewCode(boolean onNewCode) {
175+
isOnNewCode = onNewCode;
176+
}
177+
178+
public boolean isOnNewCode() {
179+
return isOnNewCode;
180+
}
159181
}
160182

161183
public static void setSource(Diagnostic diagnostic, DelegatingFinding issue) {
@@ -177,6 +199,7 @@ private static void setData(Diagnostic diagnostic, DelegatingFinding issue, Stri
177199
data.setStatus(raisedHotspotDto.getReviewStatus());
178200
}
179201
data.setEntryKey(entryKey);
202+
data.setOnNewCode(issue.isOnNewCode());
180203
diagnostic.setData(data);
181204
}
182205

src/main/java/org/sonarsource/sonarlint/ls/connected/TaintVulnerabilitiesCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,16 @@ public Optional<TaintIssue> getTaintVulnerabilityByKey(String issueId) {
7171
public Stream<Diagnostic> getAsDiagnostics(URI fileUri, boolean focusOnNewCode) {
7272
return taintVulnerabilitiesPerFile.getOrDefault(fileUri, emptyList())
7373
.stream()
74-
.flatMap(i -> TaintVulnerabilitiesCache.convert(i, focusOnNewCode).stream());
74+
.flatMap(i -> TaintVulnerabilitiesCache.convert(i).stream());
7575
}
7676

77-
static Optional<Diagnostic> convert(TaintIssue issue, boolean focusOnNewCode) {
77+
static Optional<Diagnostic> convert(TaintIssue issue) {
7878
if (issue.getTextRange() != null) {
7979
var range = TextRangeUtils.convert(issue.getTextRange());
8080
var diagnostic = new Diagnostic();
8181
boolean onNewCode = issue.isOnNewCode();
82-
var severity = (focusOnNewCode && !onNewCode) ? DiagnosticSeverity.Hint : DiagnosticSeverity.Warning;
8382

84-
diagnostic.setSeverity(severity);
83+
diagnostic.setSeverity(DiagnosticSeverity.Error);
8584
diagnostic.setRange(range);
8685
diagnostic.setCode(issue.getRuleKey());
8786
diagnostic.setMessage(message(issue));
@@ -91,6 +90,7 @@ static Optional<Diagnostic> convert(TaintIssue issue, boolean focusOnNewCode) {
9190
diagnosticData.setEntryKey(issue.getId().toString());
9291
diagnosticData.setServerIssueKey(issue.getSonarServerKey());
9392
diagnosticData.setAiCodeFixable(issue.isAiCodeFixable());
93+
diagnosticData.setOnNewCode(onNewCode);
9494
diagnostic.setData(diagnosticData);
9595

9696
return Optional.of(diagnostic);

src/test/java/org/sonarsource/sonarlint/ls/DiagnosticPublisherTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.UUID;
26+
import java.util.stream.Stream;
2627
import org.eclipse.lsp4j.Diagnostic;
2728
import org.eclipse.lsp4j.DiagnosticSeverity;
2829
import org.eclipse.lsp4j.Position;
2930
import org.eclipse.lsp4j.Range;
3031
import org.junit.jupiter.api.BeforeEach;
3132
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.Arguments;
35+
import org.junit.jupiter.params.provider.MethodSource;
36+
import org.sonarsource.sonarlint.core.rpc.protocol.backend.rules.VulnerabilityProbability;
37+
import org.sonarsource.sonarlint.core.rpc.protocol.client.hotspot.RaisedHotspotDto;
3238
import org.sonarsource.sonarlint.core.rpc.protocol.client.issue.RaisedFindingDto;
3339
import org.sonarsource.sonarlint.core.rpc.protocol.common.Either;
3440
import org.sonarsource.sonarlint.core.rpc.protocol.common.IssueSeverity;
@@ -97,6 +103,50 @@ void testNotConvertSeverity() {
97103
assertThat(underTest.issueDtoToDiagnostic(entry(id, issue)).getSeverity()).isEqualTo(DiagnosticSeverity.Warning);
98104
}
99105

106+
@Test
107+
void shouldPrepareDiagnostic() {
108+
var finding = mock(DelegatingFinding.class);
109+
var textRange = new TextRangeDto(1, 0, 1, 1);
110+
when(finding.getTextRange()).thenReturn(textRange);
111+
when(finding.getStartLine()).thenReturn(1);
112+
when(finding.getMessage()).thenReturn("Do this, don't do that");
113+
when(finding.getRuleKey()).thenReturn("rule-key");
114+
when(finding.isOnNewCode()).thenReturn(false);
115+
116+
var diagnostic = DiagnosticPublisher.prepareDiagnostic(finding, "entryKey", false, false);
117+
118+
assertThat(diagnostic.getRange().getStart().getLine()).isZero();
119+
assertThat(diagnostic.getRange().getStart().getCharacter()).isZero();
120+
assertThat(diagnostic.getRange().getEnd().getLine()).isZero();
121+
assertThat(diagnostic.getRange().getEnd().getCharacter()).isEqualTo(1);
122+
assertThat(diagnostic.getMessage()).isEqualTo("Do this, don't do that");
123+
assertThat(diagnostic.getCode().getLeft()).isEqualTo("rule-key");
124+
assertThat(diagnostic.getSeverity()).isEqualTo(DiagnosticSeverity.Warning);
125+
assertThat(diagnostic.getData()).isNotNull();
126+
assertThat(((DiagnosticPublisher.DiagnosticData) diagnostic.getData()).getEntryKey()).isEqualTo("entryKey");
127+
assertThat(((DiagnosticPublisher.DiagnosticData) diagnostic.getData()).isOnNewCode()).isFalse();
128+
}
129+
130+
@ParameterizedTest
131+
@MethodSource("vulnerabilityProbabilityProvider")
132+
void testConvertVulnerabilityProbability(VulnerabilityProbability vulnerabilityProbability, DiagnosticSeverity expectedSeverity) {
133+
var hotspotDto = mock(RaisedHotspotDto.class);
134+
when(hotspotDto.getVulnerabilityProbability()).thenReturn(vulnerabilityProbability);
135+
var diagnostic = new Diagnostic();
136+
137+
DiagnosticPublisher.setVulnerabilityProbability(diagnostic, hotspotDto);
138+
139+
assertThat(diagnostic.getSeverity()).isEqualTo(expectedSeverity);
140+
}
141+
142+
private static Stream<Arguments> vulnerabilityProbabilityProvider() {
143+
return Stream.of(
144+
Arguments.of(VulnerabilityProbability.HIGH, DiagnosticSeverity.Error),
145+
Arguments.of(VulnerabilityProbability.MEDIUM, DiagnosticSeverity.Warning),
146+
Arguments.of(VulnerabilityProbability.LOW, DiagnosticSeverity.Information)
147+
);
148+
}
149+
100150
@Test
101151
void showFirstSecretDetectedNotificationOnlyOnce() {
102152
underTest.initialize(false);

src/test/java/org/sonarsource/sonarlint/ls/connected/TaintVulnerabilitiesCacheTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TaintVulnerabilitiesCacheTests {
5757

5858
@ParameterizedTest
5959
@MethodSource("testIssueConversionParameters")
60-
void testIssueConversion(String taintSource, boolean isOnNewCode, boolean focusOnNewCode, DiagnosticSeverity expectedSeverity) {
60+
void testIssueConversion(String taintSource, boolean isOnNewCode, DiagnosticSeverity expectedSeverity) {
6161
var issue = mock(TaintIssue.class);
6262
var flow = mock(TaintVulnerabilityDto.FlowDto.class);
6363
var loc1 = mock(TaintVulnerabilityDto.FlowDto.LocationDto.class);
@@ -74,7 +74,7 @@ void testIssueConversion(String taintSource, boolean isOnNewCode, boolean focusO
7474
when(issue.getSource()).thenReturn(taintSource);
7575
when(issue.isOnNewCode()).thenReturn(isOnNewCode);
7676

77-
var diagnostic = convert(issue, focusOnNewCode).get();
77+
var diagnostic = convert(issue).get();
7878

7979
assertThat(diagnostic.getMessage()).isEqualTo("message [+2 locations]");
8080
assertThat(diagnostic.getSeverity()).isEqualTo(expectedSeverity);
@@ -90,7 +90,7 @@ void testIssueConversion(String taintSource, boolean isOnNewCode, boolean focusO
9090
void testIssueConversionNoTextRange() {
9191
var issue = mock(TaintIssue.class);
9292

93-
var diagnosticOptional = convert(issue, false);
93+
var diagnosticOptional = convert(issue);
9494

9595
assertThat(diagnosticOptional).isEmpty();
9696
}
@@ -209,15 +209,15 @@ void testRemoveTaintIssue() throws Exception {
209209

210210
private static Stream<Arguments> testIssueConversionParameters() {
211211
return Stream.of(
212-
Arguments.of(SONARCLOUD_TAINT_SOURCE, true, true, DiagnosticSeverity.Warning),
213-
Arguments.of(SONARCLOUD_TAINT_SOURCE, true, false, DiagnosticSeverity.Warning),
214-
Arguments.of(SONARCLOUD_TAINT_SOURCE, false, true, DiagnosticSeverity.Hint),
215-
Arguments.of(SONARCLOUD_TAINT_SOURCE, false, false, DiagnosticSeverity.Warning),
216-
217-
Arguments.of(SONARQUBE_TAINT_SOURCE, true, true, DiagnosticSeverity.Warning),
218-
Arguments.of(SONARQUBE_TAINT_SOURCE, true, false, DiagnosticSeverity.Warning),
219-
Arguments.of(SONARQUBE_TAINT_SOURCE, false, true, DiagnosticSeverity.Hint),
220-
Arguments.of(SONARQUBE_TAINT_SOURCE, false, false, DiagnosticSeverity.Warning)
212+
Arguments.of(SONARCLOUD_TAINT_SOURCE, true, DiagnosticSeverity.Error),
213+
Arguments.of(SONARCLOUD_TAINT_SOURCE, true, DiagnosticSeverity.Error),
214+
Arguments.of(SONARCLOUD_TAINT_SOURCE, false, DiagnosticSeverity.Error),
215+
Arguments.of(SONARCLOUD_TAINT_SOURCE, false, DiagnosticSeverity.Error),
216+
217+
Arguments.of(SONARQUBE_TAINT_SOURCE, true, DiagnosticSeverity.Error),
218+
Arguments.of(SONARQUBE_TAINT_SOURCE, true, DiagnosticSeverity.Error),
219+
Arguments.of(SONARQUBE_TAINT_SOURCE, false, DiagnosticSeverity.Error),
220+
Arguments.of(SONARQUBE_TAINT_SOURCE, false, DiagnosticSeverity.Error)
221221
);
222222
}
223223
}

src/test/java/org/sonarsource/sonarlint/ls/mediumtests/ConnectedModeMediumTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void analysisConnected_find_hotspot() {
302302
Diagnostic::getSeverity)
303303
.containsExactly(
304304
tuple(0, 13, 0, 26, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.",
305-
DiagnosticSeverity.Warning)));
305+
DiagnosticSeverity.Information)));
306306
}
307307

308308
@Test
@@ -370,7 +370,7 @@ void analysisConnected_find_tracked_hotspot_before_sq_10_1() {
370370
Diagnostic::getSeverity)
371371
.containsExactly(
372372
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.",
373-
DiagnosticSeverity.Warning)));
373+
DiagnosticSeverity.Information)));
374374
}
375375

376376
@Test
@@ -448,7 +448,7 @@ void analysisConnected_find_tracked_hotspot_after_sq_10_1() {
448448
Diagnostic::getSeverity)
449449
.containsExactly(
450450
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.",
451-
DiagnosticSeverity.Warning)));
451+
DiagnosticSeverity.Information)));
452452
}
453453

454454
@Test
@@ -500,15 +500,15 @@ void analysisConnected_scan_all_hotspot_then_forget() throws IOException {
500500
Diagnostic::getSeverity)
501501
.containsExactly(
502502
tuple(1, 15, 1, 28, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.",
503-
DiagnosticSeverity.Warning));
503+
DiagnosticSeverity.Information));
504504
assertThat(client.getDiagnostics(uri1InFolder)).isEmpty();
505505

506506
assertThat(client.getHotspots(uri2InFolder))
507507
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage,
508508
Diagnostic::getSeverity)
509509
.containsExactly(
510510
tuple(1, 15, 1, 28, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"23.45.67.89\" is safe here.",
511-
DiagnosticSeverity.Warning));
511+
DiagnosticSeverity.Information));
512512
assertThat(client.getDiagnostics(uri2InFolder)).isEmpty();
513513
});
514514

@@ -526,7 +526,7 @@ void analysisConnected_scan_all_hotspot_then_forget() throws IOException {
526526
Diagnostic::getSeverity)
527527
.containsExactly(
528528
tuple(1, 15, 1, 28, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.",
529-
DiagnosticSeverity.Warning));
529+
DiagnosticSeverity.Information));
530530

531531
// File 2 is not open, cleaning hotspots
532532
assertThat(client.getHotspots(uri2InFolder)).isEmpty();
@@ -972,7 +972,7 @@ void change_hotspot_status_to_resolved() {
972972
awaitUntilAsserted(() -> assertThat(client.getHotspots(uriInFolder))
973973
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage, Diagnostic::getSeverity)
974974
.containsExactly(
975-
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Warning)));
975+
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Information)));
976976
assertThat(client.getHotspots(uriInFolder).get(0).getData().toString()).contains("\"status\":0");
977977

978978
lsProxy.changeHotspotStatus(new SonarLintExtendedLanguageServer.ChangeHotspotStatusParams(hotspotKey, HotspotStatus.SAFE.name(), uriInFolder));
@@ -1054,7 +1054,7 @@ void should_not_change_hotspot_status_to_resolved() {
10541054
awaitUntilAsserted(() -> assertThat(client.getHotspots(uriInFolder))
10551055
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage, Diagnostic::getSeverity)
10561056
.containsExactly(
1057-
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Warning)));
1057+
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Information)));
10581058
assertThat(client.getHotspots(uriInFolder).get(0).getData().toString()).contains("\"status\":0");
10591059

10601060
lsProxy.changeHotspotStatus(new SonarLintExtendedLanguageServer.ChangeHotspotStatusParams(hotspotKey, HotspotStatus.SAFE.name(), uriInFolder));
@@ -1148,7 +1148,7 @@ void change_hotspot_status_permission_check() throws ExecutionException, Interru
11481148
awaitUntilAsserted(() -> assertThat(client.getHotspots(uriInFolder))
11491149
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage, Diagnostic::getSeverity)
11501150
.containsExactly(
1151-
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Warning)));
1151+
tuple(0, 13, 0, 26, PYTHON_S1313, "remote", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Information)));
11521152

11531153

11541154
var response = lsProxy.getAllowedHotspotStatuses(
@@ -1174,7 +1174,7 @@ void change_hotspot_status_permission_check_fail() throws ExecutionException, In
11741174
awaitUntilAsserted(() -> assertThat(client.getHotspots(uriInFolder))
11751175
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage, Diagnostic::getSeverity)
11761176
.containsExactly(
1177-
tuple(0, 13, 0, 26, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Warning)));
1177+
tuple(0, 13, 0, 26, PYTHON_S1313, "sonarqube", "Make sure using this hardcoded IP address \"12.34.56.78\" is safe here.", DiagnosticSeverity.Information)));
11781178

11791179
lsProxy.getAllowedHotspotStatuses(
11801180
new SonarLintExtendedLanguageServer.GetAllowedHotspotStatusesParams(hotspotKey, folder1BaseDir.toUri().toString(), uriInFolder)).get();
@@ -1443,7 +1443,7 @@ void shouldReportTaintIssues() {
14431443
awaitUntilAsserted(() -> assertThat(client.getTaints(fileUri))
14441444
.extracting(startLine(), startCharacter(), endLine(), endCharacter(), code(), Diagnostic::getSource, Diagnostic::getMessage,
14451445
Diagnostic::getSeverity)
1446-
.contains(tuple(0, 1, 0, 2, "ruleKey", "Latest SonarQube Server Analysis", "message", DiagnosticSeverity.Warning)));
1446+
.contains(tuple(0, 1, 0, 2, "ruleKey", "Latest SonarQube Server Analysis", "message", DiagnosticSeverity.Error)));
14471447
}
14481448

14491449
@Test

0 commit comments

Comments
 (0)