Search in sources :

Example 1 with FieldDiffs

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

the class NewEffortCalculator method calculateFromChangelog.

private static long calculateFromChangelog(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, long periodDate) {
    List<FieldDiffs> debtDiffs = from(debtChangelog).transform(ToFieldDiffs.INSTANCE).filter(HasDebtChange.INSTANCE).toSortedList(CHANGE_ORDERING);
    FieldDiffs currentChange = issue.currentChange();
    if (currentChange != null && HasDebtChange.INSTANCE.apply(currentChange)) {
        debtDiffs = Lists.newArrayList(debtDiffs);
        debtDiffs.add(currentChange);
    }
    long newDebt = issue.effortInMinutes();
    for (Iterator<FieldDiffs> it = debtDiffs.iterator(); it.hasNext(); ) {
        FieldDiffs diffs = it.next();
        Date date = diffs.creationDate();
        // TODO use longs
        if (isBeforeOrEqual(date, new Date(periodDate))) {
            // return new value from the change that is just before the period date
            return subtract(newDebt, debtDiff(diffs).newValueLong());
        }
        if (!it.hasNext()) {
            // return old value from the change that is just after the period date when there's no more element in changelog
            return subtract(newDebt, debtDiff(diffs).oldValueLong());
        }
    }
    // no changelog
    return 0L;
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Date(java.util.Date)

Example 2 with FieldDiffs

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

the class IssueChangeNotification method setIssue.

public IssueChangeNotification setIssue(DefaultIssue issue) {
    setFieldValue("key", issue.key());
    setFieldValue("assignee", issue.assignee());
    setFieldValue("message", issue.message());
    setFieldValue("componentKey", issue.componentKey());
    FieldDiffs currentChange = issue.currentChange();
    if (currentChange != null) {
        for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) {
            String type = entry.getKey();
            FieldDiffs.Diff diff = entry.getValue();
            setFieldValue("old." + type, neverEmptySerializableToString(diff.oldValue()));
            setFieldValue("new." + type, neverEmptySerializableToString(diff.newValue()));
        }
    }
    return this;
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Map(java.util.Map)

Example 3 with FieldDiffs

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

the class PersistIssuesStepTest method add_change.

@Test
public void add_change() {
    dbTester.prepareDbUnit(getClass(), "shared.xml");
    issueCache.newAppender().append(new DefaultIssue().setKey("ISSUE").setType(RuleType.CODE_SMELL).setRuleKey(RuleKey.of("xoo", "S01")).setComponentUuid("COMPONENT").setProjectUuid("PROJECT").setSeverity(Severity.BLOCKER).setStatus(Issue.STATUS_CLOSED).setResolution(Issue.RESOLUTION_FIXED).setNew(false).setChanged(true).setCurrentChange(new FieldDiffs().setIssueKey("ISSUE").setUserLogin("john").setDiff("technicalDebt", null, 1L))).close();
    step.execute();
    dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] { "id", "created_at", "updated_at" }, "issue_changes");
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 4 with FieldDiffs

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

the class IssueChangeNotificationTest method set_issue_with_current_change_having_no_old_value.

@Test
public void set_issue_with_current_change_having_no_old_value() {
    DefaultIssue issue = new DefaultIssue().setKey("ABCD").setAssignee("simon").setMessage("Remove this useless method").setComponentKey("MyService");
    IssueChangeNotification result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("resolution", null, "FIXED")));
    assertThat(result.getFieldValue("old.resolution")).isNull();
    assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
    result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("resolution", "", "FIXED")));
    assertThat(result.getFieldValue("old.resolution")).isNull();
    assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

Example 5 with FieldDiffs

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

the class IssueChangeNotificationTest method set_issue.

@Test
public void set_issue() {
    DefaultIssue issue = new DefaultIssue().setKey("ABCD").setAssignee("simon").setMessage("Remove this useless method").setComponentKey("MyService").setCurrentChange(new FieldDiffs().setDiff("resolution", "FALSE-POSITIVE", "FIXED"));
    IssueChangeNotification result = notification.setIssue(issue);
    assertThat(result.getFieldValue("key")).isEqualTo("ABCD");
    assertThat(result.getFieldValue("assignee")).isEqualTo("simon");
    assertThat(result.getFieldValue("message")).isEqualTo("Remove this useless method");
    assertThat(result.getFieldValue("componentKey")).isEqualTo("MyService");
    assertThat(result.getFieldValue("old.resolution")).isEqualTo("FALSE-POSITIVE");
    assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

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