Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Jan 20, 2025
1 parent bffb584 commit 5e46ecd
Show file tree
Hide file tree
Showing 27 changed files with 180 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.sonarsource.sonarlint.core;

import com.google.common.annotations.VisibleForTesting;
import java.net.URI;
import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -30,7 +29,6 @@
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
import org.sonarsource.sonarlint.core.connection.ConnectionManager;
import org.sonarsource.sonarlint.core.connection.ServerConnection;
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
import org.sonarsource.sonarlint.core.http.ConnectionAwareHttpClientProvider;
Expand All @@ -53,7 +51,7 @@

@Named
@Singleton
public class ServerApiProvider implements ConnectionManager {
public class ConnectionManager {

private static final SonarLintLogger LOG = SonarLintLogger.get();
private final ConnectionConfigurationRepository connectionRepository;
Expand All @@ -62,8 +60,8 @@ public class ServerApiProvider implements ConnectionManager {
private final SonarLintRpcClient client;
private final URI sonarCloudUri;

public ServerApiProvider(ConnectionConfigurationRepository connectionRepository, ConnectionAwareHttpClientProvider awareHttpClientProvider, HttpClientProvider httpClientProvider,
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, SonarLintRpcClient client) {
public ConnectionManager(ConnectionConfigurationRepository connectionRepository, ConnectionAwareHttpClientProvider awareHttpClientProvider, HttpClientProvider httpClientProvider,
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, SonarLintRpcClient client) {
this.connectionRepository = connectionRepository;
this.awareHttpClientProvider = awareHttpClientProvider;
this.httpClientProvider = httpClientProvider;
Expand Down Expand Up @@ -145,7 +143,6 @@ private HttpClient getClientFor(EndpointParams params, Either<TokenDto, Username
userPass -> httpClientProvider.getHttpClientWithPreemptiveAuth(userPass.getUsername(), userPass.getPassword()));
}

@Override
public ServerConnection getConnectionOrThrow(String connectionId) {
var serverApi = getServerApiOrThrow(connectionId);
return new ServerConnection(connectionId, serverApi, client);
Expand All @@ -161,28 +158,27 @@ public Optional<ServerConnection> tryGetConnectionWithoutCredentials(String conn
.map(serverApi -> new ServerConnection(connectionId, serverApi, client));
}

@Override
public ServerApi getTransientConnection(String token,@Nullable String organization, String baseUrl) {
return getServerApi(baseUrl, organization, token);
}

@Override
public void withValidConnection(String connectionId, Consumer<ServerApi> serverApiConsumer) {
getValidConnection(connectionId).ifPresent(connection -> connection.withClientApi(serverApiConsumer));
}

@Override
public <T> Optional<T> withValidConnectionAndReturn(String connectionId, Function<ServerApi, T> serverApiConsumer) {
return getValidConnection(connectionId).map(connection -> connection.withClientApiAndReturn(serverApiConsumer));
}

@Override
public <T> Optional<T> withValidConnectionFlatMapOptionalAndReturn(String connectionId, Function<ServerApi, Optional<T>> serverApiConsumer) {
return getValidConnection(connectionId).map(connection -> connection.withClientApiAndReturn(serverApiConsumer)).flatMap(Function.identity());
}

@VisibleForTesting
public Optional<ServerConnection> getValidConnection(String connectionId) {
return tryGetConnection(connectionId).filter(ServerConnection::isValid);
private Optional<ServerConnection> getValidConnection(String connectionId) {
return tryGetConnection(connectionId).filter(ServerConnection::isValid)
.or(() -> {
LOG.debug("Connection '{}' is invalid", connectionId);
return Optional.empty();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ public class ConnectionService {
private final ApplicationEventPublisher applicationEventPublisher;
private final ConnectionConfigurationRepository repository;
private final URI sonarCloudUri;
private final ServerApiProvider serverApiProvider;
private final ConnectionManager connectionManager;
private final TokenGeneratorHelper tokenGeneratorHelper;

@Inject
public ConnectionService(ApplicationEventPublisher applicationEventPublisher, ConnectionConfigurationRepository repository, InitializeParams params,
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, TokenGeneratorHelper tokenGeneratorHelper, ServerApiProvider serverApiProvider) {
this(applicationEventPublisher, repository, params.getSonarQubeConnections(), params.getSonarCloudConnections(), sonarCloudActiveEnvironment, serverApiProvider,
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, TokenGeneratorHelper tokenGeneratorHelper, ConnectionManager connectionManager) {
this(applicationEventPublisher, repository, params.getSonarQubeConnections(), params.getSonarCloudConnections(), sonarCloudActiveEnvironment, connectionManager,
tokenGeneratorHelper);
}

ConnectionService(ApplicationEventPublisher applicationEventPublisher, ConnectionConfigurationRepository repository,
@Nullable List<SonarQubeConnectionConfigurationDto> initSonarQubeConnections, @Nullable List<SonarCloudConnectionConfigurationDto> initSonarCloudConnections,
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, ServerApiProvider serverApiProvider, TokenGeneratorHelper tokenGeneratorHelper) {
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, ConnectionManager connectionManager, TokenGeneratorHelper tokenGeneratorHelper) {
this.applicationEventPublisher = applicationEventPublisher;
this.repository = repository;
this.sonarCloudUri = sonarCloudActiveEnvironment.getUri();
this.serverApiProvider = serverApiProvider;
this.connectionManager = connectionManager;
this.tokenGeneratorHelper = tokenGeneratorHelper;
if (initSonarQubeConnections != null) {
initSonarQubeConnections.forEach(c -> repository.addOrReplace(adapt(c)));
Expand Down Expand Up @@ -157,7 +157,7 @@ private void updateConnection(AbstractConnectionConfiguration connectionConfigur

public ValidateConnectionResponse validateConnection(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
SonarLintCancelMonitor cancelMonitor) {
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
var serverApi = connectionManager.getForTransientConnection(transientConnection);
var serverChecker = new ServerVersionAndStatusChecker(serverApi);
try {
serverChecker.checkVersionAndStatus(cancelMonitor);
Expand All @@ -179,7 +179,7 @@ public ValidateConnectionResponse validateConnection(Either<TransientSonarQubeCo

public boolean checkSmartNotificationsSupported(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
SonarLintCancelMonitor cancelMonitor) {
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
var serverApi = connectionManager.getForTransientConnection(transientConnection);
var developersApi = serverApi.developers();
return developersApi.isSupported(cancelMonitor);
}
Expand All @@ -189,15 +189,15 @@ public HelpGenerateUserTokenResponse helpGenerateUserToken(String serverUrl, Son
}

public List<SonarProjectDto> getAllProjects(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection, SonarLintCancelMonitor cancelMonitor) {
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
var serverApi = connectionManager.getForTransientConnection(transientConnection);
return serverApi.component().getAllProjects(cancelMonitor)
.stream().map(serverProject -> new SonarProjectDto(serverProject.getKey(), serverProject.getName()))
.collect(Collectors.toList());
}

public Map<String, String> getProjectNamesByKey(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
List<String> projectKeys, SonarLintCancelMonitor cancelMonitor) {
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
var serverApi = connectionManager.getForTransientConnection(transientConnection);
var projectNamesByKey = new HashMap<String, String>();
projectKeys.forEach(key -> {
var projectName = serverApi.component().getProject(key, cancelMonitor).map(ServerProject::getName).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
public class OrganizationsCache {

private static final SonarLintLogger LOG = SonarLintLogger.get();
private final ServerApiProvider serverApiProvider;
private final ConnectionManager connectionManager;

private final Cache<Either<TokenDto, UsernamePasswordDto>, TextSearchIndex<OrganizationDto>> textSearchIndexCacheByCredentials = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.build();

public OrganizationsCache(ServerApiProvider serverApiProvider) {
this.serverApiProvider = serverApiProvider;
public OrganizationsCache(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}

public List<OrganizationDto> fuzzySearchOrganizations(Either<TokenDto, UsernamePasswordDto> credentials, String searchText, SonarLintCancelMonitor cancelMonitor) {
Expand All @@ -74,7 +74,7 @@ public TextSearchIndex<OrganizationDto> getTextSearchIndex(Either<TokenDto, User
LOG.debug("Load user organizations...");
List<OrganizationDto> orgs;
try {
var serverApi = serverApiProvider.getForSonarCloudNoOrg(credentials);
var serverApi = connectionManager.getForSonarCloudNoOrg(credentials);
var serverOrganizations = serverApi.organization().listUserOrganizations(cancelMonitor);
orgs = serverOrganizations.stream().map(o -> new OrganizationDto(o.getKey(), o.getName(), o.getDescription())).collect(Collectors.toList());
} catch (Exception e) {
Expand Down Expand Up @@ -103,7 +103,7 @@ public List<OrganizationDto> listUserOrganizations(Either<TokenDto, UsernamePass

@CheckForNull
public OrganizationDto getOrganization(Either<TokenDto, UsernamePasswordDto> credentials, String organizationKey, SonarLintCancelMonitor cancelMonitor) {
var helper = serverApiProvider.getForSonarCloudNoOrg(credentials);
var helper = connectionManager.getForSonarCloudNoOrg(credentials);
var serverOrganization = helper.organization().getOrganization(organizationKey, cancelMonitor);
return serverOrganization.map(o -> new OrganizationDto(o.getKey(), o.getName(), o.getDescription())).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public class SonarProjectsCache {

private static final SonarLintLogger LOG = SonarLintLogger.get();
private final ServerApiProvider serverApiProvider;
private final ConnectionManager connectionManager;

private final Cache<String, TextSearchIndex<ServerProject>> textSearchIndexCacheByConnectionId = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
Expand Down Expand Up @@ -94,8 +94,8 @@ public int hashCode() {
}
}

public SonarProjectsCache(ServerApiProvider serverApiProvider) {
this.serverApiProvider = serverApiProvider;
public SonarProjectsCache(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}

@EventListener
Expand All @@ -120,7 +120,7 @@ public Optional<ServerProject> getSonarProject(String connectionId, String sonar
return singleProjectsCache.get(new SonarProjectKey(connectionId, sonarProjectKey), () -> {
LOG.debug("Query project '{}' on connection '{}'...", sonarProjectKey, connectionId);
try {
return serverApiProvider.withValidConnectionAndReturn(connectionId,
return connectionManager.withValidConnectionAndReturn(connectionId,
s -> s.component().getProject(sonarProjectKey, cancelMonitor)).orElse(Optional.empty());
} catch (Exception e) {
LOG.error("Error while querying project '{}' from connection '{}'", sonarProjectKey, connectionId, e);
Expand All @@ -138,7 +138,7 @@ public TextSearchIndex<ServerProject> getTextSearchIndex(String connectionId, So
LOG.debug("Load projects from connection '{}'...", connectionId);
List<ServerProject> projects;
try {
projects = serverApiProvider.withValidConnectionAndReturn(connectionId,
projects = connectionManager.withValidConnectionAndReturn(connectionId,
s -> s.component().getAllProjects(cancelMonitor))
.orElse(List.of());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public class VersionSoonUnsupportedHelper {
private final SonarLintRpcClient client;
private final ConfigurationRepository configRepository;
private final ConnectionConfigurationRepository connectionRepository;
private final ServerApiProvider serverApiProvider;
private final ConnectionManager connectionManager;
private final SynchronizationService synchronizationService;
private final Map<String, Version> cacheConnectionIdPerVersion = new ConcurrentHashMap<>();
private final ExecutorServiceShutdownWatchable<?> executorService;

public VersionSoonUnsupportedHelper(SonarLintRpcClient client, ConfigurationRepository configRepository, ServerApiProvider serverApiProvider,
public VersionSoonUnsupportedHelper(SonarLintRpcClient client, ConfigurationRepository configRepository, ConnectionManager connectionManager,
ConnectionConfigurationRepository connectionRepository, SynchronizationService synchronizationService) {
this.client = client;
this.configRepository = configRepository;
this.connectionRepository = connectionRepository;
this.serverApiProvider = serverApiProvider;
this.connectionManager = connectionManager;
this.synchronizationService = synchronizationService;
this.executorService = new ExecutorServiceShutdownWatchable<>(new ThreadPoolExecutor(0, 1, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), r -> new Thread(r, "Version Soon Unsupported Helper")));
Expand Down Expand Up @@ -107,7 +107,7 @@ private void queueCheckIfSoonUnsupported(String connectionId, String configScope
try {
var connection = connectionRepository.getConnectionById(connectionId);
if (connection != null && connection.getKind() == ConnectionKind.SONARQUBE) {
serverApiProvider.tryGetConnectionWithoutCredentials(connectionId)
connectionManager.tryGetConnectionWithoutCredentials(connectionId)
.ifPresent(serverConnection -> serverConnection.withClientApi(serverApi -> {
var version = synchronizationService.readOrSynchronizeServerVersion(connectionId, serverApi, cancelMonitor);
var isCached = cacheConnectionIdPerVersion.containsKey(connectionId) && cacheConnectionIdPerVersion.get(connectionId).compareTo(version) == 0;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.net.URIBuilder;
import org.sonarsource.sonarlint.core.ServerApiProvider;
import org.sonarsource.sonarlint.core.ConnectionManager;
import org.sonarsource.sonarlint.core.commons.api.TextRange;
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
import org.sonarsource.sonarlint.core.file.FilePathTranslation;
Expand All @@ -57,15 +57,15 @@
@Singleton
public class ShowHotspotRequestHandler implements HttpRequestHandler {
private final SonarLintRpcClient client;
private final ServerApiProvider serverApiProvider;
private final ConnectionManager connectionManager;
private final TelemetryService telemetryService;
private final RequestHandlerBindingAssistant requestHandlerBindingAssistant;
private final PathTranslationService pathTranslationService;

public ShowHotspotRequestHandler(SonarLintRpcClient client, ServerApiProvider serverApiProvider, TelemetryService telemetryService,
public ShowHotspotRequestHandler(SonarLintRpcClient client, ConnectionManager connectionManager, TelemetryService telemetryService,
RequestHandlerBindingAssistant requestHandlerBindingAssistant, PathTranslationService pathTranslationService) {
this.client = client;
this.serverApiProvider = serverApiProvider;
this.connectionManager = connectionManager;
this.telemetryService = telemetryService;
this.requestHandlerBindingAssistant = requestHandlerBindingAssistant;
this.pathTranslationService = pathTranslationService;
Expand Down Expand Up @@ -105,7 +105,7 @@ private void showHotspotForScope(String connectionId, String configurationScopeI
}

private Optional<ServerHotspotDetails> tryFetchHotspot(String connectionId, String hotspotKey, SonarLintCancelMonitor cancelMonitor) {
return serverApiProvider.withValidConnectionFlatMapOptionalAndReturn(connectionId,api -> api.hotspot().fetch(hotspotKey, cancelMonitor));
return connectionManager.withValidConnectionFlatMapOptionalAndReturn(connectionId, api -> api.hotspot().fetch(hotspotKey, cancelMonitor));
}

private static HotspotDetailsDto adapt(String hotspotKey, ServerHotspotDetails hotspot, FilePathTranslation translation) {
Expand Down
Loading

0 comments on commit 5e46ecd

Please sign in to comment.