Skip to content

Commit

Permalink
SLVS-1629 Prevent SSESessionManager creating session for SonarCloud (#…
Browse files Browse the repository at this point in the history
…5832)

[SLVS-1629](https://sonarsource.atlassian.net/browse/SLVS-1629)

SSESessionManager does not support SonarCloud connections and currently
throws an Exception when project is connected to SonarCloud.

[SLVS-1629]:
https://sonarsource.atlassian.net/browse/SLVS-1629?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
vnaskos-sonar authored Nov 14, 2024
1 parent e6043e8 commit e85dc98
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
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

0 comments on commit e85dc98

Please sign in to comment.