ChangeRebuilderImpl: Handle ChangeMessages with null PatchSet.Id

Long ago we made the design decision in NoteDb to associate every
commit in the meta graph with a patch set ID. The actual reason for
this was somewhat obscure: we need to have at least one footer in
every commit so we can tell where the ChangeMessage text (the body of
the commit) ends and the footers begin.

A consequence of this decision is that we cannot represent
ChangeMessages that have null PatchSet.Id fields in NoteDb. In fact,
trying to convert those fields was resulting in NPE, similar to
I3723b0c9, but the fix is a little more complicated.

Note that we have pretty much eliminated ChangeMessages with null
patch set IDs from the code, since we already enforce that every
ChangeUpdate is associated with a PatchSet.Id. (The one remaining
exception is in MergeOp#abandonAllOpenChangesForDeletedProject, which
is moot since in a NoteDb world such changes just get deleted.) So
this change is just about converting old messages that may for
whatever reason be lacking the field.

In ChangeRebuilderImpl, after sorting events but before combining them
into ChangeUpdates, ensure that all psId fields are populated, using
the latest patch set of the change encountered so far. This is
analogous to the behavior in the UI where if you navigate to a change
screen, it defaults to the latest patch set; then if you add a
ChangeMessage, it likewise is associated with the latest patch set.

ChangeBundle is also modified to skip comparing the patch set ID field
when the one in ReviewDb is null.

Change-Id: Id2722541f128e77342f2185910ceea33552d1524
This commit is contained in:
Dave Borowitz
2016-04-15 12:44:50 -04:00
parent 8a867d6414
commit b7db0a0f18
4 changed files with 132 additions and 15 deletions

View File

@@ -355,6 +355,45 @@ public class ChangeBundleTest {
assertDiffs(b3, b1, msg);
}
@Test
public void diffChangeMessagesAllowsNullPatchSetIdFromReviewDb()
throws Exception {
Change c = TestChanges.newChange(project, accountId);
int id = c.getId().get();
ChangeMessage cm1 = new ChangeMessage(
new ChangeMessage.Key(c.getId(), "uuid"),
accountId, TimeUtil.nowTs(), c.currentPatchSetId());
cm1.setMessage("message 1");
ChangeMessage cm2 = clone(cm1);
cm2.setPatchSetId(null);
ChangeBundle b1 = new ChangeBundle(c, messages(cm1), patchSets(),
approvals(), comments(), REVIEW_DB);
ChangeBundle b2 = new ChangeBundle(c, messages(cm2), patchSets(),
approvals(), comments(), REVIEW_DB);
// Both are ReviewDb, exact patch set ID match is required.
assertDiffs(b1, b2,
"patchset differs for ChangeMessage.Key " + c.getId() + ",uuid:"
+ " {" + id + ",1} != {null}");
// Null patch set ID on ReviewDb is ignored.
b1 = new ChangeBundle(c, messages(cm1), patchSets(), approvals(),
comments(), NOTE_DB);
b2 = new ChangeBundle(c, messages(cm2), patchSets(), approvals(),
comments(), REVIEW_DB);
assertNoDiffs(b1, b2);
// Null patch set ID on NoteDb is not ignored (but is not realistic).
b1 = new ChangeBundle(c, messages(cm1), patchSets(), approvals(),
comments(), REVIEW_DB);
b2 = new ChangeBundle(c, messages(cm2), patchSets(), approvals(),
comments(), NOTE_DB);
assertDiffs(b1, b2,
"patchset differs for ChangeMessage on " + id + " at index 0:"
+ " {" + id + ",1} != {null}");
}
@Test
public void diffPatchSetIdSets() throws Exception {
Change c = TestChanges.newChange(project, accountId);