Skip to content
Closed
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 @@ -17,7 +17,6 @@
package org.sonarsource.sonarqube.mcp.analytics;

import java.util.UUID;
import javax.annotation.Nullable;

public class AnalyticsService {

Expand Down Expand Up @@ -55,21 +54,20 @@ private static String resolveTransportMode(boolean isHttpEnabled, boolean isHttp
* @param toolExecutionDurationMs duration of the tool execution in milliseconds
* @param isSuccessful whether the tool execution completed without error
*/
public void notifyToolInvoked(String toolName, @Nullable String organizationUuidV4, @Nullable String sqsInstallationId, @Nullable String userUuid,
@Nullable String callingAgentName, @Nullable String callingAgentVersion, long toolExecutionDurationMs, boolean isSuccessful) {
public void notifyToolInvoked(String toolName, ConnectionContext connectionContext, long toolExecutionDurationMs, boolean isSuccessful) {
var connectionType = isSonarCloud ? CONNECTION_TYPE_SQC : CONNECTION_TYPE_SQS;

var event = new McpToolInvokedEvent(
UUID.randomUUID().toString(),
toolName,
connectionType,
isSonarCloud ? organizationUuidV4 : null,
isSonarCloud ? null : sqsInstallationId,
userUuid,
isSonarCloud ? connectionContext.getOrganizationUuidV4() : null,
isSonarCloud ? null : connectionContext.getSqsInstallationId(),
connectionContext.getUserUuid(),
mcpServerId,
transportMode,
callingAgentName,
callingAgentVersion,
connectionContext.getCallingAgentName(),
connectionContext.getCallingAgentVersion(),
toolExecutionDurationMs,
isSuccessful
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.CheckForNull;
import org.sonarsource.sonarqube.mcp.log.McpLogger;
import org.sonarsource.sonarqube.mcp.serverapi.ServerApiHelper;
@SuppressWarnings("java:S1075")

public class UsersApi {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.sonarsource.sonarqube.mcp.analytics;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

Expand Down Expand Up @@ -108,31 +110,18 @@ void it_should_ignore_org_uuid_for_sqs_connection() {
assertThat(event.sqsInstallationId()).isEqualTo("install-id");
}

@Test
void it_should_resolve_transport_mode_as_stdio_when_http_disabled() {
var service = new AnalyticsService(mockClient, "server-id", false, false, false);
service.notifyToolInvoked("tool", null, null, null, null, null, 0L, true);
var captor = ArgumentCaptor.forClass(McpToolInvokedEvent.class);
verify(mockClient).postEvent(captor.capture());
assertThat(captor.getValue().transportMode()).isEqualTo("stdio");
}

@Test
void it_should_resolve_transport_mode_as_http_when_http_enabled_without_tls() {
var service = new AnalyticsService(mockClient, "server-id", true, false, false);
service.notifyToolInvoked("tool", null, null, null, null, null, 0L, true);
var captor = ArgumentCaptor.forClass(McpToolInvokedEvent.class);
verify(mockClient).postEvent(captor.capture());
assertThat(captor.getValue().transportMode()).isEqualTo("http");
}

@Test
void it_should_resolve_transport_mode_as_https_when_http_and_tls_enabled() {
var service = new AnalyticsService(mockClient, "server-id", true, true, false);
@ParameterizedTest
@CsvSource({
"false, false, stdio",
"true, false, http",
"true, true, https",
})
void it_should_resolve_transport_mode(boolean httpEnabled, boolean tlsEnabled, String expectedTransportMode) {
var service = new AnalyticsService(mockClient, "server-id", httpEnabled, tlsEnabled, false);
service.notifyToolInvoked("tool", null, null, null, null, null, 0L, true);
var captor = ArgumentCaptor.forClass(McpToolInvokedEvent.class);
verify(mockClient).postEvent(captor.capture());
assertThat(captor.getValue().transportMode()).isEqualTo("https");
assertThat(captor.getValue().transportMode()).isEqualTo(expectedTransportMode);
}

@Test
Expand Down
Loading