Search in sources :

Example 36 with FieldDiffs

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

the class ProtobufIssueDiskCache method toProto.

private static IssueCache.Issue toProto(IssueCache.Issue.Builder builder, DefaultIssue defaultIssue) {
    builder.clear();
    builder.setKey(defaultIssue.key());
    builder.setRuleType(defaultIssue.type().getDbConstant());
    ofNullable(defaultIssue.componentUuid()).ifPresent(builder::setComponentUuid);
    builder.setComponentKey(defaultIssue.componentKey());
    ofNullable(defaultIssue.moduleUuid()).ifPresent(builder::setModuleUuid);
    ofNullable(defaultIssue.moduleUuidPath()).ifPresent(builder::setModuleUuidPath);
    builder.setProjectUuid(defaultIssue.projectUuid());
    builder.setProjectKey(defaultIssue.projectKey());
    builder.setRuleKey(defaultIssue.ruleKey().toString());
    ofNullable(defaultIssue.language()).ifPresent(builder::setLanguage);
    ofNullable(defaultIssue.severity()).ifPresent(builder::setSeverity);
    builder.setManualSeverity(defaultIssue.manualSeverity());
    ofNullable(defaultIssue.message()).ifPresent(builder::setMessage);
    ofNullable(defaultIssue.line()).ifPresent(builder::setLine);
    ofNullable(defaultIssue.gap()).ifPresent(builder::setGap);
    ofNullable(defaultIssue.effort()).map(Duration::toMinutes).ifPresent(builder::setEffort);
    builder.setStatus(defaultIssue.status());
    ofNullable(defaultIssue.resolution()).ifPresent(builder::setResolution);
    ofNullable(defaultIssue.assignee()).ifPresent(builder::setAssigneeUuid);
    ofNullable(defaultIssue.checksum()).ifPresent(builder::setChecksum);
    ofNullable(defaultIssue.authorLogin()).ifPresent(builder::setAuthorLogin);
    defaultIssue.defaultIssueComments().forEach(c -> builder.addComments(toProtoComment(c)));
    ofNullable(defaultIssue.tags()).ifPresent(t -> builder.setTags(String.join(TAGS_SEPARATOR, t)));
    ofNullable(defaultIssue.getLocations()).ifPresent(l -> builder.setLocations((DbIssues.Locations) l));
    builder.setIsFromExternalRuleEngine(defaultIssue.isFromExternalRuleEngine());
    builder.setCreationDate(defaultIssue.creationDate().getTime());
    ofNullable(defaultIssue.updateDate()).map(Date::getTime).ifPresent(builder::setUpdateDate);
    ofNullable(defaultIssue.closeDate()).map(Date::getTime).ifPresent(builder::setCloseDate);
    ofNullable(defaultIssue.currentChange()).ifPresent(c -> builder.setCurrentChanges(toProtoIssueChanges(c)));
    builder.setIsNew(defaultIssue.isNew());
    builder.setIsOnReferencedBranch(defaultIssue.isOnReferencedBranch());
    builder.setIsOnChangedLine(defaultIssue.isOnChangedLine());
    builder.setIsCopied(defaultIssue.isCopied());
    builder.setBeingClosed(defaultIssue.isBeingClosed());
    builder.setOnDisabledRule(defaultIssue.isOnDisabledRule());
    builder.setIsChanged(defaultIssue.isChanged());
    builder.setSendNotifications(defaultIssue.mustSendNotifications());
    ofNullable(defaultIssue.selectedAt()).ifPresent(builder::setSelectedAt);
    builder.setQuickFixAvailable(defaultIssue.isQuickFixAvailable());
    builder.setIsNoLongerNewCodeReferenceIssue(defaultIssue.isNoLongerNewCodeReferenceIssue());
    for (FieldDiffs fieldDiffs : defaultIssue.changes()) {
        builder.addChanges(toProtoIssueChanges(fieldDiffs));
    }
    return builder.build();
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs)

Example 37 with FieldDiffs

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

the class ProtobufIssueDiskCache method toProtoIssueChanges.

private static IssueCache.FieldDiffs toProtoIssueChanges(FieldDiffs fieldDiffs) {
    IssueCache.FieldDiffs.Builder builder = IssueCache.FieldDiffs.newBuilder().setCreationDate(fieldDiffs.creationDate().getTime());
    if (fieldDiffs.issueKey() != null) {
        builder.setIssueKey(fieldDiffs.issueKey());
    }
    String userUuid = fieldDiffs.userUuid();
    if (userUuid != null) {
        builder.setUserUuid(userUuid);
    }
    for (Map.Entry<String, FieldDiffs.Diff> e : fieldDiffs.diffs().entrySet()) {
        IssueCache.Diff.Builder diffBuilder = IssueCache.Diff.newBuilder();
        Serializable oldValue = e.getValue().oldValue();
        if (oldValue != null) {
            diffBuilder.setOldValue(oldValue.toString());
        }
        Serializable newValue = e.getValue().newValue();
        if (newValue != null) {
            diffBuilder.setNewValue(newValue.toString());
        }
        builder.putDiffs(e.getKey(), diffBuilder.build());
    }
    return builder.build();
}
Also used : Serializable(java.io.Serializable) FieldDiffs(org.sonar.core.issue.FieldDiffs) Map(java.util.Map)

Example 38 with FieldDiffs

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

the class PreviousStatusWas method matches.

@Override
public boolean matches(Issue issue) {
    DefaultIssue defaultIssue = (DefaultIssue) issue;
    Optional<String> lastPreviousStatus = defaultIssue.changes().stream().filter(change -> change != defaultIssue.currentChange()).filter(change -> change.creationDate() != null).sorted(Comparator.comparing(FieldDiffs::creationDate).reversed()).map(change -> change.get("status")).filter(Objects::nonNull).findFirst().map(t -> (String) t.oldValue());
    return lastPreviousStatus.filter(this.expectedPreviousStatus::equals).isPresent();
}
Also used : Objects(java.util.Objects) FieldDiffs(org.sonar.core.issue.FieldDiffs) Issue(org.sonar.api.issue.Issue) DefaultIssue(org.sonar.core.issue.DefaultIssue) Optional(java.util.Optional) Comparator(java.util.Comparator) FieldDiffs(org.sonar.core.issue.FieldDiffs) Objects(java.util.Objects) DefaultIssue(org.sonar.core.issue.DefaultIssue)

Example 39 with FieldDiffs

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

the class IssueLifecycleTest method mergeIssueFromPRIntoBranch.

@Test
public void mergeIssueFromPRIntoBranch() {
    DefaultIssue raw = new DefaultIssue().setKey("raw");
    DefaultIssue fromShort = new DefaultIssue().setKey("short").setIsNewCodeReferenceIssue(true);
    fromShort.setResolution("resolution");
    fromShort.setStatus("status");
    Date commentDate = new Date();
    fromShort.addComment(new DefaultIssueComment().setIssueKey("short").setCreatedAt(commentDate).setUserUuid("user_uuid").setMarkdownText("A comment"));
    Date diffDate = new Date();
    // file diff alone
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("file", "uuidA1", "uuidB1"));
    // file diff with another field
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("severity", "MINOR", "MAJOR").setDiff("file", "uuidA2", "uuidB2"));
    Branch branch = mock(Branch.class);
    when(branch.getName()).thenReturn("master");
    analysisMetadataHolder.setBranch(branch);
    underTest.mergeConfirmedOrResolvedFromPrOrBranch(raw, fromShort, BranchType.PULL_REQUEST, "2");
    assertThat(raw.resolution()).isEqualTo("resolution");
    assertThat(raw.status()).isEqualTo("status");
    assertThat(raw.defaultIssueComments()).extracting(DefaultIssueComment::issueKey, DefaultIssueComment::createdAt, DefaultIssueComment::userUuid, DefaultIssueComment::markdownText).containsOnly(tuple("raw", commentDate, "user_uuid", "A comment"));
    assertThat(raw.changes()).hasSize(2);
    assertThat(raw.changes().get(0).creationDate()).isEqualTo(diffDate);
    assertThat(raw.changes().get(0).userUuid()).isEqualTo("user_uuid");
    assertThat(raw.changes().get(0).issueKey()).isEqualTo("raw");
    assertThat(raw.changes().get(0).diffs()).containsOnlyKeys("severity");
    assertThat(raw.changes().get(1).userUuid()).isEqualTo("default_user_uuid");
    assertThat(raw.changes().get(1).diffs()).containsOnlyKeys(IssueFieldsSetter.FROM_BRANCH);
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).oldValue()).isEqualTo("#2");
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).newValue()).isEqualTo("master");
    assertThat(raw.isNewCodeReferenceIssue()).isTrue();
}
Also used : FieldDiffs(org.sonar.core.issue.FieldDiffs) Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) DateUtils.parseDate(org.sonar.api.utils.DateUtils.parseDate) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) Test(org.junit.Test)

