aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core
diff options
context:
space:
mode:
authoralain <108417558+alain-kermis-sonarsource@users.noreply.github.com>2022-10-27 14:08:20 +0200
committersonartech <sonartech@sonarsource.com>2022-10-27 20:03:02 +0000
commit83bd18d97d9d8e0b858875b253bc52efff70e810 (patch)
tree6984dfa8964d739eafa615aa9434cb933a899f3e /server/sonar-webserver-core
parent0806043b590e1d0f3bf08c4d88627dc9bb6cf913 (diff)
downloadsonarqube-83bd18d97d9d8e0b858875b253bc52efff70e810.tar.gz
sonarqube-83bd18d97d9d8e0b858875b253bc52efff70e810.zip
SONAR-17497 Add web service to reindex issues
Diffstat (limited to 'server/sonar-webserver-core')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexingImpl.java32
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexingImplTest.java103
2 files changed, 113 insertions, 22 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 322d91037ca..bf92288a2a7 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
@@ -96,6 +96,27 @@ public class AsyncIssueIndexingImpl implements AsyncIssueIndexing {
}
}
+ @Override
+ public void triggerForProject(String projectUuid) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+
+ // remove already existing indexation task, if any
+ removeExistingIndexationTasksForProject(dbSession, projectUuid);
+
+ dbClient.branchDao().updateAllNeedIssueSyncForProject(dbSession, projectUuid);
+ List<BranchDto> branchInNeedOfIssueSync = dbClient.branchDao().selectBranchNeedingIssueSyncForProject(dbSession, projectUuid);
+ LOG.info("{} branch(es) found in need of issue sync for project.", branchInNeedOfIssueSync.size());
+
+ List<CeTaskSubmit> tasks = new ArrayList<>();
+ for (BranchDto branch : branchInNeedOfIssueSync) {
+ tasks.add(buildTaskSubmit(branch));
+ }
+
+ ceQueue.massSubmit(tasks);
+ dbSession.commit();
+ }
+ }
+
private void sortProjectUuids(DbSession dbSession, List<String> projectUuids) {
Map<String, SnapshotDto> snapshotByProjectUuid = dbClient.snapshotDao()
.selectLastAnalysesByRootComponentUuids(dbSession, projectUuids).stream()
@@ -122,10 +143,19 @@ public class AsyncIssueIndexingImpl implements AsyncIssueIndexing {
}
private void removeExistingIndexationTasks(DbSession dbSession) {
- List<String> uuids = dbClient.ceQueueDao().selectAllInAscOrder(dbSession).stream()
+ removeIndexationTasks(dbSession, dbClient.ceQueueDao().selectAllInAscOrder(dbSession));
+ }
+
+ private void removeExistingIndexationTasksForProject(DbSession dbSession, String projectUuid) {
+ removeIndexationTasks(dbSession, dbClient.ceQueueDao().selectByMainComponentUuid(dbSession, projectUuid));
+ }
+
+ private void removeIndexationTasks(DbSession dbSession, List<CeQueueDto> ceQueueDtos) {
+ List<String> uuids = ceQueueDtos.stream()
.filter(p -> p.getTaskType().equals(BRANCH_ISSUE_SYNC))
.map(CeQueueDto::getUuid)
.collect(Collectors.toList());
+
LOG.info(String.format("%s pending indexation task found to be deleted...", uuids.size()));
for (String uuid : uuids) {
dbClient.ceQueueDao().deleteByUuid(dbSession, uuid);
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 adc62e4b41b..a97fea02e85 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
@@ -20,7 +20,6 @@
package org.sonar.server.issue.index;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -46,6 +45,7 @@ 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 org.sonar.db.project.ProjectDto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
@@ -69,9 +69,9 @@ public class AsyncIssueIndexingImplTest {
@Rule
public LogTester logTester = new LogTester();
- private DbClient dbClient = dbTester.getDbClient();
- private CeQueue ceQueue = mock(CeQueue.class);
- private UuidFactory uuidFactory = new SequenceUuidFactory();
+ private final DbClient dbClient = dbTester.getDbClient();
+ private final CeQueue ceQueue = mock(CeQueue.class);
+ private final UuidFactory uuidFactory = new SequenceUuidFactory();
private final AsyncIssueIndexingImpl underTest = new AsyncIssueIndexingImpl(ceQueue, dbClient);
@@ -102,6 +102,27 @@ public class AsyncIssueIndexingImplTest {
}
@Test
+ public void triggerForProject() {
+ ProjectDto projectDto = dbTester.components().insertPrivateProjectDto();
+ BranchDto dto = new BranchDto()
+ .setBranchType(BRANCH)
+ .setKey("branchName")
+ .setUuid("branch_uuid")
+ .setProjectUuid(projectDto.getUuid());
+ dbTester.components().insertProjectBranch(projectDto, dto);
+
+ underTest.triggerForProject(projectDto.getUuid());
+
+ Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbTester.getSession(), "branch_uuid");
+ assertThat(branch).isPresent();
+ assertThat(branch.get().isNeedIssueSync()).isTrue();
+ verify(ceQueue, times(2)).prepareSubmit();
+ verify(ceQueue, times(1)).massSubmit(anyCollection());
+ assertThat(logTester.logs(LoggerLevel.INFO))
+ .contains("2 branch(es) found in need of issue sync for project.");
+ }
+
+ @Test
public void triggerOnIndexCreation_no_branch() {
underTest.triggerOnIndexCreation();
@@ -109,36 +130,27 @@ public class AsyncIssueIndexingImplTest {
}
@Test
+ public void triggerForProject_no_branch() {
+ underTest.triggerForProject("some-random-uuid");
+ assertThat(logTester.logs(LoggerLevel.INFO)).contains("0 branch(es) found in need of issue sync for project.");
+ }
+
+ @Test
public void remove_existing_indexation_task() {
- CeQueueDto reportTask = new CeQueueDto();
- reportTask.setUuid("uuid_1");
- reportTask.setTaskType(REPORT);
- dbClient.ceQueueDao().insert(dbTester.getSession(), reportTask);
+ String reportTaskUuid = persistReportTasks();
- CeActivityDto reportActivity = new CeActivityDto(reportTask);
- reportActivity.setStatus(Status.SUCCESS);
- dbClient.ceActivityDao().insert(dbTester.getSession(), reportActivity);
CeQueueDto task = new CeQueueDto();
task.setUuid("uuid_2");
task.setTaskType(BRANCH_ISSUE_SYNC);
dbClient.ceQueueDao().insert(dbTester.getSession(), task);
-
CeActivityDto activityDto = new CeActivityDto(task);
activityDto.setStatus(Status.SUCCESS);
dbClient.ceActivityDao().insert(dbTester.getSession(), activityDto);
-
dbTester.commit();
underTest.triggerOnIndexCreation();
- assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid")
- .containsExactly(reportTask.getUuid());
- assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC)).isEmpty();
-
- assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
-
- assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(Arrays.asList("uuid_2")))).isEmpty();
-
+ assertCeTasks(reportTaskUuid);
assertThat(logTester.logs(LoggerLevel.INFO))
.contains(
"1 pending indexation task found to be deleted...",
@@ -149,6 +161,35 @@ public class AsyncIssueIndexingImplTest {
}
@Test
+ public void remove_existing_indexation_for_project_task() {
+ String reportTaskUuid = persistReportTasks();
+
+ ProjectDto projectDto = dbTester.components().insertPrivateProjectDto();
+ String branchUuid = "branch_uuid";
+ dbTester.components().insertProjectBranch(projectDto, b -> b.setBranchType(BRANCH).setUuid(branchUuid));
+ CeQueueDto mainBranchTask = new CeQueueDto().setUuid("uuid_2").setTaskType(BRANCH_ISSUE_SYNC)
+ .setMainComponentUuid(projectDto.getUuid()).setComponentUuid(projectDto.getUuid());
+ CeQueueDto branchTask = new CeQueueDto().setUuid("uuid_3").setTaskType(BRANCH_ISSUE_SYNC)
+ .setMainComponentUuid(projectDto.getUuid()).setComponentUuid(branchUuid);
+ dbClient.ceQueueDao().insert(dbTester.getSession(), mainBranchTask);
+ dbClient.ceQueueDao().insert(dbTester.getSession(), branchTask);
+ dbTester.commit();
+
+ underTest.triggerForProject(projectDto.getUuid());
+
+ assertCeTasks(reportTaskUuid);
+ assertThat(logTester.logs(LoggerLevel.INFO))
+ .contains(
+ "2 pending indexation task found to be deleted...",
+ "2 completed indexation task found to be deleted...",
+ "Indexation task deletion complete.",
+ "Deleting tasks characteristics...",
+ "Tasks characteristics deletion complete.",
+ "Tasks characteristics deletion complete.",
+ "2 branch(es) found in need of issue sync for project.");
+ }
+
+ @Test
public void order_by_last_analysis_date() {
BranchDto dto = new BranchDto()
.setBranchType(BRANCH)
@@ -274,4 +315,24 @@ public class AsyncIssueIndexingImplTest {
return snapshot;
}
+ private String persistReportTasks() {
+ CeQueueDto reportTask = new CeQueueDto();
+ reportTask.setUuid("uuid_1");
+ reportTask.setTaskType(REPORT);
+ dbClient.ceQueueDao().insert(dbTester.getSession(), reportTask);
+
+ CeActivityDto reportActivity = new CeActivityDto(reportTask);
+ reportActivity.setStatus(Status.SUCCESS);
+ dbClient.ceActivityDao().insert(dbTester.getSession(), reportActivity);
+ return reportTask.getUuid();
+ }
+
+ private void assertCeTasks(String reportTaskUuid) {
+ assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid")
+ .containsExactly(reportTaskUuid);
+ assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC)).isEmpty();
+ assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
+ assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(List.of("uuid_2")))).isEmpty();
+ }
+
}