Skip to content

Commit

Permalink
SLCORE-1120 Add region field to SonarCloudConnectionConfigurationDto
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Jan 21, 2025
1 parent cee4816 commit 00bf001
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static SonarQubeConnectionConfiguration adapt(SonarQubeConnectionConfigu
}

private SonarCloudConnectionConfiguration adapt(SonarCloudConnectionConfigurationDto scDto) {
return new SonarCloudConnectionConfiguration(sonarCloudUri, scDto.getConnectionId(), scDto.getOrganization(), scDto.isDisableNotifications());
return new SonarCloudConnectionConfiguration(sonarCloudUri, scDto.getConnectionId(), scDto.getOrganization(), scDto.getRegion(), scDto.isDisableNotifications());
}

private static void putAndLogIfDuplicateId(Map<String, AbstractConnectionConfiguration> map, AbstractConnectionConfiguration config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* SonarLint Core - Implementation
* Copyright (C) 2016-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.core;

public enum SonarCloudRegion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import java.net.URI;
import java.util.Objects;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.SonarCloudRegion;
import org.sonarsource.sonarlint.core.commons.ConnectionKind;
import org.sonarsource.sonarlint.core.serverapi.EndpointParams;

public class SonarCloudConnectionConfiguration extends AbstractConnectionConfiguration {

private final String organization;
private final SonarCloudRegion region;

public SonarCloudConnectionConfiguration(URI uri, String connectionId, String organization, boolean disableNotifications) {
public SonarCloudConnectionConfiguration(URI uri, String connectionId, String organization, String region, boolean disableNotifications) {
super(connectionId, ConnectionKind.SONARCLOUD, disableNotifications, uri.toString());
this.organization = organization;
this.region = SonarCloudRegion.valueOf(region);
}

public String getOrganization() {
Expand All @@ -43,6 +46,10 @@ public EndpointParams getEndpointParams() {
return new EndpointParams(getUrl(), true, organization);
}

public SonarCloudRegion getRegion() {
return region;
}

@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ void should_detect_sonar_scanner_for_sonarcloud_based_on_url() {
mockFindFileByNamesInScope(
List.of(buildClientFile("sonar-project.properties", "path/to/sonar-project.properties", "sonar.host.url=https://sonarcloud.io\nsonar.projectKey=" + PROJECT_KEY_1)));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_2)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_2, MY_ORG_2, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_2)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_2, MY_ORG_2, "EU", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SC_CONNECTION_ID_2), new SonarLintCancelMonitor());

Expand All @@ -121,8 +121,8 @@ void should_detect_sonar_scanner_for_sonarcloud_based_on_url() {
void should_detect_sonar_scanner_for_sonarcloud_based_on_organization() {
mockFindFileByNamesInScope(List.of(buildClientFile("sonar-project.properties", "path/to/sonar-project.properties", "sonar.organization=" + MY_ORG_2)));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_2)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_2, MY_ORG_2, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_2)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_2, MY_ORG_2, "EU", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SC_CONNECTION_ID_2), new SonarLintCancelMonitor());

Expand All @@ -137,7 +137,7 @@ void should_detect_sonar_scanner_for_sonarcloud_based_on_organization() {
void should_detect_autoscan_for_sonarcloud() {
mockFindFileByNamesInScope(List.of(buildClientFile(".sonarcloud.properties", "path/to/.sonarcloud.properties", "sonar.projectKey=" + PROJECT_KEY_1)));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SQ_CONNECTION_ID_1)).thenReturn(new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID_1, "http://mysonarqube.org", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SQ_CONNECTION_ID_1), new SonarLintCancelMonitor());
Expand All @@ -153,7 +153,7 @@ void should_detect_autoscan_for_sonarcloud() {
void should_detect_unknown_with_project_key() {
mockFindFileByNamesInScope(List.of(buildClientFile("sonar-project.properties", "path/to/sonar-project.properties", "sonar.projectKey=" + PROJECT_KEY_1)));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SQ_CONNECTION_ID_1)).thenReturn(new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID_1, "http://mysonarqube.org", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SQ_CONNECTION_ID_1), new SonarLintCancelMonitor());
Expand All @@ -169,7 +169,7 @@ void should_detect_unknown_with_project_key() {
void ignore_scanner_file_without_clue() {
mockFindFileByNamesInScope(List.of(buildClientFile("sonar-project.properties", "path/to/sonar-project.properties", "sonar.sources=src")));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SQ_CONNECTION_ID_1)).thenReturn(new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID_1, "http://mysonarqube.org", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SQ_CONNECTION_ID_1), new SonarLintCancelMonitor());
Expand All @@ -181,7 +181,7 @@ void ignore_scanner_file_without_clue() {
void ignore_scanner_file_invalid_content() {
mockFindFileByNamesInScope(List.of(buildClientFile("sonar-project.properties", "path/to/sonar-project.properties", "\\usonar.projectKey=" + PROJECT_KEY_1)));

when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, true));
when(connectionRepository.getConnectionById(SC_CONNECTION_ID_1)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID_1, MY_ORG_1, "EU", true));
when(connectionRepository.getConnectionById(SQ_CONNECTION_ID_1)).thenReturn(new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID_1, "http://mysonarqube.org", true));

