From 835fe2efc3643adbb8cf9632b83c8927ce96e7e9 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 30 Apr 2024 16:14:07 +0200 Subject: [PATCH] SONAR-22170 Removing a quality gate should remove QG conditions --- .../QualityGateConditionDaoIT.java | 13 +++++++++ .../qualitygate/QualityGateConditionDao.java | 4 +++ .../QualityGateConditionMapper.java | 2 ++ .../QualityGateConditionMapper.xml | 4 +++ .../qualitygate/ws/DestroyActionIT.java | 27 +++++++++++++++++++ .../server/qualitygate/ws/DestroyAction.java | 1 + 6 files changed, 51 insertions(+) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/qualitygate/QualityGateConditionDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/qualitygate/QualityGateConditionDaoIT.java index 9d484660ef0..f085811598f 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/qualitygate/QualityGateConditionDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/qualitygate/QualityGateConditionDaoIT.java @@ -116,6 +116,19 @@ class QualityGateConditionDaoIT { assertThat(underTest.selectByUuid(condition2.getUuid(), dbSession)).isNotNull(); } + @Test + void testDeleteForQualityGate() { + QualityGateConditionDto condition1 = insertQGCondition("qualityGateUuid"); + QualityGateConditionDto condition2 = insertQGCondition("qualityGateUuid"); + + underTest.deleteForQualityGate("qualityGateUuid", dbSession); + dbSession.commit(); + + assertThat(underTest.selectForQualityGate(dbSession, "qualityGateUuid")).isEmpty(); + assertThat(underTest.selectByUuid(condition1.getUuid(), dbSession)).isNull(); + assertThat(underTest.selectByUuid(condition2.getUuid(), dbSession)).isNull(); + } + @Test void testUpdate() { QualityGateConditionDto condition1 = insertQGCondition("2"); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDao.java index 4fa4145eef6..33399d630d2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDao.java @@ -46,6 +46,10 @@ public class QualityGateConditionDao implements Dao { mapper(session).delete(qGate.getUuid()); } + public void deleteForQualityGate(String qGateUuid, DbSession session) { + mapper(session).deleteForQualityGate(qGateUuid); + } + public void update(QualityGateConditionDto qGate, DbSession session) { mapper(session).update(qGate.setUpdatedAt(new Date())); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionMapper.java index 7990c77c05b..5fba86f0cf6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionMapper.java @@ -35,5 +35,7 @@ public interface QualityGateConditionMapper { void delete(String uuid); + void deleteForQualityGate(String qGateUuid); + void deleteConditionsWithInvalidMetrics(); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateConditionMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateConditionMapper.xml index 0efc2c5d5b5..1159f6ec40e 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateConditionMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateConditionMapper.xml @@ -37,6 +37,10 @@ delete from quality_gate_conditions where uuid=#{uuid} + + delete from quality_gate_conditions where qgate_uuid=#{qGateUuid} + + update quality_gate_conditions set metric_uuid=#{metricUuid}, diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/DestroyActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/DestroyActionIT.java index ef3e862a1a9..b7d547ffc42 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/DestroyActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/DestroyActionIT.java @@ -27,6 +27,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.Pagination; import org.sonar.db.project.ProjectDto; +import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; @@ -39,6 +40,7 @@ import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; import static java.lang.String.valueOf; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; @@ -76,6 +78,23 @@ public class DestroyActionIT { assertThat(db.getDbClient().qualityGateDao().selectByUuid(dbSession, qualityGate.getUuid())).isNull(); } + @Test + public void delete_quality_gate_should_delete_its_conditions() { + userSession.addPermission(ADMINISTER_QUALITY_GATES); + db.qualityGates().createDefaultQualityGate(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + + insertARandomCondition(qualityGate); + + ws.newRequest() + .setParam(PARAM_NAME, qualityGate.getName()) + .execute(); + + assertThat(db.getDbClient().qualityGateDao().selectByUuid(dbSession, qualityGate.getUuid())).isNull(); + assertThat(db.getDbClient().gateConditionDao().selectForQualityGate(dbSession, qualityGate.getUuid())).isEmpty(); + } + + @Test public void delete_quality_gate_if_non_default_when_a_default_exist() { db.qualityGates().createDefaultQualityGate(); @@ -228,4 +247,12 @@ public class DestroyActionIT { tuple("name", true)); } + private void insertARandomCondition(QualityGateDto qualityGate) { + QualityGateConditionDto condition = new QualityGateConditionDto() + .setUuid(randomAlphanumeric(40)) + .setMetricUuid(randomAlphanumeric(40)) + .setQualityGateUuid(qualityGate.getUuid()); + db.getDbClient().gateConditionDao().insert(condition, db.getSession()); + db.commit(); + } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java index 96236d5717d..fdd2f31ad7c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java @@ -82,6 +82,7 @@ public class DestroyAction implements QualityGatesWsAction { dbClient.qualityGateGroupPermissionsDao().deleteByQualityGate(dbSession, qualityGate); dbClient.qualityGateUserPermissionDao().deleteByQualityGate(dbSession, qualityGate); dbClient.qualityGateDao().delete(qualityGate, dbSession); + dbClient.gateConditionDao().deleteForQualityGate(qualityGate.getUuid(), dbSession); dbSession.commit(); response.noContent(); } -- 2.39.5