Search in sources :

Example 26 with IssueChangeContext

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

the class AddCommentAction method handle.

@Override
public void handle(Request request, Response response) {
    userSession.checkLoggedIn();
    AddCommentRequest wsRequest = toWsRequest(request);
    try (DbSession dbSession = dbClient.openSession(false)) {
        IssueDto issueDto = issueFinder.getByKey(dbSession, wsRequest.getIssue());
        IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid());
        DefaultIssue defaultIssue = issueDto.toDefaultIssue();
        issueFieldsSetter.addComment(defaultIssue, wsRequest.getText(), context);
        SearchResponseData preloadedSearchResponseData = issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, defaultIssue, context, false);
        responseWriter.write(defaultIssue.key(), preloadedSearchResponseData, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Example 27 with IssueChangeContext

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

the class AddCommentAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    hotspotWsSupport.checkLoggedIn();
    String hotspotKey = request.mandatoryParam(PARAM_HOTSPOT_KEY);
    String comment = request.mandatoryParam(PARAM_COMMENT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey);
        hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.USER);
        DefaultIssue defaultIssue = hotspot.toDefaultIssue();
        IssueChangeContext context = hotspotWsSupport.newIssueChangeContext();
        issueFieldsSetter.addComment(defaultIssue, comment, context);
        issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, defaultIssue, context, false);
        response.noContent();
    }
}
Also used : DbSession(org.sonar.db.DbSession) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue)

Example 28 with IssueChangeContext

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

the class ChangeStatusActionTest method success_to_change_hostpot_to_review_into_reviewed_status.

@Test
@UseDataProvider("reviewedResolutionsAndExpectedTransitionKey")
public void success_to_change_hostpot_to_review_into_reviewed_status(String resolution, String expectedTransitionKey, boolean transitionDone) {
    long now = RANDOM.nextInt(232_323);
    when(system2.now()).thenReturn(now);
    ComponentDto project = dbTester.components().insertPublicProject();
    userSessionRule.logIn().registerComponents(project).addProjectPermission(UserRole.SECURITYHOTSPOT_ADMIN, project);
    ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
    IssueDto hotspot = dbTester.issues().insertHotspot(project, file, h -> h.setStatus(STATUS_TO_REVIEW).setResolution(null));
    when(transitionService.doTransition(any(), any(), any())).thenReturn(transitionDone);
    newRequest(hotspot, STATUS_REVIEWED, resolution, NO_COMMENT).execute().assertNoContent();
    IssueChangeContext issueChangeContext = IssueChangeContext.createUser(new Date(now), userSessionRule.getUuid());
    ArgumentCaptor<DefaultIssue> defaultIssueCaptor = ArgumentCaptor.forClass(DefaultIssue.class);
    verify(transitionService).checkTransitionPermission(eq(expectedTransitionKey), defaultIssueCaptor.capture());
    verify(transitionService).doTransition(defaultIssueCaptor.capture(), eq(issueChangeContext), eq(expectedTransitionKey));
    if (transitionDone) {
        verify(issueUpdater).saveIssueAndPreloadSearchResponseData(any(DbSession.class), defaultIssueCaptor.capture(), eq(issueChangeContext), eq(true));
        // because it is mutated by FieldSetter and IssueUpdater, the same object must be passed to all methods
        verifyAllSame3Objects(defaultIssueCaptor.getAllValues());
        verifyNoInteractions(issueFieldsSetter);
    } else {
        verifyNoInteractions(issueUpdater, issueFieldsSetter);
    }
}
Also used : DbSession(org.sonar.db.DbSession) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ComponentDto(org.sonar.db.component.ComponentDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 29 with IssueChangeContext

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

the class IssueUpdaterTest method saveIssue_populates_specified_SearchResponseData_with_no_rule_but_with_project_and_component_if_rule_is_removed.

@Test
public void saveIssue_populates_specified_SearchResponseData_with_no_rule_but_with_project_and_component_if_rule_is_removed() {
    RuleDefinitionDto rule = db.rules().insertIssueRule(r -> r.setStatus(RuleStatus.REMOVED));
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    IssueDto issueDto = db.issues().insertIssue(rule, project, file);
    DefaultIssue issue = issueDto.setSeverity(MAJOR).toDefaultIssue();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid");
    issueFieldsSetter.setSeverity(issue, BLOCKER, context);
    SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
    assertThat(preloadedSearchResponseData.getIssues()).hasSize(1);
    assertThat(preloadedSearchResponseData.getIssues().iterator().next()).isNotSameAs(issueDto);
    assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty();
    assertThat(preloadedSearchResponseData.getComponents()).extracting(ComponentDto::uuid).containsOnly(project.uuid(), file.uuid());
}
Also used : IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test)

Example 30 with IssueChangeContext

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

the class IssueUpdaterTest method saveIssue_populates_specified_SearchResponseData_with_rule_project_and_component_retrieved_from_DB.

@Test
public void saveIssue_populates_specified_SearchResponseData_with_rule_project_and_component_retrieved_from_DB() {
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    IssueDto issueDto = db.issues().insertIssue(rule, project, file);
    DefaultIssue issue = issueDto.setSeverity(MAJOR).toDefaultIssue();
    UserDto changeAuthor = db.users().insertUser();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
    issueFieldsSetter.setSeverity(issue, BLOCKER, context);
    SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, true);
    assertThat(preloadedSearchResponseData.getIssues()).hasSize(1);
    assertThat(preloadedSearchResponseData.getIssues().iterator().next()).isNotSameAs(issueDto);
    assertThat(preloadedSearchResponseData.getRules()).extracting(RuleDefinitionDto::getKey).containsOnly(rule.getKey());
    assertThat(preloadedSearchResponseData.getComponents()).extracting(ComponentDto::uuid).containsOnly(project.uuid(), file.uuid());
    assertThat(issueChangePostProcessor.calledComponents()).containsExactlyInAnyOrder(file);
}
Also used : IssueChangeContext(org.sonar.core.issue.IssueChangeContext) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test)

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