]> source.dussan.org Git - sonarqube.git/commitdiff
DEV-4 Return branches analysis when searching for finished analysis
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 25 Oct 2017 13:56:59 +0000 (15:56 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Oct 2017 13:17:25 +0000 (15:17 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java

index 2c759cafb4289c2cf3159a3af2d255963b90adc7..2a4b709b4cfc37c364d397fc59eac9abf1624ec5 100644 (file)
@@ -98,7 +98,10 @@ public class SnapshotDao implements Dao {
   }
 
   /**
+   * Returned finished analysis from a list of projects and dates.
+   * "Finished" analysis means that the status in the CE_ACTIVITY table is SUCCESS => the goal is to be sure that the CE task is completely finished.
    *
+   * Note that branches analysis of projects are also returned.
    */
   public List<SnapshotDto> selectFinishedByComponentUuidsAndFromDates(DbSession dbSession, List<String> componentUuids, List<Long> fromDates) {
     checkArgument(componentUuids.size() == fromDates.size(), "The number of components (%s) and from dates (%s) must be the same.",
index b8a6226d44fdf483defb53ece9a0f7371957121f..bee9825d1c003284ef8afbfe4028f4db8903f39a 100644 (file)
@@ -57,4 +57,5 @@ public interface SnapshotMapper {
 
   List<SnapshotDto> selectFinishedByComponentUuidsAndFromDates(@Param("componentUuidFromDatePairs") List<ComponentUuidFromDatePair> pairs,
     @Param("ceStatus") CeActivityDto.Status ceStatus);
+
 }
index 8060e105448de0112583b9730a688628b4001019..d22ffebb330bb9c5537a00a23b5b7461bcc5b535 100644 (file)
     <include refid="snapshotColumns" />
     from snapshots s
       inner join projects p on p.uuid=s.component_uuid and p.enabled=${_true}
-      inner join ce_activity ca on s.uuid = ca.analysis_uuid and s.component_uuid = ca.component_uuid
+      inner join project_branches pb on pb.uuid=p.uuid
+      inner join ce_activity ca on ca.analysis_uuid = s.uuid and ca.component_uuid = pb.project_uuid
     where
       <foreach collection="componentUuidFromDatePairs" open="(" close=")" item="componentUuidFromDatePair" separator=" or ">
-        (s.component_uuid=#{componentUuidFromDatePair.componentUuid, jdbcType=VARCHAR} and s.created_at >= #{componentUuidFromDatePair.from, jdbcType=BIGINT})
+        (pb.project_uuid=#{componentUuidFromDatePair.componentUuid, jdbcType=VARCHAR} and s.created_at >= #{componentUuidFromDatePair.from, jdbcType=BIGINT})
       </foreach>
       and s.status = 'P'
       and ca.status=#{ceStatus, jdbcType=VARCHAR}
index 46c5bb93ab14fb97880f97f6056fbc56d6b42ffe..d7128820a72808cb68f0059fc7985b40acd2587d 100644 (file)
@@ -38,9 +38,12 @@ import org.sonar.db.organization.OrganizationTesting;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
 import static org.apache.commons.lang.math.RandomUtils.nextLong;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.ce.CeActivityDto.Status.CANCELED;
+import static org.sonar.db.ce.CeActivityDto.Status.SUCCESS;
 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
 import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
 import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED;
@@ -208,23 +211,19 @@ public class SnapshotDaoTest {
   public void selectFinishedByComponentUuidsAndFromDates() {
     long from = 1_500_000_000_000L;
     long otherFrom = 1_200_000_000_000L;
-    ComponentDto firstProject = db.components().insertPrivateProject();
-    ComponentDto secondProject = db.components().insertPrivateProject();
-    ComponentDto thirdProject = db.components().insertPrivateProject();
-    SnapshotDto unprocessedAnalysis = db.components().insertSnapshot(newAnalysis(firstProject).setStatus(STATUS_UNPROCESSED).setCreatedAt(from + 1_000_000L));
-    insertActivity(unprocessedAnalysis, CeActivityDto.Status.CANCELED);
-    SnapshotDto finishedAnalysis = db.components().insertSnapshot(newAnalysis(firstProject).setStatus(STATUS_PROCESSED).setCreatedAt(from));
-    insertActivity(finishedAnalysis, CeActivityDto.Status.SUCCESS);
-    SnapshotDto otherFinishedAnalysis = db.components().insertSnapshot(newAnalysis(firstProject).setStatus(STATUS_PROCESSED).setCreatedAt(from + 1_000_000L));
-    insertActivity(otherFinishedAnalysis, CeActivityDto.Status.SUCCESS);
-    SnapshotDto canceledAnalysis = db.components().insertSnapshot(newAnalysis(firstProject).setStatus(STATUS_PROCESSED).setCreatedAt(from));
-    insertActivity(canceledAnalysis, CeActivityDto.Status.CANCELED);
-    SnapshotDto oldAnalysis = db.components().insertSnapshot(newAnalysis(firstProject).setStatus(STATUS_PROCESSED).setCreatedAt(from - 1L));
-    insertActivity(oldAnalysis, CeActivityDto.Status.SUCCESS);
-    SnapshotDto analysisOnSecondProject = db.components().insertSnapshot(newAnalysis(secondProject).setStatus(STATUS_PROCESSED).setCreatedAt(otherFrom));
-    insertActivity(analysisOnSecondProject, CeActivityDto.Status.SUCCESS);
-    SnapshotDto oldAnalysisOnThirdProject = db.components().insertSnapshot(newAnalysis(thirdProject).setStatus(STATUS_PROCESSED).setCreatedAt(otherFrom - 1L));
-    insertActivity(oldAnalysisOnThirdProject, CeActivityDto.Status.SUCCESS);
+    ComponentDto firstProject = db.components().insertMainBranch();
+    ComponentDto secondProject = db.components().insertMainBranch();
+    ComponentDto thirdProject = db.components().insertMainBranch();
+    SnapshotDto finishedAnalysis = db.components().insertSnapshot(firstProject, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from));
+    insertActivity(firstProject.uuid(), finishedAnalysis, SUCCESS);
+    SnapshotDto otherFinishedAnalysis = db.components().insertSnapshot(firstProject, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from + 1_000_000L));
+    insertActivity(firstProject.uuid(), otherFinishedAnalysis, SUCCESS);
+    SnapshotDto oldAnalysis = db.components().insertSnapshot(firstProject, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from - 1L));
+    insertActivity(firstProject.uuid(), oldAnalysis, SUCCESS);
+    SnapshotDto analysisOnSecondProject = db.components().insertSnapshot(secondProject, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(otherFrom));
+    insertActivity(secondProject.uuid(), analysisOnSecondProject, SUCCESS);
+    SnapshotDto oldAnalysisOnThirdProject = db.components().insertSnapshot(thirdProject, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(otherFrom - 1L));
+    insertActivity(thirdProject.uuid(), oldAnalysisOnThirdProject, SUCCESS);
 
     List<SnapshotDto> result = underTest.selectFinishedByComponentUuidsAndFromDates(dbSession,
       Arrays.asList(firstProject.uuid(), secondProject.uuid(), thirdProject.uuid()),
@@ -234,6 +233,44 @@ public class SnapshotDaoTest {
       .containsExactlyInAnyOrder(finishedAnalysis.getUuid(), otherFinishedAnalysis.getUuid(), analysisOnSecondProject.getUuid());
   }
 
+  @Test
+  public void selectFinishedByComponentUuidsAndFromDates_ignores_unsuccessful_analysis() {
+    long from = 1_500_000_000_000L;
+    ComponentDto project = db.components().insertMainBranch();
+    SnapshotDto unprocessedAnalysis = db.components().insertSnapshot(project, s -> s.setStatus(STATUS_UNPROCESSED).setCreatedAt(from + 1_000_000L));
+    insertActivity(project.uuid(), unprocessedAnalysis, CANCELED);
+    SnapshotDto finishedAnalysis = db.components().insertSnapshot(project, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from));
+    insertActivity(project.uuid(), finishedAnalysis, SUCCESS);
+    SnapshotDto canceledAnalysis = db.components().insertSnapshot(project, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from));
+    insertActivity(project.uuid(), canceledAnalysis, CANCELED);
+
+    List<SnapshotDto> result = underTest.selectFinishedByComponentUuidsAndFromDates(dbSession, singletonList(project.uuid()), singletonList(from));
+
+    assertThat(result).extracting(SnapshotDto::getUuid)
+      .containsExactlyInAnyOrder(finishedAnalysis.getUuid());
+  }
+
+  @Test
+  public void selectFinishedByComponentUuidsAndFromDates_return_branches_analysis() {
+    long from = 1_500_000_000_000L;
+    ComponentDto project = db.components().insertMainBranch();
+    ComponentDto firstBranch = db.components().insertProjectBranch(project);
+    ComponentDto secondBranch = db.components().insertProjectBranch(project);
+    SnapshotDto finishedAnalysis = db.components().insertSnapshot(firstBranch, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from));
+    insertActivity(project.uuid(), finishedAnalysis, SUCCESS);
+    SnapshotDto otherFinishedAnalysis = db.components().insertSnapshot(firstBranch, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from + 1_000_000L));
+    insertActivity(project.uuid(), otherFinishedAnalysis, SUCCESS);
+    SnapshotDto oldAnalysis = db.components().insertSnapshot(firstBranch, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from - 1L));
+    insertActivity(project.uuid(), oldAnalysis, SUCCESS);
+    SnapshotDto analysisOnSecondBranch = db.components().insertSnapshot(secondBranch, s -> s.setStatus(STATUS_PROCESSED).setCreatedAt(from));
+    insertActivity(project.uuid(), analysisOnSecondBranch, SUCCESS);
+
+    List<SnapshotDto> result = underTest.selectFinishedByComponentUuidsAndFromDates(dbSession, singletonList(project.uuid()), singletonList(from));
+
+    assertThat(result).extracting(SnapshotDto::getUuid)
+      .containsExactlyInAnyOrder(finishedAnalysis.getUuid(), otherFinishedAnalysis.getUuid(), analysisOnSecondBranch.getUuid());
+  }
+
   @Test
   public void selectSnapshotBefore() {
     ComponentDto project = db.components().insertPrivateProject();
@@ -391,10 +428,10 @@ public class SnapshotDaoTest {
       .setBuildDate(1_500_000_000_006L);
   }
 
-  private CeActivityDto insertActivity(SnapshotDto analysis, CeActivityDto.Status status) {
+  private CeActivityDto insertActivity(String projectUuid, SnapshotDto analysis, CeActivityDto.Status status) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setTaskType(CeTaskTypes.REPORT);
-    queueDto.setComponentUuid(analysis.getComponentUuid());
+    queueDto.setComponentUuid(projectUuid);
     queueDto.setUuid(randomAlphanumeric(40));
     queueDto.setCreatedAt(nextLong());
     CeActivityDto activityDto = new CeActivityDto(queueDto);