Search in sources :

Example 31 with FieldDiffs

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

the class IssueChangeDtoTest method to_field_diffs_with_create_at.

@Test
public void to_field_diffs_with_create_at() {
    IssueChangeDto changeDto = new IssueChangeDto().setKey("EFGH").setUserUuid("user_uuid").setChangeData("Some text").setIssueKey("ABCDE").setCreatedAt(System2.INSTANCE.now());
    FieldDiffs diffs = changeDto.toFieldDiffs();
    assertThat(diffs.userUuid()).isEqualTo("user_uuid");
    assertThat(diffs.issueKey()).isEqualTo("ABCDE");
    assertThat(diffs.creationDate()).isNotNull();
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Test(org.junit.Test)

Example 32 with FieldDiffs

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

the class IssueChangeDtoTest method create_from_diff.

@Test
public void create_from_diff() {
    FieldDiffs diffs = new FieldDiffs();
    diffs.setDiff("severity", "INFO", "BLOCKER");
    diffs.setUserUuid("user_uuid");
    diffs.setCreationDate(parseDate("2015-01-13"));
    IssueChangeDto dto = IssueChangeDto.of("ABCDE", diffs, "project_uuid");
    assertThat(dto.getChangeData()).isEqualTo("severity=INFO|BLOCKER");
    assertThat(dto.getChangeType()).isEqualTo("diff");
    assertThat(dto.getCreatedAt()).isNotNull();
    assertThat(dto.getUpdatedAt()).isNotNull();
    assertThat(dto.getIssueKey()).isEqualTo("ABCDE");
    assertThat(dto.getUserUuid()).isEqualTo("user_uuid");
    assertThat(dto.getIssueChangeCreationDate()).isEqualTo(parseDate("2015-01-13").getTime());
    assertThat(dto.getProjectUuid()).isEqualTo("project_uuid");
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Test(org.junit.Test)

Example 33 with FieldDiffs

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

the class IssueWorkflowTest method addResolutionAndStatusChange.

private void addResolutionAndStatusChange(DefaultIssue issue, Date creationDate, String previousStatus, String newStatus, @Nullable String previousResolution, @Nullable String newResolution) {
    checkArgument(previousResolution != null || newResolution != null, "At least one resolution must be non null");
    FieldDiffs fieldDiffs = new FieldDiffs().setCreationDate(creationDate).setDiff("status", previousStatus, newStatus).setDiff("resolution", emptyIfNull(previousResolution), emptyIfNull(newResolution));
    issue.addChange(fieldDiffs);
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs)

Example 34 with FieldDiffs

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

the class PersistIssuesStepTest method insert_copied_issue.

@Test
public void insert_copied_issue() {
    RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
    db.rules().insert(rule);
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project, null));
    when(system2.now()).thenReturn(NOW);
    String issueKey = "ISSUE-1";
    protoIssueCache.newAppender().append(new DefaultIssue().setKey(issueKey).setType(RuleType.CODE_SMELL).setRuleKey(rule.getKey()).setComponentUuid(file.uuid()).setComponentKey(file.getKey()).setProjectUuid(project.uuid()).setProjectKey(project.getKey()).setSeverity(BLOCKER).setStatus(STATUS_OPEN).setTags(singletonList("test")).setNew(false).setCopied(true).setType(RuleType.BUG).setCreationDate(new Date(NOW)).setSelectedAt(NOW).addComment(new DefaultIssueComment().setKey("COMMENT").setIssueKey(issueKey).setUserUuid("john_uuid").setMarkdownText("Some text").setCreatedAt(new Date(NOW)).setUpdatedAt(new Date(NOW)).setNew(true)).setCurrentChange(new FieldDiffs().setIssueKey(issueKey).setUserUuid("john_uuid").setDiff("technicalDebt", null, 1L).setCreationDate(new Date(NOW)))).close();
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    IssueDto result = dbClient.issueDao().selectOrFailByKey(session, issueKey);
    assertThat(result.getKey()).isEqualTo(issueKey);
    assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
    assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
    assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
    assertThat(result.getSeverity()).isEqualTo(BLOCKER);
    assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
    assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
    assertThat(result.getTags()).containsExactlyInAnyOrder("test");
    assertThat(result.isNewCodeReferenceIssue()).isFalse();
    List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList(issueKey));
    assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
    assertThat(context.getStatistics().getAll()).contains(entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"));
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FieldDiffs(org.sonar.core.issue.FieldDiffs) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) Test(org.junit.Test)

Example 35 with FieldDiffs

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

the class ComponentIssuesLoader method loadLatestDiffChangesForReopeningOfClosedIssues.

/**
 * To be efficient both in term of memory and speed:
 * <ul>
 *   <li>only diff changes are loaded from DB, sorted by issue and then change creation date</li>
 *   <li>data from DB is streamed</li>
 *   <li>only the latest change(s) with status and resolution are added to the {@link DefaultIssue} objects</li>
 * </ul>
 */
private void loadLatestDiffChangesForReopeningOfClosedIssues(DbSession dbSession, Collection<DefaultIssue> issues) {
    Map<String, DefaultIssue> issuesByKey = issues.stream().collect(uniqueIndex(DefaultIssue::key));
    dbClient.issueChangeDao().scrollDiffChangesOfIssues(dbSession, issuesByKey.keySet(), new ResultHandler<IssueChangeDto>() {

        private DefaultIssue currentIssue = null;

        private boolean previousStatusFound = false;

        private boolean previousResolutionFound = false;

        @Override
        public void handleResult(ResultContext<? extends IssueChangeDto> resultContext) {
            IssueChangeDto issueChangeDto = resultContext.getResultObject();
            if (currentIssue == null || !currentIssue.key().equals(issueChangeDto.getIssueKey())) {
                currentIssue = issuesByKey.get(issueChangeDto.getIssueKey());
                previousStatusFound = false;
                previousResolutionFound = false;
            }
            if (currentIssue != null) {
                FieldDiffs fieldDiffs = issueChangeDto.toFieldDiffs();
                boolean hasPreviousStatus = fieldDiffs.get("status") != null;
                boolean hasPreviousResolution = fieldDiffs.get("resolution") != null;
                if ((!previousStatusFound && hasPreviousStatus) || (!previousResolutionFound && hasPreviousResolution)) {
                    currentIssue.addChange(fieldDiffs);
                }
                previousStatusFound |= hasPreviousStatus;
                previousResolutionFound |= hasPreviousResolution;
            }
        }
    });
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue)

Aggregations

FieldDiffs (org.sonar.core.issue.FieldDiffs)57 Test (org.junit.Test)42 IssueDto (org.sonar.db.issue.IssueDto)31 Date (java.util.Date)25 IssueChangeDto (org.sonar.db.issue.IssueChangeDto)18 UserDto (org.sonar.db.user.UserDto)18 DefaultIssue (org.sonar.core.issue.DefaultIssue)12 FormattingContext (org.sonar.server.issue.IssueChangeWSSupport.FormattingContext)12 ChangelogWsResponse (org.sonarqube.ws.Issues.ChangelogWsResponse)12 ComponentDto (org.sonar.db.component.ComponentDto)11 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)9 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)8 DefaultIssueComment (org.sonar.core.issue.DefaultIssueComment)8 ArrayList (java.util.ArrayList)6 Random (java.util.Random)6 Changelog (org.sonarqube.ws.Common.Changelog)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)5 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)5 Arrays (java.util.Arrays)5