Search in sources :

Example 16 with IssueChangeContext

use of org.sonar.core.issue.IssueChangeContext in project sonarqube by SonarSource.

the class MovedIssueVisitorTest method onIssue_update_component_and_module_fields_to_component_and_flag_issue_has_changed.

@Test
public void onIssue_update_component_and_module_fields_to_component_and_flag_issue_has_changed() {
    MovedFilesRepository.OriginalFile originalFile = new MovedFilesRepository.OriginalFile(6451, "original uuid", "original key");
    DefaultIssue issue = mockIssue(originalFile.getUuid());
    when(movedFilesRepository.getOriginalFile(FILE)).thenReturn(Optional.of(originalFile));
    underTest.onIssue(FILE, issue);
    verify(issue).setComponentUuid(FILE.getUuid());
    verify(issue).setComponentKey(FILE.getKey());
    verify(issue).setModuleUuid(null);
    verify(issue).setModuleUuidPath(null);
    verify(issue).setChanged(true);
    ArgumentCaptor<IssueChangeContext> issueChangeContextCaptor = ArgumentCaptor.forClass(IssueChangeContext.class);
    verify(issue).setFieldChange(issueChangeContextCaptor.capture(), eq("file"), eq(originalFile.getUuid()), eq(FILE.getUuid()));
    assertThat(issueChangeContextCaptor.getValue().date()).isEqualTo(new Date(ANALYSIS_DATE));
    assertThat(issueChangeContextCaptor.getValue().login()).isNull();
    assertThat(issueChangeContextCaptor.getValue().scan()).isFalse();
}
Also used : IssueChangeContext(org.sonar.core.issue.IssueChangeContext) MovedFilesRepository(org.sonar.server.computation.task.projectanalysis.filemove.MovedFilesRepository) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test)

Example 17 with IssueChangeContext

use of org.sonar.core.issue.IssueChangeContext in project sonarqube by SonarSource.

the class IssueUpdaterTest method verify_notification.

