import com.google.common.annotations.VisibleForTesting;
import java.util.Date;
+import java.util.Optional;
import org.sonar.api.issue.Issue;
+import org.sonar.api.issue.IssueComment;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.DefaultIssueComment;
import org.sonar.core.issue.FieldDiffs;
}
private static void copyChanges(DefaultIssue raw, DefaultIssue base) {
- base.comments().forEach(c -> raw.addComment(DefaultIssueComment.copy(raw.key(), c)));
- base.changes().forEach(c -> raw.addChange(FieldDiffs.copy(raw.key(), c)));
+ base.comments().forEach(c -> raw.addComment(copy(raw.key(), c)));
+ base.changes().forEach(c -> copy(raw.key(), c).ifPresent(raw::addChange));
+ }
+
+ /**
+ * Copy a comment from another issue
+ */
+ private static DefaultIssueComment copy(String issueKey, IssueComment c) {
+ DefaultIssueComment comment = new DefaultIssueComment();
+ comment.setIssueKey(issueKey);
+ comment.setKey(Uuids.create());
+ comment.setUserLogin(c.userLogin());
+ comment.setMarkdownText(c.markdownText());
+ comment.setCreatedAt(c.createdAt()).setUpdatedAt(c.updatedAt());
+ comment.setNew(true);
+ return comment;
+ }
+
+ /**
+ * Copy a diff from another issue
+ */
+ private static Optional<FieldDiffs> copy(String issueKey, FieldDiffs c) {
+ FieldDiffs result = new FieldDiffs();
+ result.setIssueKey(issueKey);
+ result.setUserLogin(c.userLogin());
+ result.setCreationDate(c.creationDate());
+ // Don't copy "file" changelogs as they refer to file uuids that might later be purged
+ c.diffs().entrySet().stream().filter(e -> !e.getKey().equals(IssueFieldsSetter.FILE))
+ .forEach(e -> result.setDiff(e.getKey(), e.getValue().oldValue(), e.getValue().newValue()));
+ if (result.diffs().isEmpty()) {
+ return Optional.empty();
+ }
+ return Optional.of(result);
}
public void mergeExistingOpenIssue(DefaultIssue raw, DefaultIssue base) {
import java.util.Date;
import org.junit.Rule;
import org.junit.Test;
+import org.sonar.api.issue.IssueComment;
import org.sonar.api.utils.Duration;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.DefaultIssueComment;
+import org.sonar.core.issue.FieldDiffs;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.db.protobuf.DbCommons;
import org.sonar.db.protobuf.DbIssues;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@Test
public void mergeIssueFromShortLivingBranch() {
- DefaultIssue raw = new DefaultIssue();
- DefaultIssue fromShort = new DefaultIssue();
+ DefaultIssue raw = new DefaultIssue()
+ .setKey("raw");
+ DefaultIssue fromShort = new DefaultIssue()
+ .setKey("short");
fromShort.setResolution("resolution");
fromShort.setStatus("status");
.setUserLogin("user")
.setMarkdownText("A comment"));
+ Date diffDate = new Date();
+ // file diff alone
+ fromShort.addChange(new FieldDiffs()
+ .setCreationDate(diffDate)
+ .setIssueKey("short")
+ .setUserLogin("user")
+ .setDiff("file", "uuidA1", "uuidB1"));
+ // file diff with another field
+ fromShort.addChange(new FieldDiffs()
+ .setCreationDate(diffDate)
+ .setIssueKey("short")
+ .setUserLogin("user")
+ .setDiff("severity", "MINOR", "MAJOR")
+ .setDiff("file", "uuidA2", "uuidB2"));
+
Branch branch = mock(Branch.class);
when(branch.getName()).thenReturn("master");
analysisMetadataHolder.setBranch(branch);
underTest.mergeConfirmedOrResolvedFromShortLivingBranch(raw, fromShort, "feature/foo");
+
assertThat(raw.resolution()).isEqualTo("resolution");
assertThat(raw.status()).isEqualTo("status");
- assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_SHORT_BRANCH).oldValue()).isEqualTo("feature/foo");
- assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_SHORT_BRANCH).newValue()).isEqualTo("master");
+ assertThat(raw.comments()).extracting(IssueComment::issueKey, IssueComment::createdAt, IssueComment::userLogin, IssueComment::markdownText)
+ .containsOnly(tuple("raw", commentDate, "user", "A comment"));
+ assertThat(raw.changes()).hasSize(2);
+ assertThat(raw.changes().get(0).creationDate()).isEqualTo(diffDate);
+ assertThat(raw.changes().get(0).userLogin()).isEqualTo("user");
+ assertThat(raw.changes().get(0).issueKey()).isEqualTo("raw");
+ assertThat(raw.changes().get(0).diffs()).containsOnlyKeys("severity");
+ assertThat(raw.changes().get(1).userLogin()).isEqualTo("julien");
+ assertThat(raw.changes().get(1).diffs()).containsOnlyKeys(IssueFieldsSetter.FROM_SHORT_BRANCH);
+ assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_SHORT_BRANCH).oldValue()).isEqualTo("feature/foo");
+ assertThat(raw.changes().get(1).get(IssueFieldsSetter.FROM_SHORT_BRANCH).newValue()).isEqualTo("master");
}
@Test