]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20392 Add events to audit logs when GH permissions mapping change
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Mon, 11 Sep 2023 09:35:03 +0000 (11:35 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 15 Sep 2023 20:03:05 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubPermissionsMappingDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditPersister.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/NoOpAuditPersister.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GithubPermissionsMappingNewValue.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java

index 0eb588de39dc1be83f3ecbdc3c062e3c93148ec0..33839955dd7d64c56c1b7e8cf356dd430ca3562e 100644 (file)
@@ -22,17 +22,27 @@ package org.sonar.db.provisioning;
 import java.util.Set;
 import org.junit.Rule;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.audit.AuditPersister;
+import org.sonar.db.audit.model.GithubPermissionsMappingNewValue;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
 public class GithubPermissionsMappingDaoIT {
 
   private static final String MAPPING_UUID = "uuid";
 
+  private final AuditPersister auditPersister = mock();
+
   @Rule
-  public final DbTester db = DbTester.create();
+  public final DbTester db = DbTester.create(auditPersister);
+
+  private final ArgumentCaptor<GithubPermissionsMappingNewValue> newValueCaptor = ArgumentCaptor.forClass(GithubPermissionsMappingNewValue.class);
 
   private final DbSession dbSession = db.getSession();
 
@@ -50,6 +60,10 @@ public class GithubPermissionsMappingDaoIT {
     assertThat(savedMapping.uuid()).isEqualTo(githubPermissionsMappingDto.uuid());
     assertThat(savedMapping.githubRole()).isEqualTo(githubPermissionsMappingDto.githubRole());
     assertThat(savedMapping.sonarqubePermission()).isEqualTo(githubPermissionsMappingDto.sonarqubePermission());
+
+    verify(auditPersister).addGithubPermissionsMapping(eq(dbSession), newValueCaptor.capture());
+    assertThat(newValueCaptor.getValue().getGithubRole()).isEqualTo(githubPermissionsMappingDto.githubRole());
+    assertThat(newValueCaptor.getValue().getSonarqubePermission()).isEqualTo(githubPermissionsMappingDto.sonarqubePermission());
   }
 
   @Test
index 870701b42376b73802878fb00f0b9c5d01dcccc2..401a51a80e153c2830a202b18879167b4762adea 100644 (file)
@@ -25,6 +25,7 @@ import org.sonar.db.audit.model.AbstractEditorNewValue;
 import org.sonar.db.audit.model.ComponentKeyNewValue;
 import org.sonar.db.audit.model.ComponentNewValue;
 import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue;
+import org.sonar.db.audit.model.GithubPermissionsMappingNewValue;
 import org.sonar.db.audit.model.GroupPermissionNewValue;
 import org.sonar.db.audit.model.LicenseNewValue;
 import org.sonar.db.audit.model.PermissionTemplateNewValue;
@@ -102,6 +103,10 @@ public interface AuditPersister {
 
   void deleteGroupFromPermissionTemplate(DbSession dbSession, PermissionTemplateNewValue newValue);
 
+  void addGithubPermissionsMapping(DbSession dbSession, GithubPermissionsMappingNewValue newValue);
+
+  void deleteGithubPermissionsMapping(DbSession dbSession, GithubPermissionsMappingNewValue deletedValue);
+
   void addQualityGateEditor(DbSession dbSession, AbstractEditorNewValue newValue);
 
   void deleteQualityGateEditor(DbSession dbSession, AbstractEditorNewValue newValue);
index eefd5f7ca15780910f1e3f4697c343b91b997e71..755478ad72a0a48ee9b4d819bfefae9d36755549 100644 (file)
@@ -25,6 +25,7 @@ import org.sonar.db.audit.model.ComponentKeyNewValue;
 import org.sonar.db.audit.model.ComponentNewValue;
 import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue;
 import org.sonar.db.audit.model.AbstractEditorNewValue;
+import org.sonar.db.audit.model.GithubPermissionsMappingNewValue;
 import org.sonar.db.audit.model.GroupPermissionNewValue;
 import org.sonar.db.audit.model.LicenseNewValue;
 import org.sonar.db.audit.model.PermissionTemplateNewValue;
@@ -191,6 +192,16 @@ public class NoOpAuditPersister implements AuditPersister {
     // no op
   }
 
+  @Override
+  public void addGithubPermissionsMapping(DbSession dbSession, GithubPermissionsMappingNewValue newValue) {
+    // no op
+  }
+
+  @Override
+  public void deleteGithubPermissionsMapping(DbSession dbSession, GithubPermissionsMappingNewValue deletedValue) {
+    // no op
+  }
+
   @Override
   public void addQualityGateEditor(DbSession dbSession, AbstractEditorNewValue newValue) {
     // no op
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GithubPermissionsMappingNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/GithubPermissionsMappingNewValue.java
new file mode 100644 (file)
index 0000000..9764e63
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.db.audit.model;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class GithubPermissionsMappingNewValue extends NewValue {
+
+  private final String githubRole;
+  private final String sonarqubePermission;
+
+  public GithubPermissionsMappingNewValue(String githubRole, String sonarqubePermission) {
+    this.githubRole = githubRole;
+    this.sonarqubePermission = sonarqubePermission;
+  }
+
+  @VisibleForTesting
+  public String getGithubRole() {
+    return githubRole;
+  }
+
+  public String getSonarqubePermission() {
+    return sonarqubePermission;
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder sb = new StringBuilder("{");
+    addField(sb, "\"githubRole\": ", this.githubRole, true);
+    addField(sb, "\"sonarqubePermissions\": ", this.sonarqubePermission, true);
+    endString(sb);
+    return sb.toString();
+  }
+
+}
index fad64ad92c139dd4e15376a5237b5a2acb82e00c..b97b3ea421dc8758f4fb817c1eef62b6a5b3bceb 100644 (file)
@@ -22,15 +22,34 @@ package org.sonar.db.provisioning;
 import java.util.Set;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.audit.AuditPersister;
+import org.sonar.db.audit.model.GithubPermissionsMappingNewValue;
 
 public class GithubPermissionsMappingDao implements Dao {
 
+  private final AuditPersister auditPersister;
+
+  public GithubPermissionsMappingDao(AuditPersister auditPersister) {
+    this.auditPersister = auditPersister;
+  }
+
   public Set<GithubPermissionsMappingDto> findAll(DbSession dbSession) {
     return mapper(dbSession).selectAll();
   }
 
   public void insert(DbSession dbSession, GithubPermissionsMappingDto githubPermissionsMappingDto) {
     mapper(dbSession).insert(githubPermissionsMappingDto);
+    auditPersister.addGithubPermissionsMapping(dbSession, toNewValueForAuditLogs(githubPermissionsMappingDto));
+  }
+
+  public void delete(DbSession dbSession, GithubPermissionsMappingDto githubPermissionsMappingDto) {
+    // TODO SONAR-20397
+    auditPersister.deleteGithubPermissionsMapping(dbSession, toNewValueForAuditLogs(githubPermissionsMappingDto));
+  }
+
+  private static GithubPermissionsMappingNewValue toNewValueForAuditLogs(GithubPermissionsMappingDto githubPermissionsMappingDto) {
+    return new GithubPermissionsMappingNewValue(githubPermissionsMappingDto.githubRole(),
+      githubPermissionsMappingDto.sonarqubePermission());
   }
 
   private static GithubPermissionsMappingMapper mapper(DbSession session) {