diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-10-25 15:56:59 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-10-27 15:17:25 +0200 |
commit | 4fe6138068042a2f8b1eaae4ef153def58a1a829 (patch) | |
tree | b6f7d87e17c61868f6e4d3783cb305c720bf5eb5 /server/sonar-db-dao | |
parent | ad49a8fd67b96baeb5c03b50686865f230c04c92 (diff) | |
download | sonarqube-4fe6138068042a2f8b1eaae4ef153def58a1a829.tar.gz sonarqube-4fe6138068042a2f8b1eaae4ef153def58a1a829.zip |
DEV-4 Return branches analysis when searching for finished analysis
Diffstat (limited to 'server/sonar-db-dao')
4 files changed, 63 insertions, 21 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java index 2c759cafb42..2a4b709b4cf 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -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.", diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java index b8a6226d44f..bee9825d1c0 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -57,4 +57,5 @@ public interface SnapshotMapper { List<SnapshotDto> selectFinishedByComponentUuidsAndFromDates(@Param("componentUuidFromDatePairs") List<ComponentUuidFromDatePair> pairs, @Param("ceStatus") CeActivityDto.Status ceStatus); + } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index 8060e105448..d22ffebb330 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -104,10 +104,11 @@ <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} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index 46c5bb93ab1..d7128820a72 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -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()), @@ -235,6 +234,44 @@ public class SnapshotDaoTest { } @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(); SnapshotDto analysis1 = newAnalysis(project).setCreatedAt(50L).setPeriodDate(20L); @@ -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); |