Search in sources :

Example 1 with ProtoIssueCache

use of org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache 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 ProtoIssueCache

use of org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache in project sonarqube by SonarSource.

the class SendIssueNotificationsStepTest method setUp.

@Before
public void setUp() throws Exception {
    protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
    underTest = new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient());
    when(notificationFactory.newNewIssuesNotification(any(assigneeCacheType))).thenReturn(newIssuesNotificationMock);
    when(notificationFactory.newMyNewIssuesNotification(any(assigneeCacheType))).thenReturn(myNewIssuesNotificationMock);
}
Also used : ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) Before(org.junit.Before)

Example 3 with ProtoIssueCache

use of org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache in project sonarqube by SonarSource.

the class SendIssueNotificationsStepTest method send_new_issues_notification_to_user_only_for_those_assigned_to_her.

@Test
public void send_new_issues_notification_to_user_only_for_those_assigned_to_her() throws IOException {
    UserDto perceval = db.users().insertUser(u -> u.setLogin("perceval"));
    Integer[] assigned = IntStream.range(0, 5).mapToObj(i -> 10_000 * i).toArray(Integer[]::new);
    Duration expectedEffort = Duration.create(stream(assigned).mapToInt(i -> i).sum());
    UserDto arthur = db.users().insertUser(u -> u.setLogin("arthur"));
    Integer[] assignedToOther = IntStream.range(0, 3).mapToObj(i -> 10).toArray(Integer[]::new);
    List<DefaultIssue> issues = concat(stream(assigned).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(perceval.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE))), stream(assignedToOther).map(effort -> createIssue().setType(randomRuleType).setEffort(Duration.create(effort)).setAssigneeUuid(arthur.getUuid()).setNew(true).setCreationDate(new Date(ANALYSE_DATE)))).collect(toList());
    shuffle(issues);
    ProtoIssueCache protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
    DiskCache.CacheAppender newIssueCache = protoIssueCache.newAppender();
    issues.forEach(newIssueCache::append);
    newIssueCache.close();
    analysisMetadataHolder.setProject(new Project(PROJECT.getUuid(), PROJECT.getKey(), PROJECT.getName(), null, emptyList()));
    when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
    NotificationFactory notificationFactory = mock(NotificationFactory.class);
    NewIssuesNotification newIssuesNotificationMock = createNewIssuesNotificationMock();
    when(notificationFactory.newNewIssuesNotification(assigneeCacheCaptor.capture())).thenReturn(newIssuesNotificationMock);
    MyNewIssuesNotification myNewIssuesNotificationMock1 = createMyNewIssuesNotificationMock();
    MyNewIssuesNotification myNewIssuesNotificationMock2 = createMyNewIssuesNotificationMock();
    doReturn(myNewIssuesNotificationMock1).doReturn(myNewIssuesNotificationMock2).when(notificationFactory).newMyNewIssuesNotification(any(assigneeCacheType));
    TestComputationStepContext context = new TestComputationStepContext();
    new SendIssueNotificationsStep(protoIssueCache, treeRootHolder, notificationService, analysisMetadataHolder, notificationFactory, db.getDbClient()).execute(context);
    verify(notificationService).deliverEmails(ImmutableSet.of(myNewIssuesNotificationMock1, myNewIssuesNotificationMock2));
    // old API compatibility
    verify(notificationService).deliver(myNewIssuesNotificationMock1);
    verify(notificationService).deliver(myNewIssuesNotificationMock2);
    verify(notificationFactory).newNewIssuesNotification(assigneeCacheCaptor.capture());
    verify(notificationFactory, times(2)).newMyNewIssuesNotification(assigneeCacheCaptor.capture());
    verifyNoMoreInteractions(notificationFactory);
    verifyAssigneeCache(assigneeCacheCaptor, perceval, arthur);
    Map<String, MyNewIssuesNotification> myNewIssuesNotificationMocksByUsersName = new HashMap<>();
    ArgumentCaptor<UserDto> userCaptor1 = forClass(UserDto.class);
    verify(myNewIssuesNotificationMock1).setAssignee(userCaptor1.capture());
    myNewIssuesNotificationMocksByUsersName.put(userCaptor1.getValue().getLogin(), myNewIssuesNotificationMock1);
    ArgumentCaptor<UserDto> userCaptor2 = forClass(UserDto.class);
    verify(myNewIssuesNotificationMock2).setAssignee(userCaptor2.capture());
    myNewIssuesNotificationMocksByUsersName.put(userCaptor2.getValue().getLogin(), myNewIssuesNotificationMock2);
    MyNewIssuesNotification myNewIssuesNotificationMock = myNewIssuesNotificationMocksByUsersName.get("perceval");
    ArgumentCaptor<NewIssuesStatistics.Stats> statsCaptor = forClass(NewIssuesStatistics.Stats.class);
    verify(myNewIssuesNotificationMock).setStatistics(eq(PROJECT.getName()), statsCaptor.capture());
    verify(myNewIssuesNotificationMock).setDebt(expectedEffort);
    NewIssuesStatistics.Stats stats = statsCaptor.getValue();
    assertThat(stats.hasIssues()).isTrue();
    // just checking all issues have been added to the stats
    DistributedMetricStatsInt severity = stats.getDistributedMetricStats(NewIssuesStatistics.Metric.RULE_TYPE);
    assertThat(severity.getOnCurrentAnalysis()).isEqualTo(assigned.length);
    assertThat(severity.getTotal()).isEqualTo(assigned.length);
    verifyStatistics(context, 1, 2, 0);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Collections.shuffle(java.util.Collections.shuffle) NOTIF_TYPES(org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES) Duration(org.sonar.api.utils.Duration) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) ComputationStep(org.sonar.ce.task.step.ComputationStep) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) IssueTesting.newIssue(org.sonar.db.issue.IssueTesting.newIssue) Map(java.util.Map) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) ReportComponent.builder(org.sonar.ce.task.projectanalysis.component.ReportComponent.builder) Mockito.doReturn(org.mockito.Mockito.doReturn) NotificationService(org.sonar.server.notification.NotificationService) ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) DbTester(org.sonar.db.DbTester) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Notification(org.sonar.api.notifications.Notification) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) BRANCH(org.sonar.db.component.BranchType.BRANCH) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) DistributedMetricStatsInt(org.sonar.server.issue.notification.DistributedMetricStatsInt) RuleKey(org.sonar.api.rule.RuleKey) TreeRootHolderRule(org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule) Mockito.any(org.mockito.Mockito.any) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ComponentTesting.newBranchComponent(org.sonar.db.component.ComponentTesting.newBranchComponent) Supplier(java.util.function.Supplier) RuleTesting.newRule(org.sonar.db.rule.RuleTesting.newRule) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) Stream.concat(java.util.stream.Stream.concat) DefaultBranchImpl(org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) Tuple(org.assertj.core.groups.Tuple) BranchType(org.sonar.db.component.BranchType) RandomUtils.nextInt(org.apache.commons.lang.math.RandomUtils.nextInt) DefaultIssue(org.sonar.core.issue.DefaultIssue) Type(org.sonar.ce.task.projectanalysis.component.Component.Type) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) NotificationFactory(org.sonar.ce.task.projectanalysis.notification.NotificationFactory) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) TemporaryFolder(org.junit.rules.TemporaryFolder) Project(org.sonar.server.project.Project) ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) HashMap(java.util.HashMap) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) UserDto(org.sonar.db.user.UserDto) NotificationFactory(org.sonar.ce.task.projectanalysis.notification.NotificationFactory) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) Duration(org.sonar.api.utils.Duration) DefaultIssue(org.sonar.core.issue.DefaultIssue) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) Project(org.sonar.server.project.Project) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) DistributedMetricStatsInt(org.sonar.server.issue.notification.DistributedMetricStatsInt) Test(org.junit.Test)

