diff options
author | belen-pruvost-sonarsource <belen.pruvost@sonarsource.com> | 2021-10-13 18:25:51 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-10-22 20:03:27 +0000 |
commit | 06672268a78369a8442e1d8f291b2d80c6d95f9a (patch) | |
tree | 9bbcee010a24b2294e07e4f4558905bc255d5bb5 /server/sonar-db-dao/src | |
parent | 2d5130b8f3150af215721aa44d76fa97612874f1 (diff) | |
download | sonarqube-06672268a78369a8442e1d8f291b2d80c6d95f9a.tar.gz sonarqube-06672268a78369a8442e1d8f291b2d80c6d95f9a.zip |
SONAR-15441 - Create api/qualitygates/add_group endpoint
Diffstat (limited to 'server/sonar-db-dao/src')
9 files changed, 323 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index 2212356d10d..ea48041aed2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -67,6 +67,7 @@ import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; +import org.sonar.db.qualitygate.QualityGateGroupPermissionsDao; import org.sonar.db.qualitygate.QualityGateUserPermissionsDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; @@ -147,6 +148,7 @@ public class DaoModule extends Module { QProfileEditUsersDao.class, QualityGateConditionDao.class, QualityGateDao.class, + QualityGateGroupPermissionsDao.class, QualityGateUserPermissionsDao.class, QualityProfileDao.class, QualityProfileExportDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 2eb29ee9eb0..99fa1daa6fa 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -65,6 +65,7 @@ import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; +import org.sonar.db.qualitygate.QualityGateGroupPermissionsDao; import org.sonar.db.qualitygate.QualityGateUserPermissionsDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; @@ -135,6 +136,7 @@ public class DbClient { private final PurgeDao purgeDao; private final QualityGateDao qualityGateDao; private final QualityGateConditionDao gateConditionDao; + private final QualityGateGroupPermissionsDao qualityGateGroupPermissionsDao; private final QualityGateUserPermissionsDao qualityGateUserPermissionsDao; private final ProjectQgateAssociationDao projectQgateAssociationDao; private final DuplicationDao duplicationDao; @@ -213,6 +215,7 @@ public class DbClient { qualityGateDao = getDao(map, QualityGateDao.class); qualityGateUserPermissionsDao = getDao(map, QualityGateUserPermissionsDao.class); gateConditionDao = getDao(map, QualityGateConditionDao.class); + qualityGateGroupPermissionsDao = getDao(map, QualityGateGroupPermissionsDao.class); projectQgateAssociationDao = getDao(map, ProjectQgateAssociationDao.class); duplicationDao = getDao(map, DuplicationDao.class); notificationQueueDao = getDao(map, NotificationQueueDao.class); @@ -424,6 +427,10 @@ public class DbClient { return qualityGateUserPermissionsDao; } + public QualityGateGroupPermissionsDao qualityGateGroupPermissionsDao() { + return qualityGateGroupPermissionsDao; + } + public ProjectQgateAssociationDao projectQgateAssociationDao() { return projectQgateAssociationDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 03b4dd33ab3..dfb93d67616 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -115,6 +115,7 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationMapper; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateConditionMapper; import org.sonar.db.qualitygate.QualityGateDto; +import org.sonar.db.qualitygate.QualityGateGroupPermissionsMapper; import org.sonar.db.qualitygate.QualityGateUserPermissionsMapper; import org.sonar.db.qualitygate.QualityGateMapper; import org.sonar.db.qualityprofile.ActiveRuleDto; @@ -279,6 +280,7 @@ public class MyBatis implements Startable { QProfileEditUsersMapper.class, QualityGateConditionMapper.class, QualityGateMapper.class, + QualityGateGroupPermissionsMapper.class, QualityGateUserPermissionsMapper.class, QualityProfileMapper.class, QualityProfileExportMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java new file mode 100644 index 00000000000..eecb9287f33 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import java.util.Collection; +import org.sonar.api.utils.System2; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.user.GroupDto; + +import static org.sonar.core.util.stream.MoreCollectors.toList; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class QualityGateGroupPermissionsDao implements Dao { + private final System2 system2; + + public QualityGateGroupPermissionsDao(System2 system2) { + this.system2 = system2; + } + + public boolean exists(DbSession dbSession, QualityGateDto qualityGate, GroupDto group) { + return this.exists(dbSession, qualityGate.getUuid(), group.getUuid()); + } + + public boolean exists(DbSession dbSession, String qualityGateUuid, String groupUuid) { + return mapper(dbSession).selectByQualityGateAndGroup(qualityGateUuid, groupUuid) != null; + } + + public boolean exists(DbSession dbSession, QualityGateDto qualityGate, Collection<GroupDto> groups) { + return !executeLargeInputs(groups.stream().map(GroupDto::getUuid).collect(toList()), + partition -> mapper(dbSession).selectByQualityGateAndGroups(qualityGate.getUuid(), partition)) + .isEmpty(); + } + + public void insert(DbSession dbSession, QualityGateGroupPermissionsDto dto) { + mapper(dbSession).insert(dto, system2.now()); + } + + private static QualityGateGroupPermissionsMapper mapper(DbSession dbSession) { + return dbSession.getMapper(QualityGateGroupPermissionsMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDto.java new file mode 100644 index 00000000000..0e5813c5f5a --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDto.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import java.util.Date; + +/** + * @since 9.2 + */ +public class QualityGateGroupPermissionsDto { + + private String uuid; + private String groupUuid; + private String qualityGateUuid; + private Date createdAt; + + public QualityGateGroupPermissionsDto() { + } + + public String getUuid() { + return uuid; + } + + public QualityGateGroupPermissionsDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } + + public String getGroupUuid() { + return groupUuid; + } + + public QualityGateGroupPermissionsDto setGroupUuid(String groupUuid) { + this.groupUuid = groupUuid; + return this; + } + + public String getQualityGateUuid() { + return qualityGateUuid; + } + + public QualityGateGroupPermissionsDto setQualityGateUuid(String qualityGateUuid) { + this.qualityGateUuid = qualityGateUuid; + return this; + } + + public Date getCreatedAt() { + return createdAt; + } + + public QualityGateGroupPermissionsDto setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + return this; + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java new file mode 100644 index 00000000000..1c46b5b1385 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface QualityGateGroupPermissionsMapper { + + QualityGateGroupPermissionsDto selectByQualityGateAndGroup(@Param("qualityGateUuid") String qualityGateUuid, @Param("groupUuid") String groupUuid); + + List<QualityGateGroupPermissionsDto> selectByQualityGateAndGroups(@Param("qualityGateUuid") String qualityGateUuid, @Param("groupUuids") List<String> groupUuids); + + void insert(@Param("dto") QualityGateGroupPermissionsDto dto, @Param("now") long now); +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.xml new file mode 100644 index 00000000000..efec92a4183 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.xml @@ -0,0 +1,46 @@ +<?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.qualitygate.QualityGateGroupPermissionsMapper"> + + <sql id="sqlColumns"> + qggp.uuid as "uuid", + qggp.group_uuid as "groupUuid", + qggp.quality_gate_uuid as "qualityGateUuid" + </sql> + + <select id="selectByQualityGateAndGroup" parameterType="String" resultType="org.sonar.db.qualitygate.QualityGateGroupPermissionsDto"> + select + <include refid="sqlColumns"/> + from qgate_group_permissions qggp + where + qggp.group_uuid = #{groupUuid} + and qggp.quality_gate_uuid = #{qualityGateUuid} + </select> + + <select id="selectByQualityGateAndGroups" parameterType="map" resultType="org.sonar.db.qualitygate.QualityGateGroupPermissionsDto"> + select + <include refid="sqlColumns"/> + from qgate_group_permissions qggp + where + <foreach collection="groupUuids" open="(" close=")" item="groupUuid" separator=" or "> + qggp.group_uuid = #{groupUuid, jdbcType=VARCHAR} + </foreach> + and qggp.quality_gate_uuid = #{qualityGateUuid, jdbcType=VARCHAR} + </select> + + <insert id="insert" useGeneratedKeys="false" parameterType="map"> + insert into qgate_group_permissions( + uuid, + group_uuid, + quality_gate_uuid, + created_at + ) values ( + #{dto.uuid, jdbcType=VARCHAR}, + #{dto.groupUuid, jdbcType=VARCHAR}, + #{dto.qualityGateUuid, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT} + ) + </insert> + +</mapper> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java new file mode 100644 index 00000000000..b94e8ee3e15 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java @@ -0,0 +1,92 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import java.util.Date; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.core.util.Uuids; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.user.GroupDto; +import org.sonar.db.user.GroupTesting; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThat; + +public class QualityGateGroupPermissionsDaoTest { + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + private DbSession dbSession = dbTester.getSession(); + private QualityGateGroupPermissionsDao underTest = dbTester.getDbClient().qualityGateGroupPermissionsDao(); + + @Test + public void itInsertsAndExistsReturnsTrue() { + GroupDto group = GroupTesting.newGroupDto(); + QualityGateDto qualityGateDto = insertQualityGate(); + QualityGateGroupPermissionsDto qualityGateGroupPermission = insertQualityGateGroupPermission(qualityGateDto.getUuid(), group.getUuid()); + + assertThat(qualityGateGroupPermission.getUuid()).isNotNull(); + assertThat(underTest.exists(dbSession, qualityGateDto, group)).isTrue(); + assertThat(underTest.exists(dbSession, qualityGateDto.getUuid(), group.getUuid())).isTrue(); + } + + @Test + public void existsReturnsTrueForListOfGroups() { + GroupDto group1 = GroupTesting.newGroupDto(); + GroupDto group2 = GroupTesting.newGroupDto(); + QualityGateDto qualityGateDto = insertQualityGate(); + QualityGateGroupPermissionsDto qualityGateGroupPermission = insertQualityGateGroupPermission(qualityGateDto.getUuid(), group1.getUuid()); + + assertThat(qualityGateGroupPermission.getUuid()).isNotNull(); + assertThat(underTest.exists(dbSession, qualityGateDto, List.of(group1, group2))).isTrue(); + assertThat(underTest.exists(dbSession, qualityGateDto.getUuid(), group1.getUuid())).isTrue(); + assertThat(underTest.exists(dbSession, qualityGateDto.getUuid(), group2.getUuid())).isFalse(); + } + + @Test + public void existsReturnsFalseWhenQGEditGroupsDoesNotExist() { + assertThat(underTest.exists(dbSession, randomAlphabetic(5), randomAlphabetic(5))).isFalse(); + } + + private QualityGateDto insertQualityGate() { + QualityGateDto qg = new QualityGateDto() + .setUuid(randomAlphabetic(5)) + .setName(randomAlphabetic(5)); + dbTester.getDbClient().qualityGateDao().insert(dbTester.getSession(), qg); + dbTester.commit(); + return qg; + } + + private QualityGateGroupPermissionsDto insertQualityGateGroupPermission(String qualityGateUuid, String groupUuid) { + QualityGateGroupPermissionsDto qgg = new QualityGateGroupPermissionsDto() + .setUuid(Uuids.create()) + .setQualityGateUuid(qualityGateUuid) + .setGroupUuid(groupUuid) + .setCreatedAt(new Date()); + underTest.insert(dbTester.getSession(), qgg); + dbTester.commit(); + return qgg; + } +} diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java index 6dbbe27587e..1243594bf5f 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java @@ -30,6 +30,7 @@ import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.property.PropertyDto; +import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -104,6 +105,15 @@ public class QualityGateDbTester { return dbClient.projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, componentUuid); } + public void addGroupPermission(QualityGateDto qualityGateDto, GroupDto group) { + dbClient.qualityGateGroupPermissionsDao().insert(dbSession, new QualityGateGroupPermissionsDto() + .setUuid(Uuids.createFast()) + .setGroupUuid(group.getUuid()) + .setQualityGateUuid(qualityGateDto.getUuid()) + ); + dbSession.commit(); + } + public void addUserPermission(QualityGateDto qualityGateDto, UserDto user) { dbClient.qualityGateUserPermissionDao().insert(dbSession, new QualityGateUserPermissionsDto() .setUuid(Uuids.createFast()) |