]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22862 Synchronisation supports custom roles mapping
authorNolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com>
Wed, 28 Aug 2024 08:35:44 +0000 (10:35 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 29 Aug 2024 20:02:47 +0000 (20:02 +0000)
server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java
server/sonar-alm-client/src/test/resources/org/sonar/alm/client/gitlab/project-members-full-response.json
server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonMemberRole.java [new file with mode: 0644]
server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonProjectMember.java

index afa35587b88fde93d90e01786753246a1f04ff6b..90bda3316107d9f5e2fe4ca23f10d7af865cdc42 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
+import javax.annotation.Nullable;
 import okhttp3.mockwebserver.MockResponse;
 import okhttp3.mockwebserver.MockWebServer;
 import okhttp3.mockwebserver.RecordedRequest;
@@ -42,6 +43,7 @@ import org.sonar.api.testfixtures.log.LogTester;
 import org.sonar.auth.gitlab.GsonGroup;
 import org.sonar.auth.gitlab.GsonProjectMember;
 import org.sonar.auth.gitlab.GsonUser;
+import org.sonar.auth.gitlab.GsonMemberRole;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -57,7 +59,7 @@ public class GitlabApplicationClientTest {
   @Rule
   public LogTester logTester = new LogTester();
 
-  private GitlabPaginatedHttpClient gitlabPaginatedHttpClient = mock();
+  private final GitlabPaginatedHttpClient gitlabPaginatedHttpClient = mock();
 
   private final MockWebServer server = new MockWebServer();
   private GitlabApplicationClient underTest;
@@ -683,7 +685,6 @@ public class GitlabApplicationClientTest {
   public void getAllProjectMembers_whenCallIsInError_rethrows() {
     String token = "token-toto";
     GitlabToken gitlabToken = new GitlabToken(token);
-    List<GsonProjectMember> expectedProjectMembers = expectedProjectMembers();
     when(gitlabPaginatedHttpClient.get(eq(gitlabUrl), eq(gitlabToken), eq("/projects/42/members/all"), any())).thenThrow(new IllegalStateException("exception"));
 
     assertThatIllegalStateException()
@@ -692,13 +693,14 @@ public class GitlabApplicationClientTest {
   }
 
   private static List<GsonProjectMember> expectedProjectMembers() {
-    GsonProjectMember user1 = createGsonProjectMember(12818153, 5);
-    GsonProjectMember user2 = createGsonProjectMember(22330087, 50);
-    return List.of(user1, user2);
+    GsonProjectMember user1 = createGsonProjectMember(12818153, 5, null);
+    GsonProjectMember user2 = createGsonProjectMember(22330087, 50, null);
+    GsonProjectMember user3 = createGsonProjectMember(20824381, 40, new GsonMemberRole("custom-role"));
+    return List.of(user1, user2, user3);
   }
 
-  private static GsonProjectMember createGsonProjectMember(int id, int accessLevel) {
-    return new GsonProjectMember(id, accessLevel);
+  private static GsonProjectMember createGsonProjectMember(int id, int accessLevel, @Nullable GsonMemberRole gsonMemberRole) {
+    return new GsonProjectMember(id, accessLevel, gsonMemberRole);
   }
 
   private static String getResponseContent(String path) throws IOException {
index aed1ada78931bd680760a263cf4e7979cc484dd2..a4a1992b8a0a32e4642f51f76cdd97512d79ec55 100644 (file)
     "created_at": "2024-08-05T14:38:37.647Z",
     "expires_at": "2024-09-04",
     "membership_state": "active"
+  },
+  {
+    "id": 20824381,
+    "username": "nolwenncadic",
+    "name": "Nolwenn Cadic",
+    "state": "active",
+    "locked": false,
+    "avatar_url": "https://secure.gravatar.com/avatar/7c80cccd6d4e547a2681964e8950228d6c97b9b755f2c348516b80da10d4140d?s=80&d=identicon",
+    "web_url": "https://gitlab.com/nolwenncadic",
+    "access_level": 40,
+    "created_at": "2024-08-26T14:42:45.013Z",
+    "created_by": {
+      "id": 14799915,
+      "username": "Nolwenn-cadic-sonarsource",
+      "name": "Nolwenn Cadic",
+      "state": "active",
+      "locked": false,
+      "avatar_url": "https://secure.gravatar.com/avatar/3b2216084856683e6e25bb55b824af42fb5ec5da97cd524e17599023a1e64adc?s=80&d=identicon",
+      "web_url": "https://gitlab.com/Nolwenn-cadic-sonarsource"
+    },
+    "expires_at": null,
+    "membership_state": "active",
+    "member_role": {
+      "id": 1002974,
+      "group_id": 6164984,
+      "name": "custom-role",
+      "description": "custom role",
+      "base_access_level": 40,
+      "admin_cicd_variables": false,
+      "admin_compliance_framework": false,
+      "admin_group_member": false,
+      "admin_integrations": false,
+      "admin_merge_request": false,
+      "admin_push_rules": false,
+      "admin_runners": false,
+      "admin_terraform_state": false,
+      "admin_vulnerability": false,
+      "admin_web_hook": false,
+      "archive_project": false,
+      "manage_deploy_tokens": false,
+      "manage_group_access_tokens": false,
+      "manage_merge_request_settings": false,
+      "manage_project_access_tokens": false,
+      "manage_security_policy_link": false,
+      "read_code": true,
+      "read_crm_contact": false,
+      "read_dependency": false,
+      "read_runners": false,
+      "read_vulnerability": false,
+      "remove_group": false,
+      "remove_project": false
+    }
   }
 ]
diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonMemberRole.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonMemberRole.java
new file mode 100644 (file)
index 0000000..6f5334b
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.auth.gitlab;
+
+import com.google.gson.annotations.SerializedName;
+
+public record GsonMemberRole(@SerializedName("name") String name) {
+}
index 14a5965c6670e9de7aed7454d0b5de0739876145..1c4972a2fcb76605fc4c7999e589dc67894735b2 100644 (file)
 package org.sonar.auth.gitlab;
 
 import com.google.gson.annotations.SerializedName;
+import javax.annotation.Nullable;
 
 public record GsonProjectMember (
   @SerializedName("id")
   long id,
   @SerializedName("access_level")
-  int accessLevel
+  int accessLevel,
+  @SerializedName("member_role")
+  @Nullable
+  GsonMemberRole gsonMemberRole
 ){
 }