var bindingClueWithConnections = underTest.collectBindingCluesWithConnections(CONFIG_SCOPE_ID, Set.of(SC_CONNECTION_ID_1, SQ_CONNECTION_ID_1), new SonarLintCancelMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BindingSuggestionProviderTests {
public static final String SC_1_ID = "sc1";
public static final String SQ_2_ID = "sq2";
public static final SonarQubeConnectionConfiguration SQ_1 = new SonarQubeConnectionConfiguration(SQ_1_ID, "http://mysonarqube.com", true);
public static final SonarCloudConnectionConfiguration SC_1 = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_1_ID, "myorg", true);
public static final SonarCloudConnectionConfiguration SC_1 = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_1_ID, "myorg", "EU", true);
public static final String CONFIG_SCOPE_ID_1 = "configScope1";
public static final String PROJECT_KEY_1 = "projectKey1";
public static final ServerProject SERVER_PROJECT_1 = serverProject(PROJECT_KEY_1, "Project 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class ConnectionServiceTests {
public static final SonarQubeConnectionConfigurationDto SQ_DTO_1 = new SonarQubeConnectionConfigurationDto("sq1", "http://url1/", true);
public static final SonarQubeConnectionConfigurationDto SQ_DTO_1_DUP = new SonarQubeConnectionConfigurationDto("sq1", "http://url1_dup/", true);
public static final SonarQubeConnectionConfigurationDto SQ_DTO_2 = new SonarQubeConnectionConfigurationDto("sq2", "url2", true);
public static final SonarCloudConnectionConfigurationDto SC_DTO_1 = new SonarCloudConnectionConfigurationDto("sc1", "org1", true);
public static final SonarCloudConnectionConfigurationDto SC_DTO_2 = new SonarCloudConnectionConfigurationDto("sc2", "org2", true);
public static final SonarCloudConnectionConfigurationDto SC_DTO_1 = new SonarCloudConnectionConfigurationDto("sc1", "org1", "EU", true);
public static final SonarCloudConnectionConfigurationDto SC_DTO_2 = new SonarCloudConnectionConfigurationDto("sc2", "org2", "EU", true);

ApplicationEventPublisher eventPublisher;
ConnectionService underTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void getServerApi_returns_empty_if_connection_doesnt_exists() {

@Test
void getServerApi_returns_empty_if_client_cant_provide_httpclient() {
when(connectionRepository.getConnectionById("sc1")).thenReturn(new SonarCloudConnectionConfiguration(URI.create("http://server1"), "sc1", "myorg", true));
when(connectionRepository.getConnectionById("sc1")).thenReturn(new SonarCloudConnectionConfiguration(URI.create("http://server1"), "sc1", "myorg", "EU", true));
when(awareHttpClientProvider.getHttpClient("sc1", true)).thenReturn(null);

var serverApi = underTest.getServerApi("sc1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void it_should_calculate_connectedMode_usesSC_notDisabledNotifications_telemetry
when(configurationRepository.getAllBoundScopes()).thenReturn(Set.of(new BoundScope(configurationScopeId, connectionId, projectKey)));

var connectionConfigurationRepository = mock(ConnectionConfigurationRepository.class);
when(connectionConfigurationRepository.getConnectionById(connectionId)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, connectionId, "myTestOrg", false));
when(connectionConfigurationRepository.getConnectionById(connectionId)).thenReturn(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, connectionId, "myTestOrg", "EU", false));
var underTest = new TelemetryServerAttributesProvider(configurationRepository, connectionConfigurationRepository, mock(RulesService.class), mock(RulesRepository.class), mock(NodeJsService.class));

var telemetryLiveAttributes = underTest.getTelemetryServerLiveAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class VersionSoonUnsupportedHelperTests {
private static final String SC_CONNECTION_ID = "scConnectionId";
private static final SonarQubeConnectionConfiguration SQ_CONNECTION = new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID, "https://mysonarqube.com", true);
private static final SonarQubeConnectionConfiguration SQ_CONNECTION_2 = new SonarQubeConnectionConfiguration(SQ_CONNECTION_ID_2, "https://mysonarqube2.com", true);
private static final SonarCloudConnectionConfiguration SC_CONNECTION = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID, "https://sonarcloud.com", true);
private static final SonarCloudConnectionConfiguration SC_CONNECTION = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, SC_CONNECTION_ID, "https://sonarcloud.com", "EU", true);