@Test
public void verify_notification() throws Exception {
    RuleDto rule = ruleDbTester.insertRule(newRuleDto());
    ComponentDto project = componentDbTester.insertProject();
    ComponentDto file = componentDbTester.insertComponent(newFileDto(project));
    DefaultIssue issue = issueDbTester.insertIssue(newDto(rule, file, project)).setSeverity(MAJOR).toDefaultIssue();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), "john");
    issueFieldsSetter.setSeverity(issue, BLOCKER, context);
    underTest.saveIssue(dbTester.getSession(), issue, context, "increase severity");
    verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
    IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue();
    assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key());
    assertThat(issueChangeNotification.getFieldValue("old.severity")).isEqualTo(MAJOR);
    assertThat(issueChangeNotification.getFieldValue("new.severity")).isEqualTo(BLOCKER);
    assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.key());
    assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName());
    assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.key());
    assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name());
    assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(rule.getName());
    assertThat(issueChangeNotification.getFieldValue("changeAuthor")).isEqualTo("john");
    assertThat(issueChangeNotification.getFieldValue("comment")).isEqualTo("increase severity");
}
Also used : IssueChangeContext(org.sonar.core.issue.IssueChangeContext) RuleTesting.newRuleDto(org.sonar.db.rule.RuleTesting.newRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ComponentDto(org.sonar.db.component.ComponentDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssueChangeNotification(org.sonar.server.issue.notification.IssueChangeNotification) Date(java.util.Date) Test(org.junit.Test)

Example 18 with IssueChangeContext

use of org.sonar.core.issue.IssueChangeContext in project sonarqube by SonarSource.

the class IssueService method assign.

public void assign(String issueKey, @Nullable String assignee) {
    userSession.checkLoggedIn();
    DbSession session = dbClient.openSession(false);
    try {
        DefaultIssue issue = issueFinder.getByKey(session, issueKey).toDefaultIssue();
        User user = null;
        if (!Strings.isNullOrEmpty(assignee)) {
            user = userFinder.findByLogin(assignee);
            checkRequest(user != null, "Unknown user: %s", assignee);
        }
        IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
        if (issueFieldsSetter.assign(issue, user, context)) {
            issueUpdater.saveIssue(session, issue, context, null);
        }
    } finally {
        session.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) User(org.sonar.api.user.User) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Example 19 with IssueChangeContext

use of org.sonar.core.issue.IssueChangeContext in project sonarqube by SonarSource.

the class IssueService method setTags.

public Collection<String> setTags(String issueKey, Collection<String> tags) {
    userSession.checkLoggedIn();
    DbSession session = dbClient.openSession(false);
    try {
        DefaultIssue issue = issueFinder.getByKey(session, issueKey).toDefaultIssue();
        IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
        if (issueFieldsSetter.setTags(issue, tags, context)) {
            issueUpdater.saveIssue(session, issue, context, null);
        }
        return issue.tags();
    } finally {
        session.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Example 20 with IssueChangeContext

use of org.sonar.core.issue.IssueChangeContext in project sonarqube by SonarSource.

the class BulkChangeAction method executeBulkChange.

private BulkChangeResult executeBulkChange(DbSession dbSession, Request request) {
    BulkChangeData bulkChangeData = new BulkChangeData(dbSession, request);
    BulkChangeResult result = new BulkChangeResult(bulkChangeData.issues.size());
    IssueChangeContext issueChangeContext = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid());
    List<DefaultIssue> items = bulkChangeData.issues.stream().filter(bulkChange(issueChangeContext, bulkChangeData, result)).collect(MoreCollectors.toList());
    issueStorage.save(dbSession, items);
    refreshLiveMeasures(dbSession, bulkChangeData, result);
    Set<String> assigneeUuids = items.stream().map(DefaultIssue::assignee).filter(Objects::nonNull).collect(Collectors.toSet());
    Map<String, UserDto> userDtoByUuid = dbClient.userDao().selectByUuids(dbSession, assigneeUuids).stream().collect(toMap(UserDto::getUuid, u -> u));
    String authorUuid = requireNonNull(userSession.getUuid(), "User uuid cannot be null");
    UserDto author = dbClient.userDao().selectByUuid(dbSession, authorUuid);
    checkState(author != null, "User with uuid '%s' does not exist");
    sendNotification(items, bulkChangeData, userDtoByUuid, author);
    return result;
}
Also used : PARAM_SET_SEVERITY(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_SEVERITY) Date(java.util.Date) ActionContext(org.sonar.server.issue.ActionContext) BUG(org.sonar.api.rules.RuleType.BUG) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Loggers(org.sonar.api.utils.log.Loggers) SET_SEVERITY_KEY(org.sonar.server.issue.SetSeverityAction.SET_SEVERITY_KEY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PARAM_ADD_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ADD_TAGS) WebService(org.sonar.api.server.ws.WebService) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collectors.toMap(java.util.stream.Collectors.toMap) UUID_EXAMPLE_01(org.sonar.core.util.Uuids.UUID_EXAMPLE_01) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) UUID_EXAMPLE_02(org.sonar.core.util.Uuids.UUID_EXAMPLE_02) SET_TYPE_KEY(org.sonar.server.issue.SetTypeAction.SET_TYPE_KEY) TRANSITION_PARAMETER(org.sonar.server.issue.TransitionAction.TRANSITION_PARAMETER) Map(java.util.Map) PARAM_ASSIGN(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGN) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) PARAM_REMOVE_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_REMOVE_TAGS) PARAM_SET_TYPE(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_TYPE) NotificationManager(org.sonar.server.notification.NotificationManager) PARAM_COMMENT(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMMENT) PARAM_DO_TRANSITION(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DO_TRANSITION) TAGS_PARAMETER(org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER) System2(org.sonar.api.utils.System2) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) OPEN_AS_VULNERABILITY(org.sonar.api.issue.DefaultTransitions.OPEN_AS_VULNERABILITY) Predicate(java.util.function.Predicate) ASSIGNEE_PARAMETER(org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) AddTagsAction(org.sonar.server.issue.AddTagsAction) PARAM_SEND_NOTIFICATIONS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEND_NOTIFICATIONS) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ACTION_BULK_CHANGE(org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_BULK_CHANGE) RuleKey(org.sonar.api.rule.RuleKey) Function.identity(java.util.function.Function.identity) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) Issues(org.sonarqube.ws.Issues) MAX_PAGE_SIZE(org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE) SET_AS_IN_REVIEW(org.sonar.api.issue.DefaultTransitions.SET_AS_IN_REVIEW) HashMap(java.util.HashMap) PARAM_ISSUES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUES) RuleType(org.sonar.api.rules.RuleType) COMMENT_PROPERTY(org.sonar.server.issue.CommentAction.COMMENT_PROPERTY) HashSet(java.util.HashSet) Action(org.sonar.server.issue.Action) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) RemoveTagsAction(org.sonar.server.issue.RemoveTagsAction) MoreCollectors.toSet(org.sonar.core.util.stream.MoreCollectors.toSet) Severity(org.sonar.api.rule.Severity) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) REOPEN(org.sonar.api.issue.DefaultTransitions.REOPEN) COMMENT_KEY(org.sonar.server.issue.CommentAction.COMMENT_KEY) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) BranchType(org.sonar.db.component.BranchType) DefaultIssue(org.sonar.core.issue.DefaultIssue) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Consumer(java.util.function.Consumer) UserRole(org.sonar.api.web.UserRole) DO_TRANSITION_KEY(org.sonar.server.issue.TransitionAction.DO_TRANSITION_KEY) DefaultTransitions(org.sonar.api.issue.DefaultTransitions) RESOLVE_AS_REVIEWED(org.sonar.api.issue.DefaultTransitions.RESOLVE_AS_REVIEWED) Change(org.sonar.api.server.ws.Change) AssignAction(org.sonar.server.issue.AssignAction) UserSession(org.sonar.server.user.UserSession) SEVERITY_PARAMETER(org.sonar.server.issue.SetSeverityAction.SEVERITY_PARAMETER) CheckForNull(javax.annotation.CheckForNull) TYPE_PARAMETER(org.sonar.server.issue.SetTypeAction.TYPE_PARAMETER) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) UserDto(org.sonar.db.user.UserDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Aggregations

IssueChangeContext (org.sonar.core.issue.IssueChangeContext)34 DefaultIssue (org.sonar.core.issue.DefaultIssue)33 Date (java.util.Date)30 IssueDto (org.sonar.db.issue.IssueDto)23 Test (org.junit.Test)19 ComponentDto (org.sonar.db.component.ComponentDto)17 DbSession (org.sonar.db.DbSession)13 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)12 UserDto (org.sonar.db.user.UserDto)12 DbClient (org.sonar.db.DbClient)9 WebIssueStorage (org.sonar.server.issue.WebIssueStorage)9 IssuesChangesNotificationBuilder (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder)9 ChangedIssue (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue)9 UserChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange)9 IssuesChangesNotificationSerializer (org.sonar.server.issue.notification.IssuesChangesNotificationSerializer)9 NotificationManager (org.sonar.server.notification.NotificationManager)9 RuleStatus (org.sonar.api.rule.RuleStatus)8 BLOCKER (org.sonar.api.rule.Severity.BLOCKER)8 System2 (org.sonar.api.utils.System2)8 BranchType (org.sonar.db.component.BranchType)8