]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20392 Added GithubPermissionsMapping[Dao|Dto]
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Wed, 6 Sep 2023 14:09:43 +0000 (16:09 +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 [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDao.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDto.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingMapper.java [new file with mode: 0644]
server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubPermissionsMappingMapper.xml [new file with mode: 0644]

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
new file mode 100644 (file)
index 0000000..0eb588d
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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.provisioning;
+
+import java.util.Set;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class GithubPermissionsMappingDaoIT {
+
+  private static final String MAPPING_UUID = "uuid";
+
+  @Rule
+  public final DbTester db = DbTester.create();
+
+  private final DbSession dbSession = db.getSession();
+
+  private final GithubPermissionsMappingDao underTest = db.getDbClient().githubPermissionsMappingDao();
+
+  @Test
+  public void insert_savesGithubPermissionsMappingDto() {
+    GithubPermissionsMappingDto githubPermissionsMappingDto = new GithubPermissionsMappingDto(MAPPING_UUID, "GH_role", "SQ_role");
+
+    underTest.insert(dbSession, githubPermissionsMappingDto);
+
+    Set<GithubPermissionsMappingDto> savedGithubPermissionsMappings = underTest.findAll(dbSession);
+    assertThat(savedGithubPermissionsMappings).hasSize(1);
+    GithubPermissionsMappingDto savedMapping = savedGithubPermissionsMappings.iterator().next();
+    assertThat(savedMapping.uuid()).isEqualTo(githubPermissionsMappingDto.uuid());
+    assertThat(savedMapping.githubRole()).isEqualTo(githubPermissionsMappingDto.githubRole());
+    assertThat(savedMapping.sonarqubePermission()).isEqualTo(githubPermissionsMappingDto.sonarqubePermission());
+  }
+
+  @Test
+  public void findAll_shouldReturnAllGithubOrganizationGroup() {
+    GithubPermissionsMappingDto mapping1 = new GithubPermissionsMappingDto(MAPPING_UUID, "GH_role", "SQ_role");
+    GithubPermissionsMappingDto mapping2 = new GithubPermissionsMappingDto(MAPPING_UUID + "2", "GH_role2", "SQ_role");
+
+    underTest.insert(dbSession, mapping1);
+    underTest.insert(dbSession, mapping2);
+
+    Set<GithubPermissionsMappingDto> all = underTest.findAll(dbSession);
+
+    assertThat(all).hasSize(2)
+      .containsExactlyInAnyOrder(
+        mapping1,
+        mapping2
+      );
+  }
+
+}
index 9ef51dcb6324039216bfca07aa880e73c43c879e..e67eee6703d6cfc7fea0008036a4078e3d3ecf61 100644 (file)
@@ -64,6 +64,7 @@ import org.sonar.db.property.InternalComponentPropertiesDao;
 import org.sonar.db.property.InternalPropertiesDao;
 import org.sonar.db.property.PropertiesDao;
 import org.sonar.db.provisioning.GithubOrganizationGroupDao;
+import org.sonar.db.provisioning.GithubPermissionsMappingDao;
 import org.sonar.db.purge.PurgeDao;
 import org.sonar.db.pushevent.PushEventDao;
 import org.sonar.db.qualitygate.ProjectQgateAssociationDao;
@@ -128,6 +129,7 @@ public class DaoModule extends Module {
     EventDao.class,
     EventComponentChangeDao.class,
     GithubOrganizationGroupDao.class,
+    GithubPermissionsMappingDao.class,
     ExternalGroupDao.class,
     FileSourceDao.class,
     GroupDao.class,
index 13d32c315d241d49e9dfb79ec397e37531dddf3a..9949da0a2230f592fd9586626fdb09c62bca2bed 100644 (file)
@@ -64,6 +64,7 @@ import org.sonar.db.property.InternalComponentPropertiesDao;
 import org.sonar.db.property.InternalPropertiesDao;
 import org.sonar.db.property.PropertiesDao;
 import org.sonar.db.provisioning.GithubOrganizationGroupDao;
+import org.sonar.db.provisioning.GithubPermissionsMappingDao;
 import org.sonar.db.purge.PurgeDao;
 import org.sonar.db.pushevent.PushEventDao;
 import org.sonar.db.qualitygate.ProjectQgateAssociationDao;
@@ -187,6 +188,7 @@ public class DbClient {
   private final ReportScheduleDao reportScheduleDao;
   private final ReportSubscriptionDao reportSubscriptionDao;
   private final GithubOrganizationGroupDao githubOrganizationGroupDao;
+  private final GithubPermissionsMappingDao githubPermissionsMappingDao;
 
   public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) {
     this.database = database;
@@ -244,6 +246,7 @@ public class DbClient {
     metricDao = getDao(map, MetricDao.class);
     groupDao = getDao(map, GroupDao.class);
     githubOrganizationGroupDao = getDao(map, GithubOrganizationGroupDao.class);
+    githubPermissionsMappingDao = getDao(map, GithubPermissionsMappingDao.class);
     externalGroupDao = getDao(map, ExternalGroupDao.class);
     ruleDao = getDao(map, RuleDao.class);
     ruleRepositoryDao = getDao(map, RuleRepositoryDao.class);
@@ -490,6 +493,10 @@ public class DbClient {
     return githubOrganizationGroupDao;
   }
 
+  public GithubPermissionsMappingDao githubPermissionsMappingDao() {
+    return githubPermissionsMappingDao;
+  }
+
   public ExternalGroupDao externalGroupDao() {
     return externalGroupDao;
   }
index 98b404ea849459c5084df65f381555b63eec0b52..2566492e862fdfcd1ae4ab0ad7c94405eccc5018 100644 (file)
@@ -119,6 +119,8 @@ import org.sonar.db.property.PropertiesMapper;
 import org.sonar.db.property.ScrapPropertyDto;
 import org.sonar.db.provisioning.GithubOrganizationGroupDto;
 import org.sonar.db.provisioning.GithubOrganizationGroupMapper;
+import org.sonar.db.provisioning.GithubPermissionsMappingDto;
+import org.sonar.db.provisioning.GithubPermissionsMappingMapper;
 import org.sonar.db.purge.PurgeMapper;
 import org.sonar.db.purge.PurgeableAnalysisDto;
 import org.sonar.db.pushevent.PushEventDto;
@@ -207,6 +209,7 @@ public class MyBatis {
     confBuilder.loadAlias("Event", EventDto.class);
     confBuilder.loadAlias("ExternalGroup", ExternalGroupDto.class);
     confBuilder.loadAlias("GithubOrganizationGroup", GithubOrganizationGroupDto.class);
+    confBuilder.loadAlias("GithubPermissionsMapping", GithubPermissionsMappingDto.class);
     confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class);
     confBuilder.loadAlias("KeyWithUuid", KeyWithUuidDto.class);
     confBuilder.loadAlias("Group", GroupDto.class);
@@ -285,6 +288,7 @@ public class MyBatis {
       EventMapper.class,
       EventComponentChangeMapper.class,
       GithubOrganizationGroupMapper.class,
+      GithubPermissionsMappingMapper.class,
       ExternalGroupMapper.class,
       FileSourceMapper.class,
       GroupMapper.class,
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
new file mode 100644 (file)
index 0000000..fad64ad
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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.provisioning;
+
+import java.util.Set;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+
+public class GithubPermissionsMappingDao implements Dao {
+
+  public Set<GithubPermissionsMappingDto> findAll(DbSession dbSession) {
+    return mapper(dbSession).selectAll();
+  }
+
+  public void insert(DbSession dbSession, GithubPermissionsMappingDto githubPermissionsMappingDto) {
+    mapper(dbSession).insert(githubPermissionsMappingDto);
+  }
+
+  private static GithubPermissionsMappingMapper mapper(DbSession session) {
+    return session.getMapper(GithubPermissionsMappingMapper.class);
+  }
+
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubPermissionsMappingDto.java
new file mode 100644 (file)
index 0000000..cf99b5c
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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.provisioning;
+
+public record GithubPermissionsMappingDto(String uuid, String githubRole, String 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
new file mode 100644 (file)
index 0000000..c59a911
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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.provisioning;
+
+import java.util.Set;
+
+public interface GithubPermissionsMappingMapper {
+
+  Set<GithubPermissionsMappingDto> selectAll();
+
+  void insert(GithubPermissionsMappingDto githubPermissionsMappingDto);
+
+}
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
new file mode 100644 (file)
index 0000000..7f739bc
--- /dev/null
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.db.provisioning.GithubPermissionsMappingMapper">
+
+  <sql id="githubPermissionsMappingColumns">
+    gpm.uuid as uuid,
+    gpm.github_role as githubRole,
+    gpm.sonarqube_permission as sonarqubePermission
+  </sql>
+
+  <insert id="insert" useGeneratedKeys="false" parameterType="GithubPermissionsMapping">
+    insert into github_perms_mapping (
+      uuid,
+      github_role,
+      sonarqube_permission
+    ) values (
+      #{uuid,jdbcType=VARCHAR},
+      #{githubRole,jdbcType=VARCHAR},
+      #{sonarqubePermission,jdbcType=VARCHAR}
+    )
+  </insert>
+
+  <select id="selectAll" resultType="GithubPermissionsMapping">
+    SELECT
+      <include refid="githubPermissionsMappingColumns"/>
+    FROM github_perms_mapping gpm
+  </select>
+
+</mapper>