Search in sources :

Example 1 with CloseableIterator

use of org.sonar.core.util.CloseableIterator in project sonarqube by SonarSource.

the class SendIssueNotificationsStep method doExecute.

private void doExecute(NotificationStatistics notificationStatistics, Component project) {
    long analysisDate = analysisMetadataHolder.getAnalysisDate();
    Predicate<DefaultIssue> onCurrentAnalysis = i -> i.isNew() && i.creationDate().getTime() >= truncateToSeconds(analysisDate);
    NewIssuesStatistics newIssuesStats = new NewIssuesStatistics(onCurrentAnalysis);
    Map<String, UserDto> assigneesByUuid;
    try (DbSession dbSession = dbClient.openSession(false)) {
        Iterable<DefaultIssue> iterable = protoIssueCache::traverse;
        Set<String> assigneeUuids = stream(iterable.spliterator(), false).map(DefaultIssue::assignee).filter(Objects::nonNull).collect(Collectors.toSet());
        assigneesByUuid = dbClient.userDao().selectByUuids(dbSession, assigneeUuids).stream().collect(toMap(UserDto::getUuid, dto -> dto));
    }
    try (CloseableIterator<DefaultIssue> issues = protoIssueCache.traverse()) {
        processIssues(newIssuesStats, issues, assigneesByUuid, notificationStatistics);
    }
    if (newIssuesStats.hasIssuesOnCurrentAnalysis()) {
        sendNewIssuesNotification(newIssuesStats, project, assigneesByUuid, analysisDate, notificationStatistics);
        sendMyNewIssuesNotification(newIssuesStats, project, assigneesByUuid, analysisDate, notificationStatistics);
    }
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Duration(org.sonar.api.utils.Duration) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) UserDto(org.sonar.db.user.UserDto) Date(java.util.Date) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) RuleType(org.sonar.api.rules.RuleType) HashSet(java.util.HashSet) MoreCollectors.toSet(org.sonar.core.util.stream.MoreCollectors.toSet) ComputationStep(org.sonar.ce.task.step.ComputationStep) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) NotificationService(org.sonar.server.notification.NotificationService) Component(org.sonar.ce.task.projectanalysis.component.Component) ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) BranchType(org.sonar.db.component.BranchType) ImmutableSet(com.google.common.collect.ImmutableSet) DefaultIssue(org.sonar.core.issue.DefaultIssue) Predicate(java.util.function.Predicate) Notification(org.sonar.api.notifications.Notification) CloseableIterator(org.sonar.core.util.CloseableIterator) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) AnalysisMetadataHolder(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ChronoUnit(java.time.temporal.ChronoUnit) Issue(org.sonar.api.issue.Issue) StreamSupport.stream(java.util.stream.StreamSupport.stream) NotificationFactory(org.sonar.ce.task.projectanalysis.notification.NotificationFactory) TreeRootHolder(org.sonar.ce.task.projectanalysis.component.TreeRootHolder) CheckForNull(javax.annotation.CheckForNull) DbSession(org.sonar.db.DbSession) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) UserDto(org.sonar.db.user.UserDto) DefaultIssue(org.sonar.core.issue.DefaultIssue)

Example 2 with CloseableIterator

use of org.sonar.core.util.CloseableIterator in project sonarqube by SonarSource.

the class FileSourceDataComputerTest method compute_builds_data_object_from_lines.

