From bdaf199ff439695e29081c85810a5b46c5658df7 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh <117642191+georgii-borovinskikh-sonarsource@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:29:57 +0000 Subject: [PATCH] Remove string uri from SharedBindingConfig (#5013) Fixes #5009 --- .../SharedBindingConfigFileProviderTests.cs | 25 ++++++++++++++----- .../Shared/SharedBindingConfigModelTests.cs | 3 ++- .../Migration/ConnectedModeMigration.cs | 2 +- .../Shared/SharedBindingConfigFileProvider.cs | 16 ++++++++---- .../Shared/SharedBindingConfigModel.cs | 16 +----------- .../Connection/ConnectControllerTests.cs | 10 ++++---- .../MefServices/VsSessionHostTests.cs | 2 +- .../SaveSharedConnectionCommandTests.cs | 7 +++--- .../SaveSharedConnectionCommand.cs | 2 +- .../Connection/ConnectionController.cs | 2 +- src/Integration/MefServices/VsSessionHost.cs | 2 +- 11 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigFileProviderTests.cs b/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigFileProviderTests.cs index 53ba8a0d0d..6baa0d647e 100644 --- a/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigFileProviderTests.cs +++ b/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigFileProviderTests.cs @@ -52,8 +52,7 @@ public void ReadSharedBindingConfigFile_SQConfig_Reads() var result = testSubject.ReadSharedBindingConfigFile(filePath); - result.Uri.Should().Be("https://127.0.0.1:9000"); - result.ServerUri.Should().BeEquivalentTo(uri); + result.Uri.Should().BeEquivalentTo(uri); result.ProjectKey.Should().Be("projectKey"); result.Organization.Should().BeNull(); } @@ -63,7 +62,6 @@ public void ReadSharedBindingConfigFile_SCConfig_Reads() { string configFileContent = @"{""SonarCloudOrganization"":""Some Organisation"",""ProjectKey"":""projectKey""}"; string filePath = "Some Path"; - var uri = new Uri(SharedBindingConfigFileProvider.SonarCloudUri); var file = CreateFile(filePath, configFileContent); var fileSystem = GetFileSystem(file.Object); @@ -74,8 +72,7 @@ public void ReadSharedBindingConfigFile_SCConfig_Reads() result.Organization.Should().Be("Some Organisation"); result.ProjectKey.Should().Be("projectKey"); - result.Uri.Should().Be(SharedBindingConfigFileProvider.SonarCloudUri); - result.ServerUri.Should().BeEquivalentTo(uri); + result.Uri.Should().BeEquivalentTo(SharedBindingConfigFileProvider.SonarCloudUri); } [TestMethod] @@ -125,6 +122,22 @@ public void ReadSharedBindingConfigFile_InvalidUri_ReturnsNull() result.Should().BeNull(); } + + [TestMethod] + public void ReadSharedBindingConfigFile_InvalidProjectKey_ReturnsNull() + { + var configFileContent = @"{""SonarQubeUri"":""http://localhost"",""ProjectKey"":"" ""}"; + var filePath = "Some Path"; + + var file = CreateFile(filePath, configFileContent); + var fileSystem = GetFileSystem(file.Object); + + var testSubject = CreateTestSubject(fileSystem.Object); + + var result = testSubject.ReadSharedBindingConfigFile(filePath); + + result.Should().BeNull(); + } [TestMethod] public void WriteSharedBindingConfigFile_SQConfig_Writes() @@ -133,7 +146,7 @@ public void WriteSharedBindingConfigFile_SQConfig_Writes() ""SonarQubeUri"": ""https://127.0.0.1:9000"", ""ProjectKey"": ""projectKey"" }"; - var config = new SharedBindingConfigModel() { Uri = "https://127.0.0.1:9000", ProjectKey = "projectKey" }; + var config = new SharedBindingConfigModel() { Uri = new Uri("https://127.0.0.1:9000"), ProjectKey = "projectKey" }; var filePath = "C:\\Solution\\.sonarlint\\Solution.json"; var file = CreateFile(); diff --git a/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigModelTests.cs b/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigModelTests.cs index 95ff7227d2..be49be8791 100644 --- a/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigModelTests.cs +++ b/src/ConnectedMode.UnitTests/Shared/SharedBindingConfigModelTests.cs @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using System; using SonarLint.VisualStudio.ConnectedMode.Shared; using SonarQube.Client; @@ -51,7 +52,7 @@ public void GetServerType_Null_ReturnsNull() [TestMethod] public void GetServerType_NoOrganization_ReturnsQube() { - SharedBindingConfigModel model = new SharedBindingConfigModel{ProjectKey = "abc", Uri = "https://next.sonarqube.com"}; + SharedBindingConfigModel model = new SharedBindingConfigModel{ProjectKey = "abc", Uri = new Uri("https://next.sonarqube.com")}; model.GetServerType().Should().Be(ServerType.SonarQube); } diff --git a/src/ConnectedMode/Migration/ConnectedModeMigration.cs b/src/ConnectedMode/Migration/ConnectedModeMigration.cs index 71e387cfea..a983d6c6fb 100644 --- a/src/ConnectedMode/Migration/ConnectedModeMigration.cs +++ b/src/ConnectedMode/Migration/ConnectedModeMigration.cs @@ -161,7 +161,7 @@ private async Task MigrateImplAsync(BoundSonarQubeProject oldBinding, IProgress< private bool SaveSharedBinding(BoundSonarQubeProject binding) { - var sharedBindingConfigModel = new SharedBindingConfigModel { Organization = binding.Organization?.Key, ProjectKey = binding.ProjectKey, Uri = binding.ServerUri.ToString() }; + var sharedBindingConfigModel = new SharedBindingConfigModel { Organization = binding.Organization?.Key, ProjectKey = binding.ProjectKey, Uri = binding.ServerUri }; return sharedBindingConfigProvider.SaveSharedBinding(sharedBindingConfigModel) != null; } diff --git a/src/ConnectedMode/Shared/SharedBindingConfigFileProvider.cs b/src/ConnectedMode/Shared/SharedBindingConfigFileProvider.cs index 6dc79a8224..82079e2a5f 100644 --- a/src/ConnectedMode/Shared/SharedBindingConfigFileProvider.cs +++ b/src/ConnectedMode/Shared/SharedBindingConfigFileProvider.cs @@ -45,7 +45,7 @@ internal class SharedBindingConfigFileProvider : ISharedBindingConfigFileProvide private readonly ILogger logger; private readonly IFileSystem fileSystem; - internal /*for testing*/ const string SonarCloudUri = "https://sonarcloud.io"; + internal /*for testing*/ static readonly Uri SonarCloudUri = new Uri("https://sonarcloud.io"); [ImportingConstructor] public SharedBindingConfigFileProvider(ILogger logger) : this(logger, new FileSystem()) @@ -59,22 +59,28 @@ internal SharedBindingConfigFileProvider(ILogger logger, IFileSystem fileSystem) public SharedBindingConfigModel ReadSharedBindingConfigFile(string filePath) { - SharedBindingConfigModel result = null; - try { - result = JsonConvert.DeserializeObject(fileSystem.File.ReadAllText(filePath)); + var result = JsonConvert.DeserializeObject(fileSystem.File.ReadAllText(filePath)); + if (result.IsSonarCloud()) { result.Uri = SonarCloudUri; } + + if (!string.IsNullOrWhiteSpace(result.ProjectKey) + && result.Uri != null + && (result.Uri.Scheme == Uri.UriSchemeHttp || result.Uri.Scheme == Uri.UriSchemeHttps)) + { + return result; + } } catch (Exception ex) { logger.LogVerbose($"[SharedBindingConfigFileProvider] Unable to read shared sonarlint config file: {ex.Message}"); } - return result; + return null; } public bool WriteSharedBindingConfigFile(string filePath, SharedBindingConfigModel sharedBindingConfigModel) diff --git a/src/ConnectedMode/Shared/SharedBindingConfigModel.cs b/src/ConnectedMode/Shared/SharedBindingConfigModel.cs index 0138d0939b..909baa8732 100644 --- a/src/ConnectedMode/Shared/SharedBindingConfigModel.cs +++ b/src/ConnectedMode/Shared/SharedBindingConfigModel.cs @@ -26,19 +26,8 @@ namespace SonarLint.VisualStudio.ConnectedMode.Shared { public class SharedBindingConfigModel { - private string uri; - [JsonProperty("SonarQubeUri", NullValueHandling = NullValueHandling.Ignore)] - public string Uri - { - get => uri; - set - { - uri = value; - - ServerUri = uri != null ? new Uri(uri) : null; - } - } + public Uri Uri { get; set; } [JsonProperty("SonarCloudOrganization", NullValueHandling = NullValueHandling.Ignore)] public string Organization { get; set; } @@ -46,9 +35,6 @@ public string Uri [JsonProperty("ProjectKey")] public string ProjectKey { get; set; } - [JsonIgnore] - public Uri ServerUri { get; private set; } - public bool IsSonarCloud() => !string.IsNullOrWhiteSpace(Organization); } diff --git a/src/Integration.UnitTests/Connection/ConnectControllerTests.cs b/src/Integration.UnitTests/Connection/ConnectControllerTests.cs index e52ff565cc..b69ee99ba3 100644 --- a/src/Integration.UnitTests/Connection/ConnectControllerTests.cs +++ b/src/Integration.UnitTests/Connection/ConnectControllerTests.cs @@ -233,7 +233,7 @@ public void ConnectionController_ConnectCommand_SharedConfigAndCredentialsPresen var connectionProviderMock = new Mock(); var testSubject = new ConnectionController(this.serviceProvider, this.host, null, connectionProviderMock.Object, connectionWorkflowMock.Object); - host.SharedBindingConfig = new SharedBindingConfigModel { ProjectKey = "projectKey", Uri = "https://sonarcloudi.io", Organization = "Org"}; + host.SharedBindingConfig = new SharedBindingConfigModel { ProjectKey = "projectKey", Uri = new Uri("https://sonarcloudi.io"), Organization = "Org"}; host.CredentialsForSharedConfig = new Credential("user", "pwd"); testSubject.ConnectCommand.Execute(new ConnectConfiguration(){UseSharedBinding = true}); @@ -254,9 +254,9 @@ public void ConnectionController_ConnectCommand_SharedConfig_AsksForCredentialsP var connectionProviderMock = new Mock(); var testSubject = new ConnectionController(this.serviceProvider, this.host, null, connectionProviderMock.Object, connectionWorkflowMock.Object); - host.SharedBindingConfig = new SharedBindingConfigModel { ProjectKey = "projectKey", Uri = "https://sonarcloudi.io", Organization = "Org"}; + host.SharedBindingConfig = new SharedBindingConfigModel { ProjectKey = "projectKey", Uri = new Uri("https://sonarcloudi.io"), Organization = "Org"}; var connectionInformation = - new ConnectionInformation(host.SharedBindingConfig.ServerUri, "user", "pwd".ToSecureString()) + new ConnectionInformation(host.SharedBindingConfig.Uri, "user", "pwd".ToSecureString()) { Organization = new SonarQubeOrganization(host.SharedBindingConfig.Organization, string.Empty) }; @@ -269,7 +269,7 @@ public void ConnectionController_ConnectCommand_SharedConfig_AsksForCredentialsP Times.Once); connectionProviderMock.Verify(x => x.GetConnectionInformation(It.Is(c => - c.ServerUri == host.SharedBindingConfig.ServerUri && c.Organization.Key == host.SharedBindingConfig.Organization)), + c.ServerUri == host.SharedBindingConfig.Uri && c.Organization.Key == host.SharedBindingConfig.Organization)), Times.Once); } @@ -292,7 +292,7 @@ private void TestDisabledSharedConfig(ConnectConfiguration config) SetUpOpenSolution(); var connectionProviderMock = new Mock(); host.SharedBindingConfig = new SharedBindingConfigModel - { ProjectKey = "projectKey", Uri = "https://sonarcloudi.io", Organization = "Org" }; + { ProjectKey = "projectKey", Uri = new Uri("https://sonarcloudi.io"), Organization = "Org" }; host.CredentialsForSharedConfig = new Credential("user", "pwd"); var expectedConnection = new ConnectionInformation(new Uri("https://127.0.0.0")); SetupConnectionProvider(connectionProviderMock, expectedConnection); diff --git a/src/Integration.UnitTests/MefServices/VsSessionHostTests.cs b/src/Integration.UnitTests/MefServices/VsSessionHostTests.cs index 9c4d74cdcb..96e7ba7e95 100644 --- a/src/Integration.UnitTests/MefServices/VsSessionHostTests.cs +++ b/src/Integration.UnitTests/MefServices/VsSessionHostTests.cs @@ -469,7 +469,7 @@ public void GetCredentialsForSharedConfig_CallsCredentialServiceOnlyWhenSharedCo var testSubject = CreateTestSubject(); var section = ConfigurableSectionController.CreateDefault(); sharedBindingConfigProviderMock.Setup(x => x.GetSharedBinding()) - .Returns(serverUri != null ? new SharedBindingConfigModel { Uri = serverUri} : null); + .Returns(serverUri != null ? new SharedBindingConfigModel { Uri = new Uri(serverUri)} : null); testSubject.SetActiveSection(section); var credential = new Credential("a"); credentialStoreServiceMock.Setup(x => x.ReadCredentials(It.IsAny())).Returns(credential); diff --git a/src/Integration.Vsix.UnitTests/Commands/ConnectedModeMenu/SaveSharedConnectionCommandTests.cs b/src/Integration.Vsix.UnitTests/Commands/ConnectedModeMenu/SaveSharedConnectionCommandTests.cs index d6db32d836..9395dbde8e 100644 --- a/src/Integration.Vsix.UnitTests/Commands/ConnectedModeMenu/SaveSharedConnectionCommandTests.cs +++ b/src/Integration.Vsix.UnitTests/Commands/ConnectedModeMenu/SaveSharedConnectionCommandTests.cs @@ -62,11 +62,12 @@ public void Invoke_Success_InvokesCorrectly() var organisation = new SonarQubeOrganization("organisationKey", "organisationName"); - var project = new BoundSonarQubeProject(new Uri("http://127.0.0.1:9000"), "projectKey", null, organization: organisation); + var serverUri = new Uri("http://127.0.0.1:9000"); + var project = new BoundSonarQubeProject(serverUri, "projectKey", null, organization: organisation); var bindingConfiguration = new BindingConfiguration(project, SonarLintMode.Connected, null); var sharedBindingConfigProvider = new Mock(); - sharedBindingConfigProvider.Setup(x => x.SaveSharedBinding(It.Is(y => y.Uri == "http://127.0.0.1:9000/" && y.ProjectKey == "projectKey" && y.Organization == "organisationKey"))).Returns("some Path"); + sharedBindingConfigProvider.Setup(x => x.SaveSharedBinding(It.Is(y => y.Uri == serverUri && y.ProjectKey == "projectKey" && y.Organization == "organisationKey"))).Returns("some Path"); var messageBox = new Mock(); @@ -75,7 +76,7 @@ public void Invoke_Success_InvokesCorrectly() testSubject.Invoke(command, null); - sharedBindingConfigProvider.Verify(x => x.SaveSharedBinding(It.Is(y => y.Uri == "http://127.0.0.1:9000/" && y.ProjectKey == "projectKey" && y.Organization == "organisationKey")), Times.Once); + sharedBindingConfigProvider.Verify(x => x.SaveSharedBinding(It.Is(y => y.Uri == serverUri && y.ProjectKey == "projectKey" && y.Organization == "organisationKey")), Times.Once); messageBox.Verify(mb => mb.Show(string.Format(Strings.SaveSharedConnectionCommand_SaveSuccess_Message, "some Path"), Strings.SaveSharedConnectionCommand_SaveSuccess_Caption, MessageBoxButton.OK, MessageBoxImage.Information), Times.Once); } diff --git a/src/Integration.Vsix/Commands/ConnectedModeMenu/SaveSharedConnectionCommand.cs b/src/Integration.Vsix/Commands/ConnectedModeMenu/SaveSharedConnectionCommand.cs index 606b01d92f..6b6b3f27cb 100644 --- a/src/Integration.Vsix/Commands/ConnectedModeMenu/SaveSharedConnectionCommand.cs +++ b/src/Integration.Vsix/Commands/ConnectedModeMenu/SaveSharedConnectionCommand.cs @@ -54,7 +54,7 @@ protected override void InvokeInternal() { var project = configurationProvider.GetConfiguration().Project; - var sharedBindingConfig = new SharedBindingConfigModel { ProjectKey = project.ProjectKey, Uri = project.ServerUri.ToString(), Organization = project.Organization?.Key }; + var sharedBindingConfig = new SharedBindingConfigModel { ProjectKey = project.ProjectKey, Uri = project.ServerUri, Organization = project.Organization?.Key }; var savePath = sharedBindingConfigProvider.SaveSharedBinding(sharedBindingConfig); diff --git a/src/Integration/Connection/ConnectionController.cs b/src/Integration/Connection/ConnectionController.cs index abc1a618cc..fe17e8e41b 100644 --- a/src/Integration/Connection/ConnectionController.cs +++ b/src/Integration/Connection/ConnectionController.cs @@ -122,7 +122,7 @@ private void OnConnect(ConnectConfiguration configuration) if (useSharedConfig && sharedConfig != null) { - connectionInfo = new ConnectionInformation(sharedConfig.ServerUri, + connectionInfo = new ConnectionInformation(sharedConfig.Uri, credentials?.Username, credentials?.Password?.ToSecureString()) { diff --git a/src/Integration/MefServices/VsSessionHost.cs b/src/Integration/MefServices/VsSessionHost.cs index 082d22fc62..6f959142f6 100644 --- a/src/Integration/MefServices/VsSessionHost.cs +++ b/src/Integration/MefServices/VsSessionHost.cs @@ -126,7 +126,7 @@ public Credential GetCredentialsForSharedConfig() { return SharedBindingConfig == null ? null - : credentialStoreService.ReadCredentials(SharedBindingConfig.ServerUri); + : credentialStoreService.ReadCredentials(SharedBindingConfig.Uri); } public ISectionController ActiveSection { get; private set; }