From b6375cec5bd52fb4d188467ccef4205de1287b50 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Tue, 26 Sep 2023 15:07:25 +0200 Subject: [PATCH] SONAR-20532 Add DELETE /github-permission-mappings/{githubRole} endpoint --- .../GithubPermissionsMappingDaoIT.java | 26 ++++++ .../GithubPermissionsMappingNewValue.java | 6 ++ .../GithubPermissionsMappingDao.java | 5 + .../GithubPermissionsMappingMapper.java | 2 + .../GithubPermissionsMappingMapper.xml | 5 + .../GithubPermissionsMappingServiceIT.java | 39 ++++++++ .../GithubPermissionsMappingService.java | 20 +++- .../DefaultGithubPermissionsController.java | 9 +- .../GithubPermissionsController.java | 6 ++ ...efaultGithubPermissionsControllerTest.java | 91 ++++++++++++++----- 10 files changed, 181 insertions(+), 28 deletions(-) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubPermissionsMappingDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubPermissionsMappingDaoIT.java index 4751e5d6ced..437893d1496 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubPermissionsMappingDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubPermissionsMappingDaoIT.java @@ -19,6 +19,7 @@ */ package org.sonar.db.provisioning; +import java.util.List; import java.util.Set; import org.junit.Rule; import org.junit.Test; @@ -32,6 +33,7 @@ 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; +import static org.sonar.db.audit.model.GithubPermissionsMappingNewValue.ALL_PERMISSIONS; public class GithubPermissionsMappingDaoIT { @@ -81,6 +83,30 @@ public class GithubPermissionsMappingDaoIT { assertThat(newValueCaptor.getValue().getSonarqubePermission()).isEqualTo("SQ_role"); } + @Test + public void deleteAllPermissionsForRole_deletesGithubPermissionsMappingDto() { + List role1Mappings = List.of( + new GithubPermissionsMappingDto("1", "GH_role_1", "SQ_role_1"), + new GithubPermissionsMappingDto("2", "GH_role_1", "SQ_role_2"), + new GithubPermissionsMappingDto("3", "GH_role_1", "SQ_role_3")); + + List role2Mappings = List.of( + new GithubPermissionsMappingDto("4", "GH_role_2", "SQ_role_1"), + new GithubPermissionsMappingDto("5", "GH_role_2", "SQ_role_2")); + + role1Mappings.forEach(mapping -> underTest.insert(dbSession, mapping)); + role2Mappings.forEach(mapping -> underTest.insert(dbSession, mapping)); + + underTest.deleteAllPermissionsForRole(dbSession, "GH_role_1"); + + Set savedGithubPermissionsMappings = underTest.findAll(dbSession); + assertThat(savedGithubPermissionsMappings).containsExactlyInAnyOrderElementsOf(role2Mappings); + + verify(auditPersister).deleteGithubPermissionsMapping(eq(dbSession), newValueCaptor.capture()); + assertThat(newValueCaptor.getValue().getGithubRole()).isEqualTo("GH_role_1"); + assertThat(newValueCaptor.getValue().getSonarqubePermission()).isEqualTo(ALL_PERMISSIONS); + } + @Test public void findAll_shouldReturnAllGithubOrganizationGroup() { GithubPermissionsMappingDto mapping1 = new GithubPermissionsMappingDto(MAPPING_UUID, "GH_role", "SQ_role"); 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 index 9764e63b7ff..39c3d742742 100644 --- 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 @@ -23,6 +23,8 @@ import com.google.common.annotations.VisibleForTesting; public class GithubPermissionsMappingNewValue extends NewValue { + @VisibleForTesting + public static final String ALL_PERMISSIONS = "all"; private final String githubRole; private final String sonarqubePermission; @@ -31,6 +33,10 @@ public class GithubPermissionsMappingNewValue extends NewValue { this.sonarqubePermission = sonarqubePermission; } + public static GithubPermissionsMappingNewValue withAllPermissions(String githubRole) { + return new GithubPermissionsMappingNewValue(githubRole, ALL_PERMISSIONS); + } + @VisibleForTesting public String getGithubRole() { return githubRole; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java index 18c4784d940..1bf4d9147d9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java @@ -51,6 +51,11 @@ public class GithubPermissionsMappingDao implements Dao { auditPersister.deleteGithubPermissionsMapping(dbSession, toNewValueForAuditLogs(githubRole, sonarqubePermission)); } + public void deleteAllPermissionsForRole(DbSession dbSession, String githubRole) { + mapper(dbSession).deleteAllPermissionsForRole(githubRole); + auditPersister.deleteGithubPermissionsMapping(dbSession, GithubPermissionsMappingNewValue.withAllPermissions(githubRole)); + } + private static GithubPermissionsMappingNewValue toNewValueForAuditLogs(String githubRole, String sonarqubePermission) { return new GithubPermissionsMappingNewValue(githubRole, sonarqubePermission); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingMapper.java index bb2eb7fb114..faa56070f67 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingMapper.java @@ -31,4 +31,6 @@ public interface GithubPermissionsMappingMapper { void insert(GithubPermissionsMappingDto githubPermissionsMappingDto); void delete(@Param("githubRole") String githubRole, @Param("sonarqubePermission") String sonarqubePermission); + + void deleteAllPermissionsForRole(String githubRole); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubPermissionsMappingMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubPermissionsMappingMapper.xml index 9a813f6d050..1ef0f24582d 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubPermissionsMappingMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubPermissionsMappingMapper.xml @@ -27,6 +27,11 @@ where github_role = #{githubRole,jdbcType=VARCHAR} AND sonarqube_permission = #{sonarqubePermission,jdbcType=VARCHAR} + + delete from github_perms_mapping + where github_role = #{githubRole,jdbcType=VARCHAR} + +