@Test
public void compute_builds_data_object_from_lines() {
    int lineCount = 1 + new Random().nextInt(10);
    int randomStartPoint = new Random().nextInt(500);
    List<String> lines = IntStream.range(0, lineCount).mapToObj(i -> "line" + i).collect(toList());
    List<String> expectedLineHashes = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> "str_" + i).collect(toList());
    Changeset expectedChangeset = Changeset.newChangesetBuilder().setDate((long) new Random().nextInt(9_999)).build();
    String expectedSrcHash = computeSrcHash(lines);
    CloseableIterator<String> lineIterator = spy(CloseableIterator.from(lines.iterator()));
    DbFileSources.Data.Builder expectedLineDataBuilder = DbFileSources.Data.newBuilder();
    for (int i = 0; i < lines.size(); i++) {
        expectedLineDataBuilder.addLinesBuilder().setSource(lines.get(i)).setLine(i + 1).setScmAuthor("reader_called_" + (randomStartPoint + i));
    }
    when(sourceLinesRepository.readLines(FILE)).thenReturn(lineIterator);
    when(sourceLineReadersFactory.getLineReaders(FILE)).thenReturn(lineReaders);
    when(sourceLinesHashRepository.getLineHashesComputerToPersist(FILE)).thenReturn(lineHashesComputer);
    when(lineHashesComputer.getResult()).thenReturn(expectedLineHashes);
    when(lineReaders.getLatestChangeWithRevision()).thenReturn(expectedChangeset);
    // mocked implementation of LineReader.read to ensure changes done by it to the lineBuilder argument actually end
    // up in the FileSourceDataComputer.Data object returned
    doAnswer(new Answer() {

        int i = 0;

        @Override
        public Object answer(InvocationOnMock invocation) {
            DbFileSources.Line.Builder lineBuilder = invocation.getArgument(0);
            lineBuilder.setScmAuthor("reader_called_" + (randomStartPoint + i++));
            return null;
        }
    }).when(lineReaders).read(any(), any());
    FileSourceDataComputer.Data data = underTest.compute(FILE, fileSourceDataWarnings);
    assertThat(data.getLineHashes()).isEqualTo(expectedLineHashes);
    assertThat(data.getSrcHash()).isEqualTo(expectedSrcHash);
    assertThat(data.getLatestChangeWithRevision()).isSameAs(expectedChangeset);
    assertThat(data.getLineData()).isEqualTo(expectedLineDataBuilder.build());
    verify(lineIterator).close();
    verify(lineReaders).close();
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LineHashesComputer(org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepositoryImpl.LineHashesComputer) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) DbFileSources(org.sonar.db.protobuf.DbFileSources) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SourceHashComputer(org.sonar.core.hash.SourceHashComputer) Random(java.util.Random) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Mockito.spy(org.mockito.Mockito.spy) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) Iterator(java.util.Iterator) CloseableIterator(org.sonar.core.util.CloseableIterator) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Random(java.util.Random) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DbFileSources(org.sonar.db.protobuf.DbFileSources) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Example 3 with CloseableIterator

use of org.sonar.core.util.CloseableIterator in project sonarqube by SonarSource.

the class SourceLineReadersFactory method getLineReaders.

public LineReaders getLineReaders(Component component) {
    List<LineReader> readers = new ArrayList<>();
    List<CloseableIterator<?>> closeables = new ArrayList<>();
    ScmLineReader scmLineReader = null;
    int componentRef = component.getReportAttributes().getRef();
    CloseableIterator<ScannerReport.LineCoverage> coverageIt = reportReader.readComponentCoverage(componentRef);
    closeables.add(coverageIt);
    readers.add(new CoverageLineReader(coverageIt));
    Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
    if (scmInfoOptional.isPresent()) {
        scmLineReader = new ScmLineReader(scmInfoOptional.get());
        readers.add(scmLineReader);
    }
    RangeOffsetConverter rangeOffsetConverter = new RangeOffsetConverter();
    CloseableIterator<ScannerReport.SyntaxHighlightingRule> highlightingIt = reportReader.readComponentSyntaxHighlighting(componentRef);
    closeables.add(highlightingIt);
    readers.add(new HighlightingLineReader(component, highlightingIt, rangeOffsetConverter));
    CloseableIterator<ScannerReport.Symbol> symbolsIt = reportReader.readComponentSymbols(componentRef);
    closeables.add(symbolsIt);
    readers.add(new SymbolsLineReader(component, symbolsIt, rangeOffsetConverter));
    readers.add(new DuplicationLineReader(duplicationRepository.getDuplications(component)));
    readers.add(new IsNewLineReader(newLinesRepository, component));
    return new LineReadersImpl(readers, scmLineReader, closeables);
}
Also used : CoverageLineReader(org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader) CloseableIterator(org.sonar.core.util.CloseableIterator) SymbolsLineReader(org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader) ArrayList(java.util.ArrayList) IsNewLineReader(org.sonar.ce.task.projectanalysis.source.linereader.IsNewLineReader) HighlightingLineReader(org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader) DuplicationLineReader(org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader) DuplicationLineReader(org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) LineReader(org.sonar.ce.task.projectanalysis.source.linereader.LineReader) SymbolsLineReader(org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader) IsNewLineReader(org.sonar.ce.task.projectanalysis.source.linereader.IsNewLineReader) CoverageLineReader(org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader) HighlightingLineReader(org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader) ScmLineReader(org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader) RangeOffsetConverter(org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter) ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo)

Aggregations

CloseableIterator (org.sonar.core.util.CloseableIterator)3 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 Component (org.sonar.ce.task.projectanalysis.component.Component)2 LineReader (org.sonar.ce.task.projectanalysis.source.linereader.LineReader)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 Collections.singleton (java.util.Collections.singleton)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Random (java.util.Random)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1