Skip to content

Commit

Permalink
Remove string uri from SharedBindingConfig (#5013)
Browse files Browse the repository at this point in the history
Fixes #5009
  • Loading branch information
georgii-borovinskikh-sonarsource authored Nov 3, 2023
1 parent f92be6a commit bdaf199
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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]
Expand Down Expand Up @@ -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()
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectedMode/Migration/ConnectedModeMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 11 additions & 5 deletions src/ConnectedMode/Shared/SharedBindingConfigFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -59,22 +59,28 @@ internal SharedBindingConfigFileProvider(ILogger logger, IFileSystem fileSystem)

public SharedBindingConfigModel ReadSharedBindingConfigFile(string filePath)
{
SharedBindingConfigModel result = null;

try
{
result = JsonConvert.DeserializeObject<SharedBindingConfigModel>(fileSystem.File.ReadAllText(filePath));
var result = JsonConvert.DeserializeObject<SharedBindingConfigModel>(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)
Expand Down
16 changes: 1 addition & 15 deletions src/ConnectedMode/Shared/SharedBindingConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,15 @@ 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; }

[JsonProperty("ProjectKey")]
public string ProjectKey { get; set; }

[JsonIgnore]
public Uri ServerUri { get; private set; }

public bool IsSonarCloud() => !string.IsNullOrWhiteSpace(Organization);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Integration.UnitTests/Connection/ConnectControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void ConnectionController_ConnectCommand_SharedConfigAndCredentialsPresen
var connectionProviderMock = new Mock<IConnectionInformationProvider>();
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});
Expand All @@ -254,9 +254,9 @@ public void ConnectionController_ConnectCommand_SharedConfig_AsksForCredentialsP
var connectionProviderMock = new Mock<IConnectionInformationProvider>();
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)
};
Expand All @@ -269,7 +269,7 @@ public void ConnectionController_ConnectCommand_SharedConfig_AsksForCredentialsP
Times.Once);
connectionProviderMock.Verify(x =>
x.GetConnectionInformation(It.Is<ConnectionInformation>(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);
}

Expand All @@ -292,7 +292,7 @@ private void TestDisabledSharedConfig(ConnectConfiguration config)
SetUpOpenSolution();
var connectionProviderMock = new Mock<IConnectionInformationProvider>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TargetUri>())).Returns(credential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISharedBindingConfigProvider>();
sharedBindingConfigProvider.Setup(x => x.SaveSharedBinding(It.Is<SharedBindingConfigModel>(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<SharedBindingConfigModel>(y => y.Uri == serverUri && y.ProjectKey == "projectKey" && y.Organization == "organisationKey"))).Returns("some Path");

var messageBox = new Mock<IMessageBox>();

Expand All @@ -75,7 +76,7 @@ public void Invoke_Success_InvokesCorrectly()

testSubject.Invoke(command, null);

sharedBindingConfigProvider.Verify(x => x.SaveSharedBinding(It.Is<SharedBindingConfigModel>(y => y.Uri == "http://127.0.0.1:9000/" && y.ProjectKey == "projectKey" && y.Organization == "organisationKey")), Times.Once);
sharedBindingConfigProvider.Verify(x => x.SaveSharedBinding(It.Is<SharedBindingConfigModel>(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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Integration/Connection/ConnectionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/MefServices/VsSessionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit bdaf199

Please sign in to comment.