]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12364 - migrate project analysis delete action to new concept
authorJacek <jacek.poreda@sonarsource.com>
Tue, 6 Aug 2019 11:41:48 +0000 (13:41 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 24 Sep 2019 18:21:12 +0000 (20:21 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/newcodeperiod/NewCodePeriodMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/DeleteAction.java

index c4c535ed2e1940ac96072751c17d37e42e24155d..bf5408c44c7737323bd7c9efb50df650aaa4c0f6 100644 (file)
@@ -85,6 +85,11 @@ public class NewCodePeriodDao implements Dao {
     return Optional.ofNullable(mapper(dbSession).selectByBranch(projectUuid, branchUuid));
   }
 
+  public boolean existsByProjectAnalysisUuid(DbSession dbSession, String projectAnalysisUuid) {
+    requireNonNull(projectAnalysisUuid, "Project analysis uuid must be specified.");
+    return mapper(dbSession).countByProjectAnalysis(projectAnalysisUuid) > 0;
+  }
+
   public void deleteByProjectUuidAndBranchUuid(DbSession dbSession, String projectUuid, String branchUuid) {
     requireNonNull(projectUuid, "Project uuid must be specified.");
     mapper(dbSession).deleteByProjectAndBranch(projectUuid, branchUuid);
index c02e89ad9736298738cfeda24c82803191da7459..cd800842cf3fd5f0cf3616fdbb6e08553a960e90 100644 (file)
@@ -37,4 +37,6 @@ public interface NewCodePeriodMapper {
   void deleteByProjectAndBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
 
   NewCodePeriodDto selectByBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
+
+  long countByProjectAnalysis(String projectAnalysisUuid);
 }
index 74de5d5e48e55233f1e3ee226ef254e9b6e1c4a0..fbd85860878f9a0bf8d46f5682bbfabf2ebd96ed 100644 (file)
     AND branch_uuid=#{branchUuid, jdbcType=VARCHAR}
   </update>
 
+  <select id="countByProjectAnalysis" parameterType="map" resultType="java.lang.Long">
+    SELECT
+    count(1)
+    FROM new_code_periods ncp
+    WHERE
+    ncp.type='SPECIFIC_ANALYSIS'
+    AND ncp.value=#{projectAnalysisUuid, jdbcType=VARCHAR}
+  </select>
+
 </mapper>
index cfe74ad70938ef682477b94284cfd06027122c02..4b238b9eef94b9f502e3887bbd1eb5fc192cd9a1 100644 (file)
@@ -238,6 +238,26 @@ public class NewCodePeriodDaoTest {
     assertThat(result.getUpdatedAt()).isNotEqualTo(0);
   }
 
+  @Test
+  public void exists_by_project_analysis_is_true() {
+    when(uuidFactory.create()).thenReturn(NEW_CODE_PERIOD_UUID);
+
+    underTest.insert(dbSession, new NewCodePeriodDto()
+      .setProjectUuid("proj-uuid")
+      .setBranchUuid("branch-uuid")
+      .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
+      .setValue("analysis-uuid"));
+
+    boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
+    assertThat(exists).isTrue();
+  }
+
+  @Test
+  public void exists_by_project_analysis_is_false() {
+    boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
+    assertThat(exists).isFalse();
+  }
+
   @Test
   public void fail_select_by_project_and_branch_uuids_if_project_uuid_not_provided() {
     expectedException.expect(NullPointerException.class);
index f1940d7c49af26e311c025820afb352a0bdef1bf..fec3383bfd61eec506cb4957320e877549ea5305 100644 (file)
@@ -86,14 +86,10 @@ public class DeleteAction implements ProjectAnalysesWsAction {
   }
 
   private void checkNotBaseline(DbSession dbSession, SnapshotDto analysis) {
-    dbClient.branchDao().selectByUuid(dbSession, analysis.getComponentUuid())
-      .ifPresent(branchDto -> {
-        dbClient.newCodePeriodDao().selectByBranch(dbSession, branchDto.getProjectUuid(), branchDto.getUuid())
-          .ifPresent(newCodePeriodDto ->
-            checkArgument(!newCodePeriodDto.getValue().equals(analysis.getUuid()),
-              "The analysis '%s' can not be deleted because it is set as a manual new code period baseline", analysis.getUuid())
-          );
-      });
+    boolean isSetAsBaseline = dbClient.newCodePeriodDao().existsByProjectAnalysisUuid(dbSession, analysis.getUuid());
+    checkArgument(!isSetAsBaseline,
+      "The analysis '%s' can not be deleted because it is set as a manual new code period baseline", analysis.getUuid());
+
   }
 
   private static NotFoundException analysisNotFoundException(String analysis) {