]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22170 Removing a quality gate should remove QG conditions
authorZipeng WU <zipeng.wu@sonarsource.com>
Tue, 30 Apr 2024 14:14:07 +0000 (16:14 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 3 May 2024 20:02:50 +0000 (20:02 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/qualitygate/QualityGateConditionDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateConditionMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateConditionMapper.xml
server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/DestroyActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java

index 9d484660ef0a94e55b6666c7747ed4b227dc6d15..f085811598f756cdd66fdc6a2c0ee225a0b01840 100644 (file)
@@ -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");
index 4fa4145eef631e315b01f601c2457db2cafecc32..33399d630d22a3e9bee7608a8457cbda3767af84 100644 (file)
@@ -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()));
   }
index 7990c77c05b0145c85c8fb0553a171e7bd29ca53..5fba86f0cf6813ef59935d7cd9508e56e038e320 100644 (file)
@@ -35,5 +35,7 @@ public interface QualityGateConditionMapper {
 
   void delete(String uuid);
 
+  void deleteForQualityGate(String qGateUuid);
+
   void deleteConditionsWithInvalidMetrics();
 }
index 0efc2c5d5b50ca933cbc04b3919b0f7556559e1b..1159f6ec40e6c6940d3425c48e5b7257cfb5bd3c 100644 (file)
     delete from quality_gate_conditions where uuid=#{uuid}
   </update>
 
+  <update id="deleteForQualityGate" parameterType="String">
+    delete from quality_gate_conditions where qgate_uuid=#{qGateUuid}
+  </update>
+
   <update id="update" parameterType="QualityGateCondition">
     update quality_gate_conditions set
     metric_uuid=#{metricUuid},
index ef3e862a1a97de366f10129f271234aa1f8bbb81..b7d547ffc420b8e3576fe5ec49175bc087c94cba 100644 (file)
@@ -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();
+  }
 }
index 96236d5717dfe89d7639a21db22f00070497188c..fdd2f31ad7c870f7647fe8a25954f3b3c909c79d 100644 (file)
@@ -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();
     }