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;
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
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();
}
}
*/
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;
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 {
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
.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)