]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13507 filter out duplicated tasks both in progress and done
authorPierre <pierre.guillot@sonarsource.com>
Fri, 14 Aug 2020 13:43:06 +0000 (15:43 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 20 Aug 2020 20:06:43 +0000 (20:06 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java

index a9ee03eefaeb2183f5c05d2acaad571637a47a06..6ce03d480fd38a2dc7b8c75b9ab0d5903e9b5e8c 100644 (file)
@@ -27,6 +27,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import java.util.stream.StreamSupport;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Qualifiers;
@@ -55,6 +56,7 @@ import static java.lang.String.format;
 import static java.util.Arrays.asList;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
+import static java.util.stream.Collectors.toSet;
 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
 import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
 import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
@@ -275,9 +277,14 @@ public class ActivityAction implements CeWsAction {
   private static ActivityResponse buildResponse(Iterable<Ce.Task> queuedTasks, Iterable<Ce.Task> pastTasks, int pageSize) {
     Ce.ActivityResponse.Builder wsResponseBuilder = Ce.ActivityResponse.newBuilder();
 
+    Set<String> pastIds = StreamSupport
+      .stream(pastTasks.spliterator(), false)
+      .map(Ce.Task::getId)
+      .collect(toSet());
+
     int nbInsertedTasks = 0;
     for (Ce.Task queuedTask : queuedTasks) {
-      if (nbInsertedTasks < pageSize) {
+      if (nbInsertedTasks < pageSize && !pastIds.contains(queuedTask.getId())) {
         wsResponseBuilder.addTasks(queuedTask);
         nbInsertedTasks++;
       }
index d7dddab5a5a1a32e84da168febb2183b34908bb2..2a1dad028692d0f1b8fc77f9d09e7d92f759c27a 100644 (file)
@@ -29,6 +29,8 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.api.web.UserRole;
 import org.sonar.ce.task.taskprocessor.CeTaskProcessor;
 import org.sonar.core.util.Uuids;
@@ -390,7 +392,7 @@ public class ActivityActionTest {
       .setParam(PARAM_TYPE, CeTaskTypes.REPORT)
       .setParam(PARAM_STATUS, "SUCCESS,FAILED,CANCELED,IN_PROGRESS,PENDING"));
 
-    assertThat(result.getTasksCount()).isEqualTo(2);
+    assertThat(result.getTasksCount()).isEqualTo(1);
   }
 
   @Test
@@ -576,6 +578,24 @@ public class ActivityActionTest {
     JsonAssert.assertJson(wsResponse.getInput()).isSimilarTo("{\"tasks\":[]}");
   }
 
+  @Test
+  public void filter_out_duplicate_tasks_in_progress_and_success(){
+    logInAsSystemAdministrator();
+    ComponentDto project1 = db.components().insertPrivateProject();
+    ComponentDto project2 = db.components().insertPrivateProject();
+    ComponentDto project3 = db.components().insertPrivateProject();
+    insertQueue("T2", project2, IN_PROGRESS);
+    insertQueue("T3", project3, IN_PROGRESS);
+    insertActivity("T1", project1, SUCCESS);
+    insertActivity("T2", project2, SUCCESS);
+
+    ActivityResponse response = ws.newRequest().setParam("status", "FAILED,IN_PROGRESS,SUCCESS").executeProtobuf(ActivityResponse.class);
+
+    assertThat(response.getTasksList())
+      .extracting(Task::getId)
+      .containsExactlyInAnyOrder("T1","T2","T3");
+  }
+
   private void logInAsSystemAdministrator() {
     userSession.logIn().setSystemAdministrator();
   }