@Test public void setIssueMoved_changes_componentUuid_adds_a_change() { String oldComponentUuid = "a"; String newComponentUuid = "b"; issue.setComponentUuid(oldComponentUuid); underTest.setIssueMoved(issue, newComponentUuid, context); assertThat(issue.changes()).hasSize(1); FieldDiffs fieldDiffs = issue.changes().get(0); assertThat(fieldDiffs.creationDate()).isEqualTo(context.date()); assertThat(fieldDiffs.diffs()).hasSize(1); Map.Entry<String, FieldDiffs.Diff> entry = fieldDiffs.diffs().entrySet().iterator().next(); assertThat(entry.getKey()).isEqualTo("file"); assertThat(entry.getValue().oldValue()).isEqualTo(oldComponentUuid); assertThat(entry.getValue().newValue()).isEqualTo(newComponentUuid); assertThat(issue.componentUuid()).isEqualTo(newComponentUuid); assertThat(issue.isChanged()).isTrue(); assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(context.date(), Calendar.SECOND)); } }
@SuppressWarnings("unchecked") public FieldDiffs setDiff(String field, @Nullable Serializable oldValue, @Nullable Serializable newValue) { Diff diff = diffs.get(field); if (field.equals(ASSIGNEE)) { oldValue = encodeField(oldValue); newValue = encodeField(newValue); } if (diff == null) { diff = new Diff(oldValue, newValue); diffs.put(field, diff); } else { diff.setNewValue(newValue); } return this; }
@CheckForNull public Long newValueLong() { return toLong(newValue); }
private String serialize(boolean shouldEncode) { StringBuilder sb = new StringBuilder(); boolean notFirst = false; for (Map.Entry<String, Diff> entry : diffs.entrySet()) { if (notFirst) { sb.append(','); } else { notFirst = true; } sb.append(entry.getKey()); sb.append('='); if (shouldEncode) { sb.append(entry.getValue().toEncodedString()); } else { sb.append(entry.getValue().toString()); } } return sb.toString(); }
private static Function<Map.Entry<String, FieldDiffs.Diff>, Changelog.Diff> toWsDiff(ChangeLogResults results) { return diff -> { FieldDiffs.Diff value = diff.getValue(); Changelog.Diff.Builder diffBuilder = Changelog.Diff.newBuilder(); String key = diff.getKey(); if (key.equals(FILE)) { diffBuilder.setKey(key); setNullable(results.getFileLongName(emptyToNull(value.newValue().toString())), diffBuilder::setNewValue); setNullable(results.getFileLongName(emptyToNull(value.oldValue().toString())), diffBuilder::setOldValue); } else { diffBuilder.setKey(key.equals(TECHNICAL_DEBT) ? EFFORT_CHANGELOG_KEY : key); setNullable(emptyToNull(value.newValue().toString()), diffBuilder::setNewValue); setNullable(emptyToNull(value.oldValue().toString()), diffBuilder::setOldValue); } return diffBuilder.build(); }; }
/** * Copy a diff from another issue */ private static Optional<FieldDiffs> copy(String issueKey, FieldDiffs c) { FieldDiffs result = new FieldDiffs(); result.setIssueKey(issueKey); result.setUserUuid(c.userUuid()); 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); }
@SuppressWarnings("unchecked") public FieldDiffs setDiff(String field, @Nullable Serializable oldValue, @Nullable Serializable newValue) { Diff diff = diffs.get(field); if (field.equals(ASSIGNEE)) { oldValue = encodeField(oldValue); newValue = encodeField(newValue); } if (diff == null) { diff = new Diff(oldValue, newValue); diffs.put(field, diff); } else { diff.setNewValue(newValue); } return this; }
private String toEncodedString() { return serialize(true); }
public IssueChangeNotification setIssue(DefaultIssue issue) { setFieldValue("key", issue.key()); setFieldValue("message", issue.message()); 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; }
@CheckForNull public Long newValueLong() { return toLong(newValue); }
@CheckForNull public Long oldValueLong() { return toLong(oldValue); }
@Override public String toString() { return serialize(false); }
@SuppressWarnings("unchecked") Diff decode(Diff encoded) { return new Diff( decodeField(encoded.oldValue), decodeField(encoded.newValue) ); }
assertThat(raw.isOnDisabledRule()).isTrue(); assertThat(raw.selectedAt()).isEqualTo(1000L); assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_LONG_BRANCH).oldValue()).isEqualTo("master"); assertThat(raw.changes().get(0).get(IssueFieldsSetter.FROM_LONG_BRANCH).newValue()).isEqualTo("release-2.x");
assertThat(raw.changes()).hasSize(2); assertThat(raw.changes().get(0).diffs()) .containsOnly(entry("foo", new FieldDiffs.Diff("bar", "donut"))); assertThat(raw.changes().get(1).diffs()) .containsOnly(entry("file", new FieldDiffs.Diff("A", "B")));
assertThat(raw.changes().get(1).userUuid()).isEqualTo("default_user_uuid"); 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");
@Override public void handleResult(ResultContext<? extends IssueDto> resultContext) { IssueDto resultObject = resultContext.getResultObject(); // issue are ordered by most recent change first, only the first row for a given issue is of interest if (previousIssueKey != null && previousIssueKey.equals(resultObject.getKey())) { return; } FieldDiffs fieldDiffs = FieldDiffs.parse(resultObject.getClosedChangeData() .orElseThrow(() -> new IllegalStateException("Close change data should be populated"))); checkState(Optional.ofNullable(fieldDiffs.get("status")) .map(FieldDiffs.Diff::newValue) .filter(STATUS_CLOSED::equals) .isPresent(), "Close change data should have a status diff with new value %s", STATUS_CLOSED); Integer line = Optional.ofNullable(fieldDiffs.get("line")) .map(diff -> (String) diff.oldValue()) .filter(str -> !str.isEmpty()) .map(Integer::parseInt) .orElse(null); previousIssueKey = resultObject.getKey(); DefaultIssue issue = resultObject.toDefaultIssue(); issue.setLine(line); // FIXME issue.setSelectedAt(System.currentTimeMillis()); issues.add(issue); } }
private String serialize(boolean shouldEncode) { StringBuilder sb = new StringBuilder(); boolean notFirst = false; for (Map.Entry<String, Diff> entry : diffs.entrySet()) { if (notFirst) { sb.append(','); } else { notFirst = true; } sb.append(entry.getKey()); sb.append('='); if (shouldEncode) { sb.append(entry.getValue().toEncodedString()); } else { sb.append(entry.getValue().toString()); } } return sb.toString(); }
private static Function<Map.Entry<String, FieldDiffs.Diff>, Changelog.Diff> toWsDiff(ChangeLogResults results) { return diff -> { FieldDiffs.Diff value = diff.getValue(); Changelog.Diff.Builder diffBuilder = Changelog.Diff.newBuilder(); String key = diff.getKey(); String oldValue = value.oldValue() != null ? value.oldValue().toString() : null; String newValue = value.newValue() != null ? value.newValue().toString() : null; if (key.equals(FILE)) { diffBuilder.setKey(key); ofNullable(results.getFileLongName(emptyToNull(newValue))).ifPresent(diffBuilder::setNewValue); ofNullable(results.getFileLongName(emptyToNull(oldValue))).ifPresent(diffBuilder::setOldValue); } else { diffBuilder.setKey(key.equals(TECHNICAL_DEBT) ? EFFORT_CHANGELOG_KEY : key); ofNullable(emptyToNull(newValue)).ifPresent(diffBuilder::setNewValue); ofNullable(emptyToNull(oldValue)).ifPresent(diffBuilder::setOldValue); } return diffBuilder.build(); }; }