From 80063c314e8e77cd4426f195aa05e6d266573c13 Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 14 Aug 2020 15:43:06 +0200 Subject: [PATCH] SONAR-13507 filter out duplicated tasks both in progress and done --- .../sonar/server/ce/ws/ActivityAction.java | 9 +++++++- .../server/ce/ws/ActivityActionTest.java | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java index a9ee03eefae..6ce03d480fd 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java @@ -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 queuedTasks, Iterable pastTasks, int pageSize) { Ce.ActivityResponse.Builder wsResponseBuilder = Ce.ActivityResponse.newBuilder(); + Set 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++; } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java index d7dddab5a5a..2a1dad02869 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java @@ -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(); } -- 2.39.5