Search in sources :

Example 16 with UserChange

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange in project sonarqube by SonarSource.

the class IssueUpdater method doSaveIssue.

private IssueDto doSaveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, Optional<RuleDefinitionDto> rule, ComponentDto project, BranchDto branchDto) {
    IssueDto issueDto = issueStorage.save(session, singletonList(issue)).iterator().next();
    if (// since this method is called after an update of the issue, date should never be null
    issue.updateDate() == null || // name of rule is displayed in notification, rule must therefor be present
    !rule.isPresent() || // notification are not supported on PRs
    !hasNotificationSupport(branchDto)) {
        return issueDto;
    }
    Optional<UserDto> assignee = Optional.ofNullable(issue.assignee()).map(assigneeUuid -> dbClient.userDao().selectByUuid(session, assigneeUuid));
    UserDto author = Optional.ofNullable(context.userUuid()).map(authorUuid -> dbClient.userDao().selectByUuid(session, authorUuid)).orElseThrow(() -> new IllegalStateException("Can not find dto for change author " + context.userUuid()));
    IssuesChangesNotificationBuilder notificationBuilder = new IssuesChangesNotificationBuilder(singleton(new ChangedIssue.Builder(issue.key()).setNewResolution(issue.resolution()).setNewStatus(issue.status()).setAssignee(assignee.map(assigneeDto -> new User(assigneeDto.getUuid(), assigneeDto.getLogin(), assigneeDto.getName())).orElse(null)).setRule(rule.map(r -> new Rule(r.getKey(), RuleType.valueOfNullable(r.getType()), r.getName())).get()).setProject(new Project.Builder(project.uuid()).setKey(project.getKey()).setProjectName(project.name()).setBranchName(branchDto.isMain() ? null : branchDto.getKey()).build()).build()), new UserChange(issue.updateDate().getTime(), new User(author.getUuid(), author.getLogin(), author.getName())));
    notificationService.scheduleForSending(notificationSerializer.serialize(notificationBuilder));
    return issueDto;
}
Also used : BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) RuleStatus(org.sonar.api.rule.RuleStatus) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) RuleType(org.sonar.api.rules.RuleType) Collections.singletonList(java.util.Collections.singletonList) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collections.singleton(java.util.Collections.singleton) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) DefaultIssue(org.sonar.core.issue.DefaultIssue) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DbClient(org.sonar.db.DbClient) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) RuleKey(org.sonar.api.rule.RuleKey) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) UserDto(org.sonar.db.user.UserDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule)

Example 17 with UserChange

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange in project sonarqube by SonarSource.

the class BulkChangeActionTest method send_notification_only_on_changed_issues.

@Test
public void send_notification_only_on_changed_issues() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user);
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    IssueDto issue1 = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG).setStatus(STATUS_OPEN).setResolution(null));
    IssueDto issue2 = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG).setStatus(STATUS_OPEN).setResolution(null));
    IssueDto issue3 = db.issues().insertIssue(rule, project, file, i -> i.setType(VULNERABILITY).setStatus(STATUS_OPEN).setResolution(null));
    BulkChangeWsResponse response = call(builder().setIssues(asList(issue1.getKey(), issue2.getKey(), issue3.getKey())).setSetType(RuleType.BUG.name()).setSendNotifications(true).build());
    checkResponse(response, 3, 1, 2, 0);
    verify(notificationManager).scheduleForSending(issueChangeNotificationCaptor.capture());
    assertThat(issueChangeNotificationCaptor.getAllValues()).hasSize(1);
    IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotificationCaptor.getValue());
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changedIssue = builder.getIssues().iterator().next();
    assertThat(changedIssue.getKey()).isEqualTo(issue3.getKey());
    assertThat(changedIssue.getNewStatus()).isEqualTo(STATUS_OPEN);
    assertThat(changedIssue.getNewResolution()).isEmpty();
    assertThat(changedIssue.getAssignee()).isEmpty();
    assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
    assertThat(changedIssue.getProject()).isEqualTo(projectOf(project));
    assertThat(builder.getChange()).isEqualTo(new UserChange(NOW, userOf(user)));
}
Also used : UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) BulkChangeWsResponse(org.sonarqube.ws.Issues.BulkChangeWsResponse) Test(org.junit.Test)

Example 18 with UserChange

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange in project sonarqube by SonarSource.

the class BulkChangeActionTest method send_notification_on_branch.

@Test
public void send_notification_on_branch() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user);
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature").setBranchType(BranchType.BRANCH));
    ComponentDto fileOnBranch = db.components().insertComponent(newFileDto(branch));
    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    IssueDto issue = db.issues().insertIssue(rule, branch, fileOnBranch, i -> i.setType(BUG).setStatus(STATUS_OPEN).setResolution(null));
    BulkChangeWsResponse response = call(builder().setIssues(singletonList(issue.getKey())).setDoTransition("confirm").setSendNotifications(true).build());
    checkResponse(response, 1, 1, 0, 0);
    verify(notificationManager).scheduleForSending(issueChangeNotificationCaptor.capture());
    IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotificationCaptor.getValue());
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changedIssue = builder.getIssues().iterator().next();
    assertThat(changedIssue.getKey()).isEqualTo(issue.getKey());
    assertThat(changedIssue.getNewStatus()).isEqualTo(STATUS_CONFIRMED);
    assertThat(changedIssue.getNewResolution()).isEmpty();
    assertThat(changedIssue.getAssignee()).isEmpty();
    assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
    assertThat(changedIssue.getProject()).isEqualTo(projectBranchOf(db, branch));
    assertThat(builder.getChange()).isEqualTo(new UserChange(NOW, userOf(user)));
    verifyPostProcessorCalled(fileOnBranch);
}
Also used : UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) BulkChangeWsResponse(org.sonarqube.ws.Issues.BulkChangeWsResponse) Test(org.junit.Test)

