Skip to content

Commit

Permalink
SLCORE-800 Clean up deprecated classes related to analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Oct 21, 2024
1 parent 9a18d1a commit 6916138
Show file tree
Hide file tree
Showing 72 changed files with 1,828 additions and 4,749 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ eks_container: &CONTAINER_DEFINITION
builder_subnet_id: ${CIRRUS_AWS_SUBNET}
namespace: default
cpu: 4
memory: 4G
memory: 6G

ec2_instance: &WINVM_DEFINITION
experimental: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
import org.sonarsource.sonarlint.core.rpc.protocol.backend.initialize.InitializeParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.DidChangeAnalysisReadinessParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.DidDetectSecretParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.DidRaiseIssueParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.FileEditDto;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.GetInferredAnalysisPropertiesParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.QuickFixDto;
Expand Down Expand Up @@ -693,7 +692,6 @@ private void streamIssue(String configScopeId, UUID analysisId, Issue issue, Con
if (activeRule != null) {
var rawIssue = new RawIssue(issue, activeRule);
rawIssues.add(rawIssue);
client.didRaiseIssue(new DidRaiseIssueParams(configScopeId, analysisId, toDto(issue, activeRule)));
if (ruleKey.contains("secrets")) {
client.didDetectSecret(new DidDetectSecretParams(configScopeId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,143 +20,41 @@
package org.sonarsource.sonarlint.core.tracking;

import com.google.common.util.concurrent.MoreExecutors;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import javax.inject.Singleton;
import org.sonarsource.sonarlint.core.branch.SonarProjectBranchTrackingService;
import org.sonarsource.sonarlint.core.commons.Binding;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
import org.sonarsource.sonarlint.core.event.SonarServerEventReceivedEvent;
import org.sonarsource.sonarlint.core.file.FilePathTranslation;
import org.sonarsource.sonarlint.core.file.PathTranslationService;
import org.sonarsource.sonarlint.core.reporting.FindingReportingService;
import org.sonarsource.sonarlint.core.repository.config.ConfigurationRepository;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.hotspot.HotspotStatus;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.ClientTrackedFindingDto;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.LocalOnlySecurityHotspotDto;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.ServerMatchedSecurityHotspotDto;
import org.sonarsource.sonarlint.core.rpc.protocol.client.hotspot.RaisedHotspotDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.Either;
import org.sonarsource.sonarlint.core.serverapi.hotspot.ServerHotspot;
import org.sonarsource.sonarlint.core.serverapi.push.SecurityHotspotChangedEvent;
import org.sonarsource.sonarlint.core.serverapi.push.SecurityHotspotClosedEvent;
import org.sonarsource.sonarlint.core.serverapi.push.SecurityHotspotRaisedEvent;
import org.sonarsource.sonarlint.core.storage.StorageService;
import org.sonarsource.sonarlint.core.sync.HotspotSynchronizationService;
import org.sonarsource.sonarlint.core.tracking.matching.ClientTrackedFindingMatchingAttributeMapper;
import org.sonarsource.sonarlint.core.tracking.matching.IssueMatcher;
import org.sonarsource.sonarlint.core.tracking.matching.ServerHotspotMatchingAttributesMapper;
import org.springframework.context.event.EventListener;

import static java.util.stream.Collectors.toMap;

@Named
@Singleton
public class SecurityHotspotMatchingService {
private static final int FETCH_ALL_SECURITY_HOTSPOTS_THRESHOLD = 10;
private static final SonarLintLogger LOG = SonarLintLogger.get();
private final ConfigurationRepository configurationRepository;
private final StorageService storageService;
private final SonarProjectBranchTrackingService branchTrackingService;
private final HotspotSynchronizationService hotspotSynchronizationService;
private final PathTranslationService pathTranslationService;
private final FindingReportingService findingReportingService;
private final ExecutorService executorService;

public SecurityHotspotMatchingService(ConfigurationRepository configurationRepository, StorageService storageService,
SonarProjectBranchTrackingService branchTrackingService, HotspotSynchronizationService hotspotSynchronizationService,
PathTranslationService pathTranslationService, FindingReportingService findingReportingService) {
public SecurityHotspotMatchingService(ConfigurationRepository configurationRepository, StorageService storageService, FindingReportingService findingReportingService) {
this.configurationRepository = configurationRepository;
this.storageService = storageService;
this.branchTrackingService = branchTrackingService;
this.hotspotSynchronizationService = hotspotSynchronizationService;
this.pathTranslationService = pathTranslationService;
this.findingReportingService = findingReportingService;
this.executorService = Executors.newSingleThreadExecutor(r -> new Thread(r, "sonarlint-server-tracking-hotspot-updater"));
}

public Map<Path, List<Either<ServerMatchedSecurityHotspotDto, LocalOnlySecurityHotspotDto>>> matchWithServerSecurityHotspots(String configurationScopeId,
Map<Path, List<ClientTrackedFindingDto>> clientTrackedHotspotsByIdeRelativePath, boolean shouldFetchHotspotsFromServer, SonarLintCancelMonitor cancelMonitor) {
var effectiveBindingOpt = configurationRepository.getEffectiveBinding(configurationScopeId);
var activeBranchOpt = branchTrackingService.awaitEffectiveSonarProjectBranch(configurationScopeId);
var translationOpt = pathTranslationService.getOrComputePathTranslation(configurationScopeId);
if (effectiveBindingOpt.isEmpty() || activeBranchOpt.isEmpty() || translationOpt.isEmpty()) {
return clientTrackedHotspotsByIdeRelativePath.entrySet().stream()
.map(e -> Map.entry(e.getKey(), e.getValue().stream()
.map(issue -> Either.<ServerMatchedSecurityHotspotDto, LocalOnlySecurityHotspotDto>forRight(
new LocalOnlySecurityHotspotDto(UUID.randomUUID())))
.collect(Collectors.toList())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
var binding = effectiveBindingOpt.get();
var activeBranch = activeBranchOpt.get();
if (shouldFetchHotspotsFromServer) {
refreshServerSecurityHotspots(cancelMonitor, binding, activeBranch, clientTrackedHotspotsByIdeRelativePath, translationOpt.get());
}
var newCodeDefinition = storageService.binding(binding).newCodeDefinition().read();
return clientTrackedHotspotsByIdeRelativePath.entrySet().stream().map(e -> {
var serverRelativePath = e.getKey();
var serverHotspots = storageService.binding(binding).findings().loadHotspots(activeBranch, serverRelativePath);
var matches = matchSecurityHotspots(serverHotspots, e.getValue())
.stream().map(result -> {
if (result.isLeft()) {
var serverSecurityHotspot = result.getLeft();
var creationDate = serverSecurityHotspot.getCreationDate();
var isOnNewCode = newCodeDefinition.map(definition -> definition.isOnNewCode(creationDate.toEpochMilli())).orElse(true);
return Either.<ServerMatchedSecurityHotspotDto, LocalOnlySecurityHotspotDto>forLeft(
new ServerMatchedSecurityHotspotDto(UUID.randomUUID(), serverSecurityHotspot.getKey(), creationDate.toEpochMilli(),
HotspotStatus.valueOf(serverSecurityHotspot.getStatus().name()), isOnNewCode));
} else {
return Either.<ServerMatchedSecurityHotspotDto, LocalOnlySecurityHotspotDto>forRight(new LocalOnlySecurityHotspotDto(result.getRight().getId()));
}
}).collect(Collectors.toList());
return Map.entry(serverRelativePath, matches);
}).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private void refreshServerSecurityHotspots(SonarLintCancelMonitor cancelMonitor, Binding binding, String activeBranch,
Map<Path, List<ClientTrackedFindingDto>> clientTrackedHotspotsByIdeRelativePath, FilePathTranslation translation) {
var serverFileRelativePaths = clientTrackedHotspotsByIdeRelativePath.keySet()
.stream().map(translation::ideToServerPath).collect(Collectors.toSet());
var downloadAllSecurityHotspotsAtOnce = serverFileRelativePaths.size() > FETCH_ALL_SECURITY_HOTSPOTS_THRESHOLD;
var fetchTasks = new LinkedList<CompletableFuture<?>>();
if (downloadAllSecurityHotspotsAtOnce) {
fetchTasks.add(CompletableFuture.runAsync(() -> hotspotSynchronizationService.fetchProjectHotspots(binding, activeBranch, cancelMonitor), executorService));
} else {
fetchTasks.addAll(serverFileRelativePaths.stream()
.map(serverFileRelativePath -> CompletableFuture
.runAsync(() -> hotspotSynchronizationService.fetchFileHotspots(binding, activeBranch, serverFileRelativePath, cancelMonitor), executorService))
.collect(Collectors.toList()));
}
CompletableFuture.allOf(fetchTasks.toArray(new CompletableFuture[0])).join();
}

private static List<Either<ServerHotspot, LocalOnlySecurityHotspot>> matchSecurityHotspots(Collection<ServerHotspot> serverHotspots,
List<ClientTrackedFindingDto> clientTrackedHotspots) {
var matcher = new IssueMatcher<>(new ClientTrackedFindingMatchingAttributeMapper(), new ServerHotspotMatchingAttributesMapper());
var matchingResult = matcher.match(clientTrackedHotspots, serverHotspots);
return clientTrackedHotspots.stream().<Either<ServerHotspot, LocalOnlySecurityHotspot>>map(clientTrackedHotspot -> {
var match = matchingResult.getMatch(clientTrackedHotspot);
if (match != null) {
return Either.forLeft(match);
} else {
return Either.forRight(new LocalOnlySecurityHotspot(UUID.randomUUID()));
}
}).collect(Collectors.toList());
}

@EventListener
public void onServerEventReceived(SonarServerEventReceivedEvent event) {
var connectionId = event.getConnectionId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalysisRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFileListParams;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesAndTrackParams;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesParams;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesResponse;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFullProjectParams;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.DidChangePathToCompileCommandsParams;
Expand Down Expand Up @@ -122,17 +121,6 @@ public CompletableFuture<GetAutoDetectedNodeJsResponse> getAutoDetectedNodeJs()
});
}

@Override
public CompletableFuture<AnalyzeFilesResponse> analyzeFiles(AnalyzeFilesParams params) {
var configurationScopeId = params.getConfigurationScopeId();
return requestAsync(cancelChecker -> {
var analysisResults = getBean(AnalysisService.class)
.analyze(cancelChecker, params.getConfigurationScopeId(), params.getAnalysisId(), params.getFilesToAnalyze(),
params.getExtraProperties(), params.getStartTime(), false, false, false).join();
return generateAnalyzeFilesResponse(analysisResults);
}, configurationScopeId);
}

@Override
public CompletableFuture<AnalyzeFilesResponse> analyzeFilesAndTrack(AnalyzeFilesAndTrackParams params) {
var configurationScopeId = params.getConfigurationScopeId();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
import org.sonarsource.sonarlint.core.rpc.protocol.backend.newcode.NewCodeRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.rules.RulesRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.telemetry.TelemetryRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.IssueTrackingRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.SecurityHotspotMatchingRpcService;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TaintVulnerabilityTrackingRpcService;
import org.sonarsource.sonarlint.core.spring.SpringApplicationContextInitializer;
import org.sonarsource.sonarlint.core.storage.StorageService;
Expand Down Expand Up @@ -215,16 +213,6 @@ public IssueRpcService getIssueService() {
return new IssueRpcServiceDelegate(this);
}

@Override
public IssueTrackingRpcService getIssueTrackingService() {
return new IssueTrackingRpcServiceDelegate(this);
}

@Override
public SecurityHotspotMatchingRpcService getSecurityHotspotMatchingService() {
return new SecurityHotspotMatchingRpcServiceDelegate(this);
}

@Override
public NewCodeRpcService getNewCodeService() {
return new NewCodeRpcServiceDelegate(this);
Expand Down
Loading

0 comments on commit 6916138

Please sign in to comment.