private final SonarLintRpcClient client = mock(SonarLintRpcClient.class);
private final ServerApiProvider serverApiProvider = mock(ServerApiProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void should_cancel_flow_when_branch_does_not_match() throws HttpException, IOExc
var context = mock(HttpContext.class);

when(connectionConfigurationRepository.findByOrganization(any())).thenReturn(List.of(
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", false)));
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", "EU", false)));
when(configurationRepository.getBoundScopesToConnectionAndSonarProject(any(), any())).thenReturn(List.of(new BoundScope("configScope", "connectionId", "projectKey")));
when(sonarLintRpcClient.matchProjectBranch(any())).thenReturn(CompletableFuture.completedFuture(new MatchProjectBranchResponse(false)));

Expand Down Expand Up @@ -308,7 +308,7 @@ void should_find_main_branch_when_not_provided_and_not_stored() throws HttpExcep
when(clientFile.getUri()).thenReturn(URI.create("file:///src/main/java/Main.java"));
when(filePathTranslation.serverToIdePath(any())).thenReturn(Path.of("src/main/java/Main.java"));
when(connectionConfigurationRepository.findByOrganization(any())).thenReturn(List.of(
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", false)));
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", "EU", false)));
when(configurationRepository.getBoundScopesToConnectionAndSonarProject(any(), any())).thenReturn(List.of(new BoundScope("configScope", "connectionId", "projectKey")));
when(sonarLintRpcClient.matchProjectBranch(any())).thenReturn(CompletableFuture.completedFuture(new MatchProjectBranchResponse(true)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void should_cancel_flow_when_branch_does_not_match() throws HttpException, IOExc
var context = mock(HttpContext.class);

when(connectionConfigurationRepository.findByOrganization(any())).thenReturn(List.of(
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", false)));
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", "EU", false)));
when(configurationRepository.getBoundScopesToConnectionAndSonarProject(any(), any())).thenReturn(List.of(new BoundScope("configScope"
, "connectionId", "projectKey")));
when(sonarLintRpcClient.matchProjectBranch(any())).thenReturn(CompletableFuture.completedFuture(new MatchProjectBranchResponse(false)));
Expand Down Expand Up @@ -394,7 +394,7 @@ void should_find_main_branch_when_branch_is_not_provided() throws HttpException,
var context = mock(HttpContext.class);

when(connectionConfigurationRepository.findByOrganization(any())).thenReturn(List.of(
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", false)));
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", "EU", false)));
when(configurationRepository.getBoundScopesToConnectionAndSonarProject(any(), any())).thenReturn(List.of(new BoundScope("configScope"
, "connectionId", "projectKey")));
when(sonarLintRpcClient.matchProjectBranch(any())).thenReturn(CompletableFuture.completedFuture(new MatchProjectBranchResponse(true)));
Expand Down Expand Up @@ -424,7 +424,7 @@ void should_find_main_branch_when_not_provided_and_not_stored() throws HttpExcep
var context = mock(HttpContext.class);

when(connectionConfigurationRepository.findByOrganization(any())).thenReturn(List.of(
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", false)));
new SonarCloudConnectionConfiguration(PRODUCTION_EU_URI, "name", "organizationKey", "EU", false)));
when(configurationRepository.getBoundScopesToConnectionAndSonarProject(any(), any())).thenReturn(List.of(new BoundScope("configScope"
, "connectionId", "projectKey")));
when(sonarLintRpcClient.matchProjectBranch(any())).thenReturn(CompletableFuture.completedFuture(new MatchProjectBranchResponse(true)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class SonarCloudConnectionConfigurationTest {

@Test
void testEqualsAndHashCode() {
var underTest = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", true);
var underTest = new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", "EU", true);
assertThat(underTest)
.isEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", true))
.isNotEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id2", "org1", true))
.isNotEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org2", true))
.isEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", "EU", true))
.isNotEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id2", "org1", "EU", true))
.isNotEqualTo(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org2", "EU", true))
.isNotEqualTo(new SonarQubeConnectionConfiguration("id1", "http://server1", true))
.hasSameHashCodeAs(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", true));
.hasSameHashCodeAs(new SonarCloudConnectionConfiguration(SonarCloudActiveEnvironment.PRODUCTION_EU_URI, "id1", "org1", "EU", true));
}

}
Loading

0 comments on commit 00bf001

Please sign in to comment.