Example 19 with UserChange

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange in project sonarqube by SonarSource.

the class BulkChangeActionTest method send_notification.

@Test
public void send_notification() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user);
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    IssueDto issue = db.issues().insertIssue(rule, project, file, i -> i.setType(BUG).setStatus(STATUS_OPEN).setResolution(null));
    BulkChangeWsResponse response = call(builder().setIssues(singletonList(issue.getKey())).setDoTransition("confirm").setSendNotifications(true).build());
    checkResponse(response, 1, 1, 0, 0);
    verify(notificationManager).scheduleForSending(issueChangeNotificationCaptor.capture());
    IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotificationCaptor.getValue());
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changedIssue = builder.getIssues().iterator().next();
    assertThat(changedIssue.getKey()).isEqualTo(issue.getKey());
    assertThat(changedIssue.getProject().getUuid()).isEqualTo(project.uuid());
    assertThat(changedIssue.getProject().getKey()).isEqualTo(project.getKey());
    assertThat(changedIssue.getProject().getProjectName()).isEqualTo(project.name());
    assertThat(changedIssue.getProject().getBranchName()).isEmpty();
    assertThat(changedIssue.getRule().getKey()).isEqualTo(rule.getKey());
    assertThat(changedIssue.getRule().getName()).isEqualTo(rule.getName());
    assertThat(builder.getChange().getDate()).isEqualTo(NOW);
    assertThat(builder.getChange()).isInstanceOf(UserChange.class);
    UserChange userChange = (UserChange) builder.getChange();
    assertThat(userChange.getUser().getUuid()).isEqualTo(user.getUuid());
    assertThat(userChange.getUser().getLogin()).isEqualTo(user.getLogin());
    assertThat(userChange.getUser().getName()).contains(user.getName());
}
Also used : UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) BulkChangeWsResponse(org.sonarqube.ws.Issues.BulkChangeWsResponse) Test(org.junit.Test)

Example 20 with UserChange

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange in project sonarqube by SonarSource.

the class IssueUpdaterTest method verify_notification_on_branch.

@Test
public void verify_notification_on_branch() {
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto branch = db.components().insertProjectBranch(project, t -> t.setBranchType(BRANCH));
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    DefaultIssue issue = db.issues().insertIssue(rule, branch, file, t -> t.setSeverity(MAJOR)).toDefaultIssue();
    UserDto changeAuthor = db.users().insertUser();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
    issueFieldsSetter.setSeverity(issue, BLOCKER, context);
    underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
    verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
    IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
    IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changedIssue = builder.getIssues().iterator().next();
    assertThat(changedIssue.getKey()).isEqualTo(issue.key());
    assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
    assertThat(changedIssue.getNewResolution()).isEmpty();
    assertThat(changedIssue.getAssignee()).isEmpty();
    assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
    assertThat(changedIssue.getProject()).isEqualTo(projectBranchOf(db, branch));
    assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
}
Also used : MAJOR(org.sonar.api.rule.Severity.MAJOR) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) Date(java.util.Date) EsTester(org.sonar.server.es.EsTester) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) RuleStatus(org.sonar.api.rule.RuleStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) IssueFieldsSetter(org.sonar.server.issue.IssueFieldsSetter) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) ArgumentCaptor(org.mockito.ArgumentCaptor) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) NotificationManager(org.sonar.server.notification.NotificationManager) RESOLUTION_FIXED(org.sonar.api.issue.Issue.RESOLUTION_FIXED) DbTester(org.sonar.db.DbTester) BranchType(org.sonar.db.component.BranchType) TestIssueChangePostProcessor(org.sonar.server.issue.TestIssueChangePostProcessor) DefaultIssue(org.sonar.core.issue.DefaultIssue) System2(org.sonar.api.utils.System2) DefaultRuleFinder(org.sonar.server.rule.DefaultRuleFinder) IssuesChangesNotificationBuilderTesting.ruleOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.ruleOf) IssuesChangesNotificationBuilderTesting.userOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.userOf) Test(org.junit.Test) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) BRANCH(org.sonar.db.component.BranchType.BRANCH) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) IssuesChangesNotificationBuilderTesting.projectOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectOf) SequenceUuidFactory(org.sonar.core.util.SequenceUuidFactory) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) IssuesChangesNotificationBuilderTesting.projectBranchOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectBranchOf) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Mockito.mock(org.mockito.Mockito.mock) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Date(java.util.Date) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Test(org.junit.Test)

Aggregations

UserChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange)40 Test (org.junit.Test)35 ChangedIssue (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue)27 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)21 Random (java.util.Random)21 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)20 Mockito.mock (org.mockito.Mockito.mock)20 Project (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project)20 User (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User)19 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)18 List (java.util.List)17 Set (java.util.Set)17 AnalysisChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)16 Collectors.toSet (java.util.stream.Collectors.toSet)16 IntStream (java.util.stream.IntStream)16 Stream (java.util.stream.Stream)16 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)16 RunWith (org.junit.runner.RunWith)16