Skip to content

Commit d234a0d

Browse files
wip
1 parent e9c0a69 commit d234a0d

File tree

7 files changed

+52
-17
lines changed

7 files changed

+52
-17
lines changed

API_CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* `getCleanCodeAttribute`
2525
* `getImpacts`
2626
* Use `getSeverityMode` instead.
27+
* Add SonarCloud region parameter to `org.sonarsource.sonarlint.core.rpc.protocol.client.connection.SonarCloudConnectionParams` to support multi-region SQC connection configuration
2728

2829
# 10.13
2930

backend/core/src/main/java/org/sonarsource/sonarlint/core/SonarCloudActiveEnvironment.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.net.URI;
2323
import java.util.Map;
24-
import java.util.Optional;
2524

2625
import static org.apache.commons.lang.StringUtils.removeEnd;
2726

@@ -63,12 +62,16 @@ public boolean isSonarQubeCloud(String uri) {
6362
.anyMatch(u -> u.equals(removeEnd(uri, "/")));
6463
}
6564

66-
public Optional<SonarCloudRegion> getRegion(String uri) {
65+
/**
66+
* Before calling this method, caller should make sure URI is SonarCloud
67+
*/
68+
public SonarCloudRegion getRegionOrThrow(String uri) {
6769
return uris.entrySet()
6870
.stream()
6971
.filter(e -> removeEnd(e.getValue().getProductionUri().toString(), "/").equals(removeEnd(uri, "/")))
7072
.findFirst()
71-
.map(Map.Entry::getKey);
73+
.map(Map.Entry::getKey)
74+
.orElseThrow(() -> new IllegalArgumentException("URI should be a known SonarCloud URI"));
7275
}
7376

7477
private static class SonarQubeCloudUris {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SonarLint Core - Implementation
3+
* Copyright (C) 2016-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonarsource.sonarlint.core.embedded.server;
21+
22+
import org.apache.hc.core5.http.ClassicHttpRequest;
23+
import org.apache.hc.core5.http.ProtocolException;
24+
import org.sonarsource.sonarlint.core.SonarCloudActiveEnvironment;
25+
26+
public class RequestHandlerUtils {
27+
28+
private RequestHandlerUtils() {
29+
// util
30+
}
31+
32+
public static String getServerUrlForSonarCloud(ClassicHttpRequest request, SonarCloudActiveEnvironment sonarCloudActiveEnvironment) throws ProtocolException {
33+
var originUrl = request.getHeader("Origin").getValue();
34+
// Since the 'isSonarCloud' check passed, we are sure that the region will be there
35+
var region = sonarCloudActiveEnvironment.getRegionOrThrow(originUrl);
36+
return sonarCloudActiveEnvironment.getUri(region).toString();
37+
}
38+
}

backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/ShowFixSuggestionRequestHandler.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import static org.apache.commons.lang3.StringUtils.isNotBlank;
7070
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
7171
import static org.sonarsource.sonarlint.core.commons.util.StringUtils.sanitizeAgainstRTLO;
72+
import static org.sonarsource.sonarlint.core.embedded.server.RequestHandlerUtils.getServerUrlForSonarCloud;
7273

7374
public class ShowFixSuggestionRequestHandler implements HttpRequestHandler {
7475

@@ -166,7 +167,7 @@ private static AssistCreatingConnectionParams createAssistServerConnectionParams
166167
String tokenValue = query.getTokenValue();
167168
if (query.isSonarCloud) {
168169
// It 'isSonarCloud' check passed, we are sure we will have a region
169-
var region = sonarCloudActiveEnvironment.getRegion(query.getServerUrl()).get();
170+
var region = sonarCloudActiveEnvironment.getRegionOrThrow(query.getServerUrl());
170171
return new AssistCreatingConnectionParams(new SonarCloudConnectionParams(query.getOrganizationKey(), tokenName, tokenValue, SonarCloudRegion.valueOf(region.name())));
171172
} else {
172173
return new AssistCreatingConnectionParams(new SonarQubeConnectionParams(query.getServerUrl(), tokenName, tokenValue));
@@ -211,13 +212,8 @@ ShowFixSuggestionQuery extractQuery(ClassicHttpRequest request, @Nullable String
211212
var payload = extractAndValidatePayload(request);
212213
boolean isSonarCloud = isSonarCloud(origin);
213214
String serverUrl;
214-
215-
// TODO remove duplication
216215
if (isSonarCloud) {
217-
var originUrl = request.getHeader("Origin").getValue();
218-
var region = sonarCloudActiveEnvironment.getRegion(originUrl);
219-
// Since the 'isSonarCloud' check passed, we are sure that the region will be there
220-
serverUrl = sonarCloudActiveEnvironment.getUri(region.get()).toString();
216+
serverUrl = getServerUrlForSonarCloud(request, sonarCloudActiveEnvironment);
221217
} else {
222218
serverUrl = params.get("server");
223219
}

backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/ShowIssueRequestHandler.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
import static org.apache.commons.lang3.StringUtils.isNotBlank;
6767
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
68+
import static org.sonarsource.sonarlint.core.embedded.server.RequestHandlerUtils.getServerUrlForSonarCloud;
6869

6970
public class ShowIssueRequestHandler implements HttpRequestHandler {
7071

@@ -207,12 +208,8 @@ ShowIssueQuery extractQuery(ClassicHttpRequest request) throws ProtocolException
207208
}
208209
boolean isSonarCloud = isSonarCloud(request);
209210
String serverUrl;
210-
211211
if (isSonarCloud) {
212-
var originUrl = request.getHeader("Origin").getValue();
213-
var region = sonarCloudActiveEnvironment.getRegion(originUrl);
214-
// Since the 'isSonarCloud' check passed, we are sure that the region will be there
215-
serverUrl = sonarCloudActiveEnvironment.getUri(region.get()).toString();
212+
serverUrl = getServerUrlForSonarCloud(request, sonarCloudActiveEnvironment);
216213
} else {
217214
serverUrl = params.get("server");
218215
}

backend/core/src/main/java/org/sonarsource/sonarlint/core/spring/SonarLintSpringAppConfig.java

-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ UserPaths provideClientPaths(InitializeParams initializeParams) {
206206
@Bean
207207
SonarCloudActiveEnvironment provideSonarCloudActiveEnvironment(InitializeParams params) {
208208
var alternativeSonarCloudEnv = params.getAlternativeSonarCloudEnvironment();
209-
// TODO consider giving possibility to provide custom urls for both regions
210209
return alternativeSonarCloudEnv == null ? SonarCloudActiveEnvironment.prod()
211210
: new SonarCloudActiveEnvironment(alternativeSonarCloudEnv.getUri(), alternativeSonarCloudEnv.getWebSocketsEndpointUri());
212211
}

test-utils/src/main/java/org/sonarsource/sonarlint/core/test/utils/SonarLintBackendFixture.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ public SonarLintBackendBuilder withSonarCloudConnection(String connectionId, Str
253253
storageBuilder.accept(storage);
254254
storages.add(storage);
255255
}
256-
sonarCloudConnections.add(new SonarCloudConnectionConfigurationDto(connectionId, organizationKey, org.sonarsource.sonarlint.core.rpc.protocol.common.SonarCloudRegion.EU, disableNotifications));
256+
sonarCloudConnections.add(new SonarCloudConnectionConfigurationDto(connectionId, organizationKey,
257+
org.sonarsource.sonarlint.core.rpc.protocol.common.SonarCloudRegion.EU, disableNotifications));
257258
return this;
258259
}
259260

0 commit comments

Comments
 (0)