Skip to content

Commit f100f00

Browse files
SCAN4NET-358 host.url = sonarqube.us should infer the us region (#2409)
1 parent 402ad1b commit f100f00

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

Tests/SonarScanner.MSBuild.PreProcessor.Test/ArgumentProcessorTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,21 @@ public void PreArgProc_Region_Invalid()
217217
@"The arguments 'sonar.host.url' and 'sonar.scanner.sonarcloudUrl' are both set. Please set only 'sonar.scanner.sonarcloudUrl'.")]
218218
[DataRow(null, "https://host", null, "https://api", typeof(ServerHostInfo), "https://host", "https://api")]
219219
[DataRow(null, "https://host", null, null, typeof(ServerHostInfo), "https://host", "https://host/api/v2")]
220-
public void PreArgProc_Region_Overrides(string region, string hostOverride, string sonarClourUrlOverride, string apiOverride, Type expectedHostInforType, string expectedHostUri, string expectedApiUri, params string[] expectedWarnings)
220+
[DataRow(null, "https://SONARQUBE.us/", null, null, typeof(CloudHostInfo), "https://SONARQUBE.us/", "https://api.sonarqube.us")]
221+
[DataRow(null, null, "https://SONARQUBE.us/", null, typeof(CloudHostInfo), "https://SONARQUBE.us/", "https://api.sonarqube.us")]
222+
[DataRow(null, "https://sonarqube.us", null, "https://api", typeof(CloudHostInfo), "https://sonarqube.us", "https://api")]
223+
[DataRow(null, null, "https://sonarqube.us", "https://api", typeof(CloudHostInfo), "https://sonarqube.us", "https://api")]
224+
[DataRow("US", "https://SONARQUBE.us/", null, null, typeof(CloudHostInfo), "https://SONARQUBE.us/", "https://api.sonarqube.us",
225+
@"The sonar.region parameter is set to ""US"". The setting will be overriden by one or more of the properties sonar.host.url, sonar.scanner.sonarcloudUrl, or sonar.scanner.apiBaseUrl.")]
226+
[DataRow(null, "https://SONARCLOUD.io/", null, null, typeof(CloudHostInfo), "https://SONARCLOUD.io/", "https://api.sonarcloud.io")]
227+
[DataRow(null, null, "https://SONARCLOUD.io/", null, typeof(CloudHostInfo), "https://SONARCLOUD.io/", "https://api.sonarcloud.io")]
228+
[DataRow(null, "https://sonarcloud.io", null, "https://api", typeof(CloudHostInfo), "https://sonarcloud.io", "https://api")]
229+
[DataRow(null, null, "https://sonarcloud.io", "https://api", typeof(CloudHostInfo), "https://sonarcloud.io", "https://api")]
230+
[DataRow("us", "https://sonarcloud.io", null, null, typeof(CloudHostInfo), "https://sonarcloud.io", "https://api.sonarcloud.io",
231+
@"The sonar.region parameter is set to ""us"". The setting will be overriden by one or more of the properties sonar.host.url, sonar.scanner.sonarcloudUrl, or sonar.scanner.apiBaseUrl.")]
232+
[DataRow("us", null, "https://sonarcloud.io", null, typeof(CloudHostInfo), "https://sonarcloud.io", "https://api.sonarcloud.io",
233+
@"The sonar.region parameter is set to ""us"". The setting will be overriden by one or more of the properties sonar.host.url, sonar.scanner.sonarcloudUrl, or sonar.scanner.apiBaseUrl.")]
234+
public void PreArgProc_Region_Overrides(string region, string hostOverride, string sonarClourUrlOverride, string apiOverride, Type expectedHostInfoType, string expectedHostUri, string expectedApiUri, params string[] expectedWarnings)
221235
{
222236
var logger = new TestLogger();
223237
var args = CheckProcessingSucceeds(
@@ -232,8 +246,8 @@ public void PreArgProc_Region_Overrides(string region, string hostOverride, stri
232246
.. apiOverride is null ? Array.Empty<string>() : [$"/d:{SonarProperties.ApiBaseUrl}={apiOverride}"],
233247
]);
234248

235-
args.ServerInfo.Should().BeOfType(expectedHostInforType);
236-
args.ServerInfo.Should().BeEquivalentTo(new { ServerUrl = expectedHostUri, ApiBaseUrl = expectedApiUri, IsSonarCloud = expectedHostInforType == typeof(CloudHostInfo) });
249+
args.ServerInfo.Should().BeOfType(expectedHostInfoType);
250+
args.ServerInfo.Should().BeEquivalentTo(new { ServerUrl = expectedHostUri, ApiBaseUrl = expectedApiUri, IsSonarCloud = expectedHostInfoType == typeof(CloudHostInfo) });
237251
logger.AssertDebugLogged($"Server Url: {expectedHostUri}");
238252
logger.AssertDebugLogged($"Api Url: {expectedApiUri}");
239253
logger.AssertDebugLogged($"Is SonarCloud: {args.ServerInfo.IsSonarCloud}");

Tests/SonarScanner.MSBuild.PreProcessor.Test/ProcessedArgsTests.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,26 +304,33 @@ public void ProcArgs_HostUrl_SonarCloudUrl_HostUrlAndSonarcloudUrlMissing()
304304
}
305305

