]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19086 Compare DB groups and GitHub teams, update DB membership accordingly
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Fri, 5 May 2023 09:32:42 +0000 (11:32 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:14 +0000 (20:03 +0000)
server/sonar-auth-github/src/main/java/org/sonar/auth/github/GithubTeamConverter.java
server/sonar-auth-github/src/test/java/org/sonar/auth/github/GithubTeamConverterTest.java
server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDto.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java

index be7bc9cd432a6b22a0d453708ba78e110faf5317..39bdff80d910949f42552db5b2a2fbae129d14dc 100644 (file)
@@ -31,7 +31,11 @@ public class GithubTeamConverter {
   }
 
   public static String toGroupName(GsonTeam team) {
-    return team.getOrganizationId() + "/" + team.getId();
+    return toGroupName(team.getOrganizationId(), team.getId());
+  }
+
+  public static String toGroupName(String organization, String groupName) {
+    return organization + "/" + groupName;
   }
 
   public static Optional<String> extractOrganizationName(String groupName) {
index 2f7ad3288be496ed787ad329da9ffaeacdde3d45..5b96e88528788437e26c216a70f7b1e7ee0ec548 100644 (file)
@@ -27,11 +27,16 @@ import static org.assertj.core.api.Assertions.assertThat;
 public class GithubTeamConverterTest {
 
   @Test
-  public void toGroup_creates_correct_groupName() {
+  public void toGroupName_withGsonTeam_returnsCorrectGroupName() {
     GsonTeam team = new GsonTeam("team-1", new GsonTeam.GsonOrganization("Org1"));
     assertThat(GithubTeamConverter.toGroupName(team)).isEqualTo("Org1/team-1");
   }
 
+  @Test
+  public void toGroupName_withGroupAndName_returnsCorrectGroupName() {
+    assertThat(GithubTeamConverter.toGroupName("Org1", "team-1")).isEqualTo("Org1/team-1");
+  }
+
   @Test
   public void extractOrganizationName_whenNameIsCorrect_extractsOrganizationName() {
     assertThat(GithubTeamConverter.extractOrganizationName("Org1/team1")).isEqualTo(Optional.of("Org1"));
index c6a6b80dcc8cfab7e10a6e0ecb5dc13f7c85a5f9..0a1c7e536d11e6f0b4b1df7b6718dc841c5de8a6 100644 (file)
@@ -50,6 +50,7 @@ public final class SqTables {
     "es_queue",
     "events",
     "event_component_changes",
+    "external_groups",
     "file_sources",
     "groups",
     "groups_users",
index 03d29082fbf0e55bd5747e9b7801ac5a0d462b73..d66f42a2d5241158053c80511462e14467cf96e7 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.db.user;
 
+import java.util.Objects;
 import javax.annotation.Nullable;
 
 public record ExternalGroupDto(String groupUuid, String externalId, String externalIdentityProvider, @Nullable String name) {
@@ -26,4 +27,8 @@ public record ExternalGroupDto(String groupUuid, String externalId, String exter
   public ExternalGroupDto(String groupUuid, String externalId, String externalIdentityProvider) {
     this(groupUuid, externalId, externalIdentityProvider, null);
   }
+
+  public String getNameOrThrow() {
+    return Objects.requireNonNull(name);
+  }
 }
index e3bba169d92b8ab63e3fb9c886e04ef94fab403b..45a2815630bd2d3a3b7eb9f1bdfe5d4d88f41947 100644 (file)
@@ -44,6 +44,7 @@ import org.sonar.db.scim.ScimUserDto;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Arrays.stream;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
 import static org.apache.commons.lang.math.RandomUtils.nextLong;
 import static org.sonar.db.permission.GlobalPermission.ADMINISTER;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
@@ -157,6 +158,11 @@ public class UserDbTester {
     return dto;
   }
 
+  public void markGroupAsGithubManaged(String groupUuid) {
+    db.getDbClient().externalGroupDao().insert(db.getSession(), new ExternalGroupDto(groupUuid, randomAlphanumeric(20), "github"));
+    db.commit();
+  }
+
   public ScimGroupDto insertScimGroup(GroupDto dto) {
     ScimGroupDto result = db.getDbClient().scimGroupDao().enableScimForGroup(db.getSession(), dto.getUuid());
     db.commit();