Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLVS-1629 Prevent SSESessionManager creating session for SonarCloud #5832

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.Threading.Tasks;
using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Binding;
Expand Down Expand Up @@ -89,10 +87,25 @@ public void CreateSessionIfInConnectedMode_WhenInStandaloneModeOnCreation_DoesNo
testScope.SSESessionFactoryMock.Verify(factory => factory.Create(DefaultProjectKey, It.IsAny<OnSessionFailedAsync>()), Times.Never);
}

[TestMethod]
public void CreateSessionIfInConnectedMode_WhenConnectedToSonarCloud_DoesNotCreateSession()
{
var bindingConfig = TestScope.CreateConnectedModeSonarCloudBindingConfiguration(DefaultProjectKey);

var testScope = new TestScope(bindingConfig);

var _ = testScope.CreateTestSubject();
var testSubject = testScope.CreateTestSubject();

testSubject.CreateSessionIfInConnectedMode(bindingConfig);

testScope.SSESessionFactoryMock.Verify(factory => factory.Create(DefaultProjectKey, It.IsAny<OnSessionFailedAsync>()), Times.Never);
}

[TestMethod]
public void CreateSessionIfInConnectedMode_WhenInConnectedModeOnCreation_CreatesSession()
{
var bindingConfig = TestScope.CreateConnectedModeBindingConfiguration(DefaultProjectKey);
var bindingConfig = TestScope.CreateConnectedModeSonarQubeBindingConfiguration(DefaultProjectKey);

var testScope = new TestScope(bindingConfig);
var sessionMock = testScope.SetUpSSEFactoryToReturnNoOpSSESession(DefaultProjectKey);
Expand Down Expand Up @@ -158,7 +171,7 @@ public void OnSolutionChanged_WhenChangesFromConnectedToConnected_CancelsSession
[DataTestMethod]
public async Task OnSessionFailed_CancelsSessionAndStartsNewOne()
{
var bindingConfig = TestScope.CreateConnectedModeBindingConfiguration(DefaultProjectKey);
var bindingConfig = TestScope.CreateConnectedModeSonarQubeBindingConfiguration(DefaultProjectKey);

var testScope = new TestScope(bindingConfig);

Expand Down Expand Up @@ -256,11 +269,23 @@ public SSESessionManager CreateTestSubject()
Mock.Of<ILogger>());
}

public static BindingConfiguration CreateConnectedModeBindingConfiguration(string projectKey)
public static BindingConfiguration CreateConnectedModeSonarQubeBindingConfiguration(string projectKey)
{
var sonarQube = new ServerConnection.SonarQube(new Uri("http://localhost"));
return CreateConnectedModeBindingConfiguration(projectKey, sonarQube);
}

public static BindingConfiguration CreateConnectedModeSonarCloudBindingConfiguration(string projectKey)
{
var sonarCloud = new ServerConnection.SonarCloud(projectKey);
return CreateConnectedModeBindingConfiguration(projectKey, sonarCloud);
}

private static BindingConfiguration CreateConnectedModeBindingConfiguration(string projectKey, ServerConnection serverConnection)
{
var randomString = Guid.NewGuid().ToString();
var bindingConfiguration = new BindingConfiguration(
new BoundServerProject(randomString, projectKey, new ServerConnection.SonarQube(new Uri("http://localhost"))),
new BoundServerProject(randomString, projectKey, serverConnection),
SonarLintMode.Connected,
randomString);
return bindingConfiguration;
Expand Down Expand Up @@ -298,7 +323,7 @@ public Mock<ISSESession> SetUpSSEFactoryToReturnNoOpSSESession(string projectKey

public void RaiseInConnectedModeEvent(string projectKey)
{
var openProjectEvent = CreateConnectedModeBindingConfiguration(projectKey);
var openProjectEvent = CreateConnectedModeSonarQubeBindingConfiguration(projectKey);
RaiseSolutionBindingEvent(openProjectEvent);
}

Expand Down
12 changes: 8 additions & 4 deletions src/ConnectedMode/ServerSentEvents/SSESessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Binding;
Expand All @@ -43,7 +41,7 @@ internal sealed class SSESessionManager : IDisposable
private bool disposed;

[ImportingConstructor]
public SSESessionManager(IActiveSolutionBoundTracker activeSolutionBoundTracker,
public SSESessionManager(IActiveSolutionBoundTracker activeSolutionBoundTracker,
ISSESessionFactory sseSessionFactory,
ILogger logger)
{
Expand Down Expand Up @@ -83,7 +81,7 @@ public void CreateSessionIfInConnectedMode(BindingConfiguration bindingConfigura
lock (syncRoot)
{
EndCurrentSession();

var isInConnectedMode = !bindingConfiguration.Equals(BindingConfiguration.Standalone);

if (!isInConnectedMode)
Expand All @@ -92,6 +90,12 @@ public void CreateSessionIfInConnectedMode(BindingConfiguration bindingConfigura
return;
}

if (bindingConfiguration.Project.ServerConnection is ServerConnection.SonarCloud)
{
logger.LogVerbose("[SSESessionManager] Not available for the current server connection");
return;
}

logger.LogVerbose("[SSESessionManager] In connected mode, creating session...");

currentSession = sseSessionFactory.Create(bindingConfiguration.Project.ServerProjectKey, OnSessionFailedAsync);
Expand Down