Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLCORE-800 Clean up deprecated classes related to analysis #1108

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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 @@ -686,7 +685,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.AnalyzeOpenFilesParams;
Expand Down Expand Up @@ -124,17 +123,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
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesAndTrackParams;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.config.binding.BindingSuggestionDto;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TaintVulnerabilityDto;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.RawIssueDto;
import org.sonarsource.sonarlint.core.rpc.protocol.client.binding.AssistBindingParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.binding.AssistBindingResponse;
import org.sonarsource.sonarlint.core.rpc.protocol.client.binding.NoBindingSuggestionFoundParams;
Expand Down Expand Up @@ -188,14 +186,6 @@ default Path getBaseDir(String configurationScopeId) throws ConfigScopeNotFoundE

void didChangeAnalysisReadiness(Set<String> configurationScopeIds, boolean areReadyForAnalysis);

/**
* @deprecated since 10.2, please implement raiseIssues and raiseHotspots instead.
* See {@link org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalysisRpcService#analyzeFilesAndTrack(AnalyzeFilesAndTrackParams)}
*/
@Deprecated(since = "10.2")
default void didRaiseIssue(String configurationScopeId, UUID analysisId, RawIssueDto rawIssue) {
}

default void raiseIssues(String configurationScopeId, Map<URI, List<RaisedIssueDto>> issuesByFileUri, boolean isIntermediatePublication, @Nullable UUID analysisId) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.sonarsource.sonarlint.core.rpc.protocol.client.OpenUrlInBrowserParams;
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.GetFileExclusionsParams;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.GetFileExclusionsResponse;
import org.sonarsource.sonarlint.core.rpc.protocol.client.analysis.GetInferredAnalysisPropertiesParams;
Expand Down Expand Up @@ -372,11 +371,6 @@ public void didChangeAnalysisReadiness(DidChangeAnalysisReadinessParams params)
notify(() -> delegate.didChangeAnalysisReadiness(params.getConfigurationScopeIds(), params.areReadyForAnalysis()));
}

@Override
public void didRaiseIssue(DidRaiseIssueParams params) {
notify(() -> delegate.didRaiseIssue(params.getConfigurationScopeId(), params.getAnalysisId(), params.getRawIssue()));
}

@Override
public void raiseIssues(RaiseIssuesParams params) {
notify(() -> delegate.raiseIssues(params.getConfigurationScopeId(), params.getIssuesByFileUri(), params.isIntermediatePublication(), params.getAnalysisId()));
Expand Down
Loading