From 379a7845d2ebaa6ad77e6de027e4aa6300dd0d59 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 23 Jun 2020 15:21:21 +0200 Subject: [PATCH] SONAR-13444 display branch/pr names and id in background tasks --- .../issue/index/AsyncIssueIndexingImpl.java | 19 ++++++- .../index/AsyncIssueIndexingImplTest.java | 50 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java index ce9b250075b..3539180eacd 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java @@ -21,11 +21,13 @@ package org.sonar.server.issue.index; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -36,9 +38,12 @@ import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.BranchDto; +import org.sonar.db.component.BranchType; import org.sonar.db.component.SnapshotDto; -import static java.util.Collections.emptyMap; +import static org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_KEY; +import static org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_TYPE_KEY; +import static org.sonar.db.ce.CeTaskCharacteristicDto.PULL_REQUEST; import static org.sonar.db.ce.CeTaskTypes.BRANCH_ISSUE_SYNC; @ServerSide @@ -134,12 +139,22 @@ public class AsyncIssueIndexingImpl implements AsyncIssueIndexing { dbClient.ceActivityDao().deleteByUuids(dbSession, ceUuids); dbSession.commit(); LOG.info("Indexation task deletion complete."); + + LOG.info("Deleting tasks characteristics..."); + Set tasksUuid = Stream.concat(uuids.stream(), ceUuids.stream()).collect(Collectors.toSet()); + dbClient.ceTaskCharacteristicsDao().deleteByTaskUuids(dbSession, tasksUuid); + dbSession.commit(); + LOG.info("Tasks characteristics deletion complete."); } private CeTaskSubmit buildTaskSubmit(BranchDto branch) { + Map characteristics = new HashMap<>(); + characteristics.put(branch.getBranchType() == BranchType.BRANCH ? BRANCH_KEY : PULL_REQUEST, branch.getKey()); + characteristics.put(BRANCH_TYPE_KEY, branch.getBranchType().name()); + return ceQueue.prepareSubmit() .setType(BRANCH_ISSUE_SYNC) .setComponent(new CeTaskSubmit.Component(branch.getUuid(), branch.getProjectUuid())) - .setCharacteristics(emptyMap()).build(); + .setCharacteristics(characteristics).build(); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java index f8c4ddb158c..39af2271744 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java @@ -19,7 +19,9 @@ */ package org.sonar.server.issue.index; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Optional; import org.junit.Before; @@ -38,18 +40,22 @@ import org.sonar.db.DbTester; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeActivityDto.Status; import org.sonar.db.ce.CeQueueDto; +import org.sonar.db.ce.CeTaskCharacteristicDto; import org.sonar.db.component.BranchDto; import org.sonar.db.component.SnapshotDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_TYPE_KEY; import static org.sonar.db.ce.CeTaskTypes.BRANCH_ISSUE_SYNC; import static org.sonar.db.ce.CeTaskTypes.REPORT; import static org.sonar.db.component.BranchType.BRANCH; +import static org.sonar.db.component.BranchType.PULL_REQUEST; import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED; public class AsyncIssueIndexingImplTest { @@ -128,11 +134,15 @@ public class AsyncIssueIndexingImplTest { assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1); + assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(Arrays.asList("uuid_2")))).hasSize(0); + assertThat(logTester.logs(LoggerLevel.INFO)) .contains( "1 pending indexation task found to be deleted...", "1 completed indexation task found to be deleted...", - "Indexation task deletion complete."); + "Indexation task deletion complete.", + "Deleting tasks characteristics...", + "Tasks characteristics deletion complete."); } @Test @@ -174,6 +184,44 @@ public class AsyncIssueIndexingImplTest { .contains("2 projects found in need of issue sync."); } + @Test + public void characteristics_are_defined() { + BranchDto dto = new BranchDto() + .setBranchType(BRANCH) + .setKey("branch_1") + .setUuid("branch_uuid1") + .setProjectUuid("project_uuid1"); + dbClient.branchDao().insert(dbTester.getSession(), dto); + dbTester.commit(); + insertSnapshot("analysis_1", "project_uuid1", 1); + + BranchDto dto2 = new BranchDto() + .setBranchType(PULL_REQUEST) + .setKey("pr_1") + .setUuid("pr_uuid_1") + .setProjectUuid("project_uuid2"); + dbClient.branchDao().insert(dbTester.getSession(), dto2); + dbTester.commit(); + insertSnapshot("analysis_2", "project_uuid2", 2); + + underTest.triggerOnIndexCreation(); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(Collection.class); + verify(ceQueue, times(1)).massSubmit(captor.capture()); + List> captures = captor.getAllValues(); + assertThat(captures).hasSize(1); + Collection tasks = captures.get(0); + assertThat(tasks).hasSize(2); + + assertThat(tasks) + .extracting(p -> p.getCharacteristics().get(BRANCH_TYPE_KEY), + p -> p.getCharacteristics().get(CeTaskCharacteristicDto.BRANCH_KEY), + p -> p.getCharacteristics().get(CeTaskCharacteristicDto.PULL_REQUEST)) + .containsExactlyInAnyOrder( + tuple("BRANCH", "branch_1", null), + tuple("PULL_REQUEST", null, "pr_1")); + } + private SnapshotDto insertSnapshot(String analysisUuid, String projectUuid, long createdAt) { SnapshotDto snapshot = new SnapshotDto() .setUuid(analysisUuid) -- 2.39.5