]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13444 display branch/pr names and id in background tasks
authorPierre <pierre.guillot@sonarsource.com>
Tue, 23 Jun 2020 13:21:21 +0000 (15:21 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 26 Jun 2020 20:04:58 +0000 (20:04 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java

index ce9b250075b0d482bece541f6ec125d3cf42f479..3539180eacddf11a93f34624ce952bb240b9aac0 100644 (file)
@@ -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<String> 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<String, String> 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();
   }
 }
index f8c4ddb158c15daf2b14042b1bef36416d2d2129..39af227174479cfe73a82e8d9f526f93c1b770bc 100644 (file)
@@ -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<Collection<CeTaskSubmit>> captor = ArgumentCaptor.forClass(Collection.class);
+    verify(ceQueue, times(1)).massSubmit(captor.capture());
+    List<Collection<CeTaskSubmit>> captures = captor.getAllValues();
+    assertThat(captures).hasSize(1);
+    Collection<CeTaskSubmit> 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)