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-1455 Make Id of type URL #5698

Merged
merged 12 commits into from
Sep 25, 2024
Merged

Conversation

vnaskos-sonar
Copy link
Contributor

@vnaskos-sonar vnaskos-sonar commented Sep 20, 2024

SLVS-1455

What

Store the https://sonarcloud.io/organizations/OrganizationKey instead of the sonarcloud organization as connection ID.

Why

This is pre-work to remove the prefixes (sc| and sq|) from ConnectionIdHelper. Connections should be identifiable by their Id.

How

  1. ConnectionInfo is a UI model that is used to display information on the UI and act as a DTO.
    1.1. The ConnectionInfo.Id is either the sonarqube uri or the sonarcloud organization.
  2. ServerConnection is a backend model and it should not be used directly on the UI.
    1.1. The ServerConnection.Id is always a uri
  3. ...Adapter should be able to convert UI models to backend models (eg. ConnectionInfo to ServerConnection)
  4. ...Repository should only know/reference backend models and never UI models.

Expected results

  • connections.json: All connections Id should be URLs
    • sonarqube: http://localhost:9000
    • sonarcloud: https://sonarcloud.io/organizations/OrganizationKey
  • binding.config: ServerConnectionId should be the same as in connection.json
  • On the UI (Manage Binding, Manage connections, New connection) the sonarcloud entries should display only the organizationKey

@vnaskos-sonar vnaskos-sonar force-pushed the vn/id-to-uri branch 2 times, most recently from 77da67c to 2d08fca Compare September 23, 2024 11:14
@vnaskos-sonar vnaskos-sonar marked this pull request as ready for review September 23, 2024 13:18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the feedback contained in the comments.

Additionally, the id for SonarQube receives a trailing / (even if in the UI I did not add it), but the SonarCloud doesn't have it. Should we make them consistent?

image

src/ConnectedMode/SlCoreConnectionAdapter.cs Show resolved Hide resolved
src/ConnectedMode/ConnectionInfo.cs Outdated Show resolved Hide resolved
src/Core/Binding/ServerConnection.cs Show resolved Hide resolved
src/Core/CoreStrings.resx Show resolved Hide resolved
@@ -33,11 +33,12 @@ public record ConnectionInfo(string Id, ConnectionServerType ServerType)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add documentation for what the Id is expected to be and that it is different than the ServerConnection

src/ConnectedMode/SlCoreConnectionAdapter.cs Outdated Show resolved Hide resolved
src/Core.UnitTests/Binding/ServerConnectionTests.cs Outdated Show resolved Hide resolved
@@ -91,7 +91,11 @@ public Task<AdapterResponseWithData<List<OrganizationDisplay>>> GetOrganizations

try
{
var credentials = MapCredentials(credentialsModel?.ToICredentials());
if (credentialsModel == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this explicit check. If it is null, there will be a null reference exception that will be caught by the catch block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the credentialsModel is null the ToICredentials will throw a generic null exception. In this case I believe it's better to be proactive and add back the .? so the exception we get becomes specific to the lack of credentials.

@@ -37,7 +37,7 @@ public static ConnectionInfo From(ServerConnection serverConnection)
{
ServerConnection.SonarQube sonarQubeConnection => new ConnectionInfo(sonarQubeConnection.Id, ConnectionServerType.SonarQube),
ServerConnection.SonarCloud sonarCloudConnection => new ConnectionInfo(sonarCloudConnection.OrganizationKey, ConnectionServerType.SonarCloud),
_ => null
_ => throw new ArgumentException(Resources.UnexpectedConnectionType)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not technically possible to add a unit test for this case. Technically, this branch is unreachable as the ServerConnection constructor is private, so a ServerConnection can be either SonarQube or SonarCloud. It is also not possible to remove the default case without having a warning, as the C# compiler can't properly recognize exhaustive switch.

@@ -216,7 +216,7 @@ private static Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnec
new TransientSonarQubeConnectionDto(connectionInfo.Id, credentialsDto)),
ConnectionServerType.SonarCloud => Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto>.CreateRight(
new TransientSonarCloudConnectionDto(connectionInfo.Id, credentialsDto)),
_ => null
_ => throw new ArgumentException(Resources.UnexpectedConnectionType)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit test

@@ -230,7 +230,7 @@ private static Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnec
new TransientSonarQubeConnectionDto(sonarQubeConnection.Id, credentials)),
ServerConnection.SonarCloud sonarCloudConnection => Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto>.CreateRight(
new TransientSonarCloudConnectionDto(sonarCloudConnection.OrganizationKey, credentials)),
_ => null
_ => throw new ArgumentException(Resources.UnexpectedConnectionType)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit test


return connectionInfo.ServerType switch
{
ConnectionServerType.SonarQube => Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto>.CreateLeft(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider adding a comment here or in the caller method to point out that the organizationKey is expected to be null, when validating a transient connection (a connection that is not yet finished)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it would make it a bit more clear. However, I am not sure whether this is the correct place. This requirement comes from the SLCore API, which is already documented there, and on our end, we have unit tests to verify that this behavior is respected.

Copy link

@vnaskos-sonar vnaskos-sonar merged commit 9c754e2 into feature/new-connected-mode Sep 25, 2024
2 checks passed
@vnaskos-sonar vnaskos-sonar deleted the vn/id-to-uri branch September 25, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants