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:
@@ -40,6 +40,7 @@ import com.google.gerrit.server.change.Rebuild;
|
||||
import com.google.gerrit.server.config.AllUsersName;
|
||||
import com.google.gerrit.server.notedb.ChangeBundle;
|
||||
import com.google.gerrit.server.notedb.ChangeNoteUtil;
|
||||
import com.google.gerrit.server.notedb.ChangeNotes;
|
||||
import com.google.gerrit.server.notedb.NoteDbChangeState;
|
||||
import com.google.gerrit.server.schema.DisabledChangesReviewDbWrapper;
|
||||
import com.google.gerrit.testutil.NoteDbChecker;
|
||||
@@ -59,6 +60,7 @@ import org.junit.Test;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ChangeRebuilderIT extends AbstractDaemonTest {
|
||||
@@ -165,12 +167,48 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
|
||||
|
||||
// Events need to be otherwise identical for the account ID to be compared.
|
||||
ChangeMessage msg1 =
|
||||
insertMessage(psId, user.getId(), TimeUtil.nowTs(), "message 1");
|
||||
insertMessage(psId, null, msg1.getWrittenOn(), "message 2");
|
||||
insertMessage(id, psId, user.getId(), TimeUtil.nowTs(), "message 1");
|
||||
insertMessage(id, psId, null, msg1.getWrittenOn(), "message 2");
|
||||
|
||||
checker.rebuildAndCheckChanges(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullPatchSetId() throws Exception {
|
||||
PushOneCommit.Result r = createChange();
|
||||
PatchSet.Id psId1 = r.getPatchSetId();
|
||||
Change.Id id = psId1.getParentKey();
|
||||
|
||||
// Events need to be otherwise identical for the PatchSet.ID to be compared.
|
||||
ChangeMessage msg1 =
|
||||
insertMessage(id, null, user.getId(), TimeUtil.nowTs(), "message 1");
|
||||
insertMessage(id, null, user.getId(), msg1.getWrittenOn(), "message 2");
|
||||
|
||||
PatchSet.Id psId2 = amendChange(r.getChangeId()).getPatchSetId();
|
||||
|
||||
ChangeMessage msg3 =
|
||||
insertMessage(id, null, user.getId(), TimeUtil.nowTs(), "message 3");
|
||||
insertMessage(id, null, user.getId(), msg3.getWrittenOn(), "message 4");
|
||||
|
||||
checker.rebuildAndCheckChanges(id);
|
||||
|
||||
notesMigration.setWriteChanges(true);
|
||||
notesMigration.setReadChanges(true);
|
||||
|
||||
ChangeNotes notes = notesFactory.create(db, project, id);
|
||||
Map<String, PatchSet.Id> psIds = new HashMap<>();
|
||||
for (ChangeMessage msg : notes.getChangeMessages()) {
|
||||
PatchSet.Id psId = msg.getPatchSetId();
|
||||
assertThat(psId).named("patchset for " + msg).isNotNull();
|
||||
psIds.put(msg.getMessage(), psId);
|
||||
}
|
||||
// Patch set IDs were replaced during conversion process.
|
||||
assertThat(psIds).containsEntry("message 1", psId1);
|
||||
assertThat(psIds).containsEntry("message 2", psId1);
|
||||
assertThat(psIds).containsEntry("message 3", psId2);
|
||||
assertThat(psIds).containsEntry("message 4", psId2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noWriteToNewRef() throws Exception {
|
||||
PushOneCommit.Result r = createChange();
|
||||
@@ -404,9 +442,8 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
|
||||
}
|
||||
}
|
||||
|
||||
private ChangeMessage insertMessage(PatchSet.Id psId, Account.Id author,
|
||||
Timestamp ts, String message) throws Exception {
|
||||
Change.Id id = psId.getParentKey();
|
||||
private ChangeMessage insertMessage(Change.Id id, PatchSet.Id psId,
|
||||
Account.Id author, Timestamp ts, String message) throws Exception {
|
||||
ChangeMessage msg = new ChangeMessage(
|
||||
new ChangeMessage.Key(id, ChangeUtil.messageUUID(db)),
|
||||
author, ts, psId);
|
||||
|
||||
Reference in New Issue
Block a user