Skip to content

Commit

Permalink
SLCORE-818 fix the issue regarding getting SC serverUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
serhat-yenican-sonarsource committed May 30, 2024
1 parent 0c8681b commit 13dcabc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.inject.Singleton;
import org.sonarsource.sonarlint.core.BindingCandidatesFinder;
import org.sonarsource.sonarlint.core.BindingSuggestionProvider;
import org.sonarsource.sonarlint.core.SonarCloudActiveEnvironment;
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
import org.sonarsource.sonarlint.core.commons.progress.ExecutorServiceShutdownWatchable;
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
Expand All @@ -56,9 +57,11 @@ public class RequestHandlerBindingAssistant {
private final ConfigurationRepository configurationRepository;
private final UserTokenService userTokenService;
private final ExecutorServiceShutdownWatchable<?> executorService;
private final String sonarCloudUrl;

public RequestHandlerBindingAssistant(BindingSuggestionProvider bindingSuggestionProvider, BindingCandidatesFinder bindingCandidatesFinder, SonarLintRpcClient client,
ConnectionConfigurationRepository connectionConfigurationRepository, ConfigurationRepository configurationRepository, UserTokenService userTokenService) {
public RequestHandlerBindingAssistant(BindingSuggestionProvider bindingSuggestionProvider, BindingCandidatesFinder bindingCandidatesFinder,
SonarLintRpcClient client, ConnectionConfigurationRepository connectionConfigurationRepository, ConfigurationRepository configurationRepository,
UserTokenService userTokenService, SonarCloudActiveEnvironment sonarCloudActiveEnvironment) {
this.bindingSuggestionProvider = bindingSuggestionProvider;
this.bindingCandidatesFinder = bindingCandidatesFinder;
this.client = client;
Expand All @@ -67,6 +70,7 @@ public RequestHandlerBindingAssistant(BindingSuggestionProvider bindingSuggestio
this.userTokenService = userTokenService;
this.executorService = new ExecutorServiceShutdownWatchable<>(new ThreadPoolExecutor(0, 1, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), r -> new Thread(r, "Show Issue or Hotspot Request Handler")));
this.sonarCloudUrl = sonarCloudActiveEnvironment.getUri().toString();
}

interface Callback {
Expand All @@ -81,7 +85,7 @@ void assistConnectionAndBindingIfNeededAsync(AssistCreatingConnectionParams conn

private void assistConnectionAndBindingIfNeeded(AssistCreatingConnectionParams connectionParams, String projectKey,
Callback callback, SonarLintCancelMonitor cancelMonitor) {
String serverUrl = connectionParams.getServerUrl();
String serverUrl = getServerUrl(connectionParams);
LOG.debug("Assist connection and binding if needed for project {} and server {}", projectKey, serverUrl);
try {
var connectionsMatchingOrigin = connectionConfigurationRepository.findByUrl(serverUrl);
Expand All @@ -105,6 +109,10 @@ private void assistConnectionAndBindingIfNeeded(AssistCreatingConnectionParams c
}
}

private String getServerUrl(AssistCreatingConnectionParams connectionParams) {
return connectionParams.getConnection().isLeft() ? connectionParams.getConnection().getLeft().getServerUrl() : sonarCloudUrl;
}

private AssistCreatingConnectionResponse assistCreatingConnectionAndWaitForRepositoryUpdate(
AssistCreatingConnectionParams connectionParams, SonarLintCancelMonitor cancelMonitor) {
var assistNewConnectionResult = assistCreatingConnection(connectionParams, cancelMonitor);
Expand Down Expand Up @@ -194,7 +202,7 @@ private void revokeToken(AssistCreatingConnectionParams connectionParams, SonarL
String tokenName = connectionParams.getTokenName();
String tokenValue = connectionParams.getTokenValue();
if (tokenName != null && tokenValue != null) {
var revokeTokenParams = new RevokeTokenParams(connectionParams.getServerUrl(), tokenName, tokenValue);
var revokeTokenParams = new RevokeTokenParams(getServerUrl(connectionParams), tokenName, tokenValue);
userTokenService.revokeToken(revokeTokenParams, cancelMonitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,6 @@ private static ShowIssueRequestHandler getShowIssueRequestHandler(TelemetryServi
var userTokenService = mock(UserTokenService.class);
SonarCloudActiveEnvironment sonarCloudActiveEnvironment = SonarCloudActiveEnvironment.prod();
return new ShowIssueRequestHandler(sonarLintClient, serverApiProvider, telemetryService,
new RequestHandlerBindingAssistant(bindingSuggestionProvider, bindingCandidatesFinder, sonarLintClient, repository, configurationRepository, userTokenService), pathTranslationService, sonarCloudActiveEnvironment);
new RequestHandlerBindingAssistant(bindingSuggestionProvider, bindingCandidatesFinder, sonarLintClient, repository, configurationRepository, userTokenService, sonarCloudActiveEnvironment), pathTranslationService, sonarCloudActiveEnvironment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void it_should_assist_creating_the_connection_when_server_url_unknown() throws E
ArgumentCaptor<AssistCreatingConnectionParams> captor = ArgumentCaptor.captor();
verify(fakeClient, timeout(1000)).assistCreatingConnection(captor.capture(), any());
assertThat(captor.getAllValues())
.extracting(AssistCreatingConnectionParams::getServerUrl,
.extracting(connectionParams -> connectionParams.getConnection().getLeft().getServerUrl(),
connectionParams -> connectionParams.getConnection().getLeft() != null,
AssistCreatingConnectionParams::getTokenName,
AssistCreatingConnectionParams::getTokenValue)
Expand Down

0 comments on commit 13dcabc

Please sign in to comment.