]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18708 add 'managed' property for users in the telemetry
authorPierre <pierre.guillot@sonarsource.com>
Thu, 9 Mar 2023 10:23:16 +0000 (11:23 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:07 +0000 (20:04 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTelemetryDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java
server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java

index 1b9d348c1fba547f4e24beb4ba13f591f41be79b..ffca27d756bb345e80375e4f9cf09609c0efb67f 100644 (file)
@@ -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<UserTelemetryDto> 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<UserTelemetryDto> 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);
index 00973620385ea49b620df11fd6d1a193eb5fe8b1..35bcf53cfe29926d9616e5e28f0a5cd0a2690b80 100644 (file)
@@ -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;
+  }
 }
index 2f5ae52d2ec04327a553e17eb5028278818bcdb5..bc92cc5234ac0675897abc171f176e07235c3986 100644 (file)
             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
     </select>
 
index 62ae8e49d2c91f28da728c889cb494f3601738cf..350cbb4943fcf63ecf5a2301dcb2e483594b911f 100644 (file)
@@ -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();
       });
index 59c2f805cd4be0dedf4e377c15b99183c5d6662f..647cac10aec9670988a52a47d141c62ae9e51c61 100644 (file)
@@ -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<UserTelemetryDto> 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());
   }