306306
[DataTestMethod]
307-
[DataRow("https://sonarcloud.io")]
308-
[DataRow("https://SonarCloud.io")]
309-
[DataRow("https://SONARCLOUD.IO")]
310-
[DataRow("https://sonarcloud.io/")]
311-
[DataRow("https://sonarcloud.io///")]
312-
[DataRow("https://sonarqube.us")]
313-
[DataRow("https://SonarQube.us")]
314-
[DataRow("https://SONARQUBE.US")]
315-
[DataRow("https://sonarqube.us/")]
316-
[DataRow("https://sonarqube.us///")]
317-
public void ProcArgs_HostUrl_SonarCloudUrl_HostUrlIsAnySonarCloud(string hostUrl)
307+
[DataRow("https://sonarcloud.io", "https://api.sonarcloud.io", "")]
308+
[DataRow("https://SonarCloud.io", "https://api.sonarcloud.io", "")]
309+
[DataRow("https://SONARCLOUD.IO", "https://api.sonarcloud.io", "")]
310+
[DataRow("https://sonarcloud.io/", "https://api.sonarcloud.io", "")]
311+
[DataRow("https://sonarcloud.io///", "https://api.sonarcloud.io", "")]
312+
[DataRow("https://sonarqube.us", "https://api.sonarqube.us", "us")]
313+
[DataRow("https://SonarQube.us", "https://api.sonarqube.us", "us")]
314+
[DataRow("https://SONARQUBE.US", "https://api.sonarqube.us", "us")]
315+
[DataRow("https://sonarqube.us/", "https://api.sonarqube.us", "us")]
316+
[DataRow("https://sonarqube.us///", "https://api.sonarqube.us", "us")]
317+
public void ProcArgs_HostUrl_SonarCloudUrl_HostUrlIsAnySonarCloud(string hostUrl, string expectedApiBaseUrl, string expectedRegion)
318318
{
319319
var sut = CreateDefaultArgs(new ListPropertiesProvider([new Property(SonarProperties.HostUrl, hostUrl)]));
320320

321-
sut.ServerInfo.Should().NotBeNull();
322-
sut.ServerInfo.IsSonarCloud.Should().BeTrue();
323-
sut.ServerInfo.ServerUrl.Should().Be(hostUrl);
321+
sut.Should().BeEquivalentTo(new
322+
{
323+
IsValid = true,
324+
ServerInfo = new
325+
{
326+
IsSonarCloud = true,
327+
ServerUrl = hostUrl,
328+
ApiBaseUrl = expectedApiBaseUrl,
329+
Region = expectedRegion,
330+
},
331+
});
324332
logger.Warnings.Should().BeEmpty();
325333
logger.Errors.Should().BeEmpty();
326-
sut.IsValid.Should().BeTrue();
327334
}
328335

329336
[TestMethod]

src/SonarScanner.MSBuild.PreProcessor/HostInfo.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public record CloudHostInfo(string ServerUrl, string ApiBaseUrl, string Region)
8585
public static new CloudHostInfo FromProperties(ILogger logger, string sonarHostUrl, string sonarCloudUrl, string apiBaseUrl, string region)
8686
{
8787
region = region?.Trim().ToLower() ?? string.Empty;
88-
if (KnownRegions.SingleOrDefault(x => x.Region == region) is { } knownRegion)
88+
if ((KnownRegionByUrl(sonarHostUrl) ?? KnownRegionByUrl(sonarCloudUrl) ?? KnownRegions.SingleOrDefault(x => x.Region == region)) is { } knownRegion)
8989
{
9090
var serverUrl = sonarCloudUrl ?? sonarHostUrl ?? knownRegion.ServerUrl;
9191
var apiUrl = apiBaseUrl ?? knownRegion.ApiBaseUrl;
@@ -98,9 +98,14 @@ public record CloudHostInfo(string ServerUrl, string ApiBaseUrl, string Region)
9898
}
9999
}
100100

101-
public static bool IsKnownUrl(string url)
101+
public static bool IsKnownUrl(string url) =>
102+
KnownRegionByUrl(url) is not null;
103+
104+
private static CloudHostInfo KnownRegionByUrl(string url)
102105
{
103-
url = url.TrimEnd('/');
104-
return KnownRegions.Any(x => x.ServerUrl.Equals(url, StringComparison.InvariantCultureIgnoreCase));
106+
url = url?.TrimEnd('/');
107+
return string.IsNullOrWhiteSpace(url)
108+
? null
109+
: KnownRegions.SingleOrDefault(x => string.Equals(x.ServerUrl, url, StringComparison.InvariantCultureIgnoreCase));
105110
}
106111
}

0 commit comments

Comments
 (0)