From 9d02b0639e617458d771c4a794db33e70cbb35a1 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 9 Mar 2023 11:23:16 +0100 Subject: [PATCH] SONAR-18708 add 'managed' property for users in the telemetry --- .../it/java/org/sonar/db/user/UserDaoIT.java | 29 +++++++++++++++++-- .../org/sonar/db/user/UserTelemetryDto.java | 12 ++++++++ .../org/sonar/db/user/UserMapper.xml | 4 ++- .../telemetry/TelemetryDataJsonWriter.java | 1 + .../TelemetryDataJsonWriterTest.java | 12 +++++--- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDaoIT.java index 1b9d348c1fb..ffca27d756b 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDaoIT.java @@ -35,6 +35,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.project.ProjectDto; +import org.sonar.db.scim.ScimUserDto; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -593,10 +594,11 @@ public class UserDaoIT { List result = underTest.selectUsersForTelemetry(db.getSession()); assertThat(result) - .extracting(UserTelemetryDto::getUuid, UserTelemetryDto::isActive, UserTelemetryDto::getLastConnectionDate, UserTelemetryDto::getLastSonarlintConnectionDate) + .extracting(UserTelemetryDto::getUuid, UserTelemetryDto::isActive, UserTelemetryDto::getLastConnectionDate, UserTelemetryDto::getLastSonarlintConnectionDate, UserTelemetryDto::getScimUuid) .containsExactlyInAnyOrder( - tuple(u1.getUuid(), u1.isActive(), u1.getLastConnectionDate(), u1.getLastSonarlintConnectionDate()), - tuple(u2.getUuid(), u2.isActive(), u2.getLastConnectionDate(), u2.getLastSonarlintConnectionDate())); + tuple(u1.getUuid(), u1.isActive(), u1.getLastConnectionDate(), u1.getLastSonarlintConnectionDate(), null), + tuple(u2.getUuid(), u2.isActive(), u2.getLastConnectionDate(), u2.getLastSonarlintConnectionDate(), null) + ); } @Test @@ -620,6 +622,22 @@ public class UserDaoIT { tuple(u2.getUuid(), u2.isActive(), 20_000_000_000L, u2.getLastSonarlintConnectionDate())); } + @Test + public void selectUserTelemetryWithScim() { + UserDto u1 = insertUser(true); + UserDto u2 = insertUser(false); + ScimUserDto scimUser1 = enableScimForUser(u1); + + List result = underTest.selectUsersForTelemetry(db.getSession()); + + assertThat(result) + .extracting(UserTelemetryDto::getUuid, UserTelemetryDto::isActive, UserTelemetryDto::getLastConnectionDate, UserTelemetryDto::getLastSonarlintConnectionDate, UserTelemetryDto::getScimUuid) + .containsExactlyInAnyOrder( + tuple(u1.getUuid(), u1.isActive(), u1.getLastConnectionDate(), u1.getLastSonarlintConnectionDate(), scimUser1.getScimUserUuid()), + tuple(u2.getUuid(), u2.isActive(), u2.getLastConnectionDate(), u2.getLastSonarlintConnectionDate(), null) + ); + } + private UserDto insertActiveUser() { return insertUser(true); } @@ -630,6 +648,11 @@ public class UserDaoIT { return dto; } + + private ScimUserDto enableScimForUser(UserDto userDto) { + return dbClient.scimUserDao().enableScimForUser(db.getSession(), userDto.getUuid()); + } + private UserGroupDto insertUserGroup(UserDto user) { GroupDto group = newGroupDto().setName(randomAlphanumeric(30)); dbClient.groupDao().insert(session, group); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTelemetryDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTelemetryDto.java index 00973620385..35bcf53cfe2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTelemetryDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTelemetryDto.java @@ -30,6 +30,8 @@ public class UserTelemetryDto { private Long lastConnectionDate = null; @Nullable private Long lastSonarlintConnectionDate = null; + @Nullable + private String scimUuid = null; public String getUuid() { return uuid; @@ -77,4 +79,14 @@ public class UserTelemetryDto { this.lastSonarlintConnectionDate = lastSonarlintConnectionDate; return this; } + + public UserTelemetryDto setScimUuid(@Nullable String scimUuid) { + this.scimUuid = scimUuid; + return this; + } + + @Nullable + public String getScimUuid() { + return scimUuid; + } } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml index 2f5ae52d2ec..bc92cc5234a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -110,8 +110,10 @@ u.active as "active", u.external_identity_provider as "externalIdentityProvider", u.last_connection_date as "lastConnectionDate", - u.last_sonarlint_connection as "lastSonarlintConnectionDate" + u.last_sonarlint_connection as "lastSonarlintConnectionDate", + su.scim_uuid as "scimUuid" FROM users u + LEFT JOIN scim_users su on su.user_uuid = u.uuid ORDER BY u.uuid diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java index 62ae8e49d2c..350cbb4943f 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java @@ -118,6 +118,7 @@ public class TelemetryDataJsonWriter { if (user.getLastSonarlintConnectionDate() != null) { json.prop("lastSonarlintActivity", toUtc(user.getLastSonarlintConnectionDate())); } + json.prop("managed", user.getScimUuid() != null); json.endObject(); }); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java index 59c2f805cd4..647cac10aec 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java @@ -307,21 +307,24 @@ public class TelemetryDataJsonWriterTest { "status": "active", "identityProvider": "gitlab", "lastActivity": "1970-01-01T00:00:00+0000", - "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + "lastSonarlintActivity": "1970-01-01T00:00:00+0000", + "managed": true }, { "userUuid": "%s", "status": "inactive", "identityProvider": "gitlab", "lastActivity": "1970-01-01T00:00:00+0000", - "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + "lastSonarlintActivity": "1970-01-01T00:00:00+0000", + "managed": false }, { "userUuid": "%s", "status": "active", "identityProvider": "gitlab", "lastActivity": "1970-01-01T00:00:00+0000", - "lastSonarlintActivity": "1970-01-01T00:00:00+0000" + "lastSonarlintActivity": "1970-01-01T00:00:00+0000", + "managed": true } ] } @@ -482,7 +485,8 @@ public class TelemetryDataJsonWriterTest { private static List attachUsers() { return IntStream.range(0, 3) .mapToObj( - i -> new UserTelemetryDto().setUuid("uuid-" + i).setActive(i % 2 == 0).setLastConnectionDate(1L).setLastSonarlintConnectionDate(2L).setExternalIdentityProvider("gitlab")) + i -> new UserTelemetryDto().setUuid("uuid-" + i).setActive(i % 2 == 0).setLastConnectionDate(1L) + .setLastSonarlintConnectionDate(2L).setExternalIdentityProvider("gitlab").setScimUuid(i % 2 == 0 ? "scim-uuid-" + i : null)) .collect(Collectors.toList()); } -- 2.39.5