Example 4 with ProtoIssueCache

use of org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache in project sonarqube by SonarSource.

the class PersistIssuesStepTest method setup.

@Before
public void setup() throws Exception {
    protoIssueCache = new ProtoIssueCache(temp.newFile(), System2.INSTANCE);
    reportReader.setMetadata(ScannerReport.Metadata.getDefaultInstance());
    underTest = new PersistIssuesStep(dbClient, system2, conflictResolver, new RuleRepositoryImpl(adHocRuleCreator, dbClient), protoIssueCache, new IssueStorage(), UuidFactoryImpl.INSTANCE);
}
Also used : ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) RuleRepositoryImpl(org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl) IssueStorage(org.sonar.server.issue.IssueStorage) Before(org.junit.Before)

Aggregations

ProtoIssueCache (org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache)4 Before (org.junit.Before)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collections.singleton (java.util.Collections.singleton)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors.toList (java.util.stream.Collectors.toList)2 Notification (org.sonar.api.notifications.Notification)2 RuleType (org.sonar.api.rules.RuleType)2 Duration (org.sonar.api.utils.Duration)2 Branch (org.sonar.ce.task.projectanalysis.analysis.Branch)2 Component (org.sonar.ce.task.projectanalysis.component.Component)2 NotificationFactory (org.sonar.ce.task.projectanalysis.notification.NotificationFactory)2 ComputationStep (org.sonar.ce.task.step.ComputationStep)2 DefaultIssue (org.sonar.core.issue.DefaultIssue)2 BranchType (org.sonar.db.component.BranchType)2 PULL_REQUEST (org.sonar.db.component.BranchType.PULL_REQUEST)2 UserDto (org.sonar.db.user.UserDto)2