Example 40 with FieldDiffs

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

the class IssueLifecycleTest method copyExistingIssuesFromSourceBranchOfPullRequest.

@Test
public void copyExistingIssuesFromSourceBranchOfPullRequest() {
    String pullRequestKey = "1";
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
    when(branch.getName()).thenReturn("sourceBranch-1");
    when(branch.getPullRequestKey()).thenReturn(pullRequestKey);
    analysisMetadataHolder.setBranch(branch);
    analysisMetadataHolder.setPullRequestKey(pullRequestKey);
    DefaultIssue raw = new DefaultIssue().setKey("raw");
    DefaultIssue fromShort = new DefaultIssue().setKey("short");
    fromShort.setResolution("resolution");
    fromShort.setStatus("status");
    Date commentDate = new Date();
    fromShort.addComment(new DefaultIssueComment().setIssueKey("short").setCreatedAt(commentDate).setUserUuid("user_uuid").setMarkdownText("A comment"));
    Date diffDate = new Date();
    // file diff alone
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("file", "uuidA1", "uuidB1"));
    // file diff with another field
    fromShort.addChange(new FieldDiffs().setCreationDate(diffDate).setIssueKey("short").setUserUuid("user_uuid").setDiff("severity", "MINOR", "MAJOR").setDiff("file", "uuidA2", "uuidB2"));
    underTest.copyExistingIssueFromSourceBranchToPullRequest(raw, fromShort);
    assertThat(raw.resolution()).isEqualTo("resolution");
    assertThat(raw.status()).isEqualTo("status");
    assertThat(raw.defaultIssueComments()).extracting(DefaultIssueComment::issueKey, DefaultIssueComment::createdAt, DefaultIssueComment::userUuid, DefaultIssueComment::markdownText).containsOnly(tuple("raw", commentDate, "user_uuid", "A comment"));
    assertThat(raw.changes()).hasSize(2);
    assertThat(raw.changes().get(0).creationDate()).isEqualTo(diffDate);
    assertThat(raw.changes().get(0).userUuid()).isEqualTo("user_uuid");
    assertThat(raw.changes().get(0).issueKey()).isEqualTo("raw");
    assertThat(raw.changes().get(0).diffs()).containsOnlyKeys("severity");
    assertThat(raw.changes().get(1).userUuid()).isEqualTo("default_user_uuid");
    assertThat(raw.changes().get(1).diffs()).containsOnlyKeys(IssueFieldsSetter.FROM_BRANCH);
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).oldValue()).isEqualTo("sourceBranch-1");
    assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_BRANCH).newValue()).isEqualTo("#1");
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) FieldDiffs(org.sonar.core.issue.FieldDiffs) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) DateUtils.parseDate(org.sonar.api.utils.DateUtils.parseDate) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) 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