Store PatchLineComments in Git notes

Teach ChangeNotes how to read the PatchLineComments out of a Git Note
object in the format specified below. Also, teach ChangeUpdate how to
write those notes to the correct ref. Note that this does not deal
with draft PatchLineComments at all.

Below is an example of what one of the notes would look like (there can
be up to one note per commit to which comments refer). This includes
not only the patch sets themselves but also their bases.
--------------------------------------------
Patch-set: 3
Rev-Id: abcd1234abcd1234abcd1234abcd1234abcd1234
File: path/to/file

5:1-40:10
Mon Jun 15 2014 03:03 PM
Author: Yacob Yonas <1@gerrit>
Parent: 000060
UUID: 000070
Bytes: 15
I do
not get it

35:1-40:10
Mon Jun 15 2014 04:04 PM
Author: Dave Borowitz <2@gerrit>
Parent: 000080
UUID: 000090
Bytes: 15
I do
not get it

File: path/to/other

70:1-70:100
Mon Jun 8 2014 12:01 PM
Author: Dave Borowitz <2@gerrit>
UUID: 000020
Bytes: 3
Why

-------------------------------------------
The first line of every note will either read "Patch-set: X" or
"Base-of-patch-set: X" to tell us whether the comments in this commit
refer to the base of a patch set or a patch set, as well as which
patch set. Then, we have the Rev-Id for the commit on which these
commits were made.  There is a block for all of the comments on each
file. This will either be the commit for the patch set or for its base
(we will know which one because of the first line of the note).  These
blocks will start with a line of the format: "File: path/to/file".
There is one block for each of the comments. Within a file block, the
comments on that file are sorted by comment range and then by
timestamp for breaking ties. If a comment doesn't have a range, we use
the line number field of the comment instead for comparison.

The first line of a comment block either contains the comment's line
number or its comment range. A range would be in the format
"1:1-100:2" while a line number would simply be one number on that
line. The comment range is optional in a comment, so we allow
either. If that line contains just a line number, the range field on
the PatchLineComment into which we are parsing will be left empty. The
second line of the comment block holds the timestamp for the
comment. Next we have information about the account that made the
comment. Then, we have the parent UUID and current comment UUID. Even
though the field, UUID, no longer lives in the reviewdb, we must keep
that field because of specifications for the REST API. It is important
to note that the line for the parent UUID will be omitted completely
if a comment does not have a parent (see third comment block for an
example of that). Then, the final field is the length of the comment
itself (not including the metadata) in bytes. Finally, after the
comment's actual message, there are two new line characters.

Change-Id: I07db8a252de9901e64e5a6936fa4df0cb3e0d338
This commit is contained in:
Yacob Yonas
2014-06-17 12:07:13 -07:00
committed by Dave Borowitz
parent 6324b55648
commit 195b9d7ece
6 changed files with 1133 additions and 23 deletions

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.notedb;
import static com.google.gerrit.server.notedb.ReviewerState.CC;
import static com.google.gerrit.server.notedb.ReviewerState.REVIEWER;
import static com.google.inject.Scopes.SINGLETON;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.easymock.EasyMock.expect;
@@ -26,17 +27,24 @@ import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.CommentRange;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.PatchSetInfo;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
@@ -53,6 +61,7 @@ import com.google.gerrit.server.git.GitModule;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.VersionedMetaData.BatchMetaDataUpdate;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.notedb.CommentsInNotesUtil;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.util.TimeUtil;
@@ -70,7 +79,9 @@ import org.easymock.EasyMock;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.joda.time.DateTime;
@@ -81,6 +92,7 @@ import org.junit.Before;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
@@ -831,6 +843,330 @@ public class ChangeNotesTest {
assertEquals(ps1, cm.get(1).getPatchSetId());
}
@Test
public void patchLineCommentNotesFormatSide1() throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, otherUser);
String uuid = "uuid";
String message1 = "comment 1";
String message2 = "comment 2";
String message3 = "comment 3";
CommentRange range1 = new CommentRange(1, 1, 2, 1);
Timestamp time1 = TimeUtil.nowTs();
Timestamp time2 = TimeUtil.nowTs();
Timestamp time3 = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
PatchLineComment comment1 = newPatchLineComment(psId, "file1", uuid,
range1, range1.getEndLine(), otherUser, null, time1, message1,
(short) 1, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment1);
update.commit();
update = newUpdate(c, otherUser);
CommentRange range2 = new CommentRange(2, 1, 3, 1);
PatchLineComment comment2 = newPatchLineComment(psId, "file1", uuid,
range2, range2.getEndLine(), otherUser, null, time2, message2,
(short) 1, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment2);
update.commit();
update = newUpdate(c, otherUser);
CommentRange range3 = new CommentRange(3, 1, 4, 1);
PatchLineComment comment3 = newPatchLineComment(psId, "file2", uuid,
range3, range3.getEndLine(), otherUser, null, time3, message3,
(short) 1, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment3);
update.commit();
ChangeNotes notes = newNotes(c);
RevWalk walk = new RevWalk(repo);
ArrayList<Note> notesInTree =
Lists.newArrayList(notes.getNoteMap().iterator());
Note note = Iterables.getOnlyElement(notesInTree);
byte[] bytes =
walk.getObjectReader().open(
note.getData(), Constants.OBJ_BLOB).getBytes();
String noteString = new String(bytes, UTF_8);
assertEquals("Patch-set: 1\n"
+ "Revision: abcd1234abcd1234abcd1234abcd1234abcd1234\n"
+ "File: file1\n"
+ "\n"
+ "1:1-2:1\n"
+ CommentsInNotesUtil.formatTime(serverIdent, time1) + "\n"
+ "Author: Other Account <2@gerrit>\n"
+ "UUID: uuid\n"
+ "Bytes: 9\n"
+ "comment 1\n"
+ "\n"
+ "2:1-3:1\n"
+ CommentsInNotesUtil.formatTime(serverIdent, time2) + "\n"
+ "Author: Other Account <2@gerrit>\n"
+ "UUID: uuid\n"
+ "Bytes: 9\n"
+ "comment 2\n"
+ "\n"
+ "File: file2\n"
+ "\n"
+ "3:1-4:1\n"
+ CommentsInNotesUtil.formatTime(serverIdent, time3) + "\n"
+ "Author: Other Account <2@gerrit>\n"
+ "UUID: uuid\n"
+ "Bytes: 9\n"
+ "comment 3\n"
+ "\n",
noteString);
}
@Test
public void patchLineCommentNotesFormatSide0() throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, otherUser);
String uuid = "uuid";
String message1 = "comment 1";
String message2 = "comment 2";
CommentRange range1 = new CommentRange(1, 1, 2, 1);
Timestamp time1 = TimeUtil.nowTs();
Timestamp time2 = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
PatchLineComment comment1 = newPatchLineComment(psId, "file1", uuid,
range1, range1.getEndLine(), otherUser, null, time1, message1,
(short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment1);
update.commit();
update = newUpdate(c, otherUser);
CommentRange range2 = new CommentRange(2, 1, 3, 1);
PatchLineComment comment2 = newPatchLineComment(psId, "file1", uuid,
range2, range2.getEndLine(), otherUser, null, time2, message2,
(short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment2);
update.commit();
ChangeNotes notes = newNotes(c);
RevWalk walk = new RevWalk(repo);
ArrayList<Note> notesInTree =
Lists.newArrayList(notes.getNoteMap().iterator());
Note note = Iterables.getOnlyElement(notesInTree);
byte[] bytes =
walk.getObjectReader().open(
note.getData(), Constants.OBJ_BLOB).getBytes();
String noteString = new String(bytes, UTF_8);
assertEquals("Base-for-patch-set: 1\n"
+ "Revision: abcd1234abcd1234abcd1234abcd1234abcd1234\n"
+ "File: file1\n"
+ "\n"
+ "1:1-2:1\n"
+ CommentsInNotesUtil.formatTime(serverIdent, time1) + "\n"
+ "Author: Other Account <2@gerrit>\n"
+ "UUID: uuid\n"
+ "Bytes: 9\n"
+ "comment 1\n"
+ "\n"
+ "2:1-3:1\n"
+ CommentsInNotesUtil.formatTime(serverIdent, time2) + "\n"
+ "Author: Other Account <2@gerrit>\n"
+ "UUID: uuid\n"
+ "Bytes: 9\n"
+ "comment 2\n"
+ "\n",
noteString);
}
@Test
public void patchLineCommentMultipleOnePatchsetOneFileBothSides()
throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, otherUser);
String uuid = "uuid";
String messageForBase = "comment for base";
String messageForPS = "comment for ps";
CommentRange range = new CommentRange(1, 1, 2, 1);
Timestamp now = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
PatchLineComment commentForBase =
newPatchLineComment(psId, "filename", uuid,
range, range.getEndLine(), otherUser, null, now, messageForBase,
(short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(commentForBase);
update.commit();
update = newUpdate(c, otherUser);
PatchLineComment commentForPS =
newPatchLineComment(psId, "filename", uuid,
range, range.getEndLine(), otherUser, null, now, messageForPS,
(short) 1, "abcd4567abcd4567abcd4567abcd4567abcd4567");
update.setPatchSetId(psId);
update.putComment(commentForPS);
update.commit();
ChangeNotes notes = newNotes(c);
Multimap<PatchSet.Id, PatchLineComment> commentsForBase =
notes.getBaseComments();
Multimap<PatchSet.Id, PatchLineComment> commentsForPS =
notes.getPatchSetComments();
assertEquals(commentsForBase.size(), 1);
assertEquals(commentsForPS.size(), 1);
assertEquals(commentForBase,
Iterables.getOnlyElement(commentsForBase.get(psId)));
assertEquals(commentForPS,
Iterables.getOnlyElement(commentsForPS.get(psId)));
}
@Test
public void patchLineCommentMultipleOnePatchsetOneFile() throws Exception {
Change c = newChange();
String uuid = "uuid";
CommentRange range = new CommentRange(1, 1, 2, 1);
PatchSet.Id psId = c.currentPatchSetId();
String filename = "filename";
short side = (short) 1;
ChangeUpdate update = newUpdate(c, otherUser);
Timestamp timeForComment1 = TimeUtil.nowTs();
Timestamp timeForComment2 = TimeUtil.nowTs();
PatchLineComment comment1 = newPatchLineComment(psId, filename, uuid, range,
range.getEndLine(), otherUser, null, timeForComment1, "comment 1", side,
"abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment1);
update.commit();
update = newUpdate(c, otherUser);
PatchLineComment comment2 = newPatchLineComment(psId, filename, uuid, range,
range.getEndLine(), otherUser, null, timeForComment2, "comment 2", side,
"abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment2);
update.commit();
ChangeNotes notes = newNotes(c);
Multimap<PatchSet.Id, PatchLineComment> commentsForBase =
notes.getBaseComments();
Multimap<PatchSet.Id, PatchLineComment> commentsForPS =
notes.getPatchSetComments();
assertEquals(commentsForBase.size(), 0);
assertEquals(commentsForPS.size(), 2);
ImmutableList<PatchLineComment> commentsForThisPS =
(ImmutableList<PatchLineComment>) commentsForPS.get(psId);
assertEquals(commentsForThisPS.size(), 2);
PatchLineComment commentFromNotes1 = commentsForThisPS.get(0);
PatchLineComment commentFromNotes2 = commentsForThisPS.get(1);
assertEquals(comment1, commentFromNotes1);
assertEquals(comment2, commentFromNotes2);
}
@Test
public void patchLineCommentMultipleOnePatchsetMultipleFiles()
throws Exception {
Change c = newChange();
String uuid = "uuid";
CommentRange range = new CommentRange(1, 1, 2, 1);
PatchSet.Id psId = c.currentPatchSetId();
String filename1 = "filename1";
String filename2 = "filename2";
short side = (short) 1;
ChangeUpdate update = newUpdate(c, otherUser);
Timestamp now = TimeUtil.nowTs();
PatchLineComment comment1 = newPatchLineComment(psId, filename1, uuid,
range, range.getEndLine(), otherUser, null, now, "comment 1", side,
"abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment1);
update.commit();
update = newUpdate(c, otherUser);
PatchLineComment comment2 = newPatchLineComment(psId, filename2, uuid,
range, range.getEndLine(), otherUser, null, now, "comment 2", side,
"abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(psId);
update.putComment(comment2);
update.commit();
ChangeNotes notes = newNotes(c);
Multimap<PatchSet.Id, PatchLineComment> commentsForBase =
notes.getBaseComments();
Multimap<PatchSet.Id, PatchLineComment> commentsForPS =
notes.getPatchSetComments();
assertEquals(commentsForBase.size(), 0);
assertEquals(commentsForPS.size(), 2);
ImmutableList<PatchLineComment> commentsForThisPS =
(ImmutableList<PatchLineComment>) commentsForPS.get(psId);
assertEquals(commentsForThisPS.size(), 2);
PatchLineComment commentFromNotes1 = commentsForThisPS.get(0);
PatchLineComment commentFromNotes2 = commentsForThisPS.get(1);
assertEquals(comment1, commentFromNotes1);
assertEquals(comment2, commentFromNotes2);
}
@Test
public void patchLineCommentMultiplePatchsets() throws Exception {
Change c = newChange();
String uuid = "uuid";
CommentRange range = new CommentRange(1, 1, 2, 1);
PatchSet.Id ps1 = c.currentPatchSetId();
String filename = "filename1";
short side = (short) 1;
ChangeUpdate update = newUpdate(c, otherUser);
Timestamp now = TimeUtil.nowTs();
PatchLineComment comment1 = newPatchLineComment(ps1, filename, uuid,
range, range.getEndLine(), otherUser, null, now, "comment on ps1", side,
"abcd1234abcd1234abcd1234abcd1234abcd1234");
update.setPatchSetId(ps1);
update.putComment(comment1);
update.commit();
incrementPatchSet(c);
PatchSet.Id ps2 = c.currentPatchSetId();
update = newUpdate(c, otherUser);
now = TimeUtil.nowTs();
PatchLineComment comment2 = newPatchLineComment(ps2, filename, uuid,
range, range.getEndLine(), otherUser, null, now, "comment on ps2", side,
"abcd4567abcd4567abcd4567abcd4567abcd4567");
update.setPatchSetId(ps2);
update.putComment(comment2);
update.commit();
ChangeNotes notes = newNotes(c);
LinkedListMultimap<PatchSet.Id, PatchLineComment> commentsForBase =
LinkedListMultimap.create(notes.getBaseComments());
LinkedListMultimap<PatchSet.Id, PatchLineComment> commentsForPS =
LinkedListMultimap.create(notes.getPatchSetComments());
assertEquals(commentsForBase.keys().size(), 0);
assertEquals(commentsForPS.values().size(), 2);
List<PatchLineComment> commentsForPS1 = commentsForPS.get(ps1);
assertEquals(commentsForPS1.size(), 1);
PatchLineComment commentFromPs1 = commentsForPS1.get(0);
List<PatchLineComment> commentsForPS2 = commentsForPS.get(ps2);
assertEquals(commentsForPS2.size(), 1);
PatchLineComment commentFromPs2 = commentsForPS2.get(0);
assertEquals(comment1, commentFromPs1);
assertEquals(comment2, commentFromPs2);
}
private Change newChange() {
Change.Id changeId = new Change.Id(1);
@@ -844,6 +1180,21 @@ public class ChangeNotesTest {
return c;
}
private PatchLineComment newPatchLineComment(PatchSet.Id psId,
String filename, String UUID, CommentRange range, int line,
IdentifiedUser commenter, String parentUUID, Timestamp t,
String message, short side, String commitSHA1) {
PatchLineComment comment = new PatchLineComment(
new PatchLineComment.Key(
new Patch.Key(psId, filename), UUID),
line, commenter.getAccountId(), parentUUID, t);
comment.setSide(side);
comment.setMessage(message);
comment.setRange(range);
comment.setRevId(new RevId(commitSHA1));
return comment;
}
private ChangeUpdate newUpdate(Change c, final IdentifiedUser user)
throws Exception {
return injector.createChildInjector(new FactoryModule() {
@@ -877,10 +1228,13 @@ public class ChangeNotesTest {
return new Timestamp(c.getCreatedOn().getTime() + millis);
}
private ChangeControl stubChangeControl(Change c, IdentifiedUser user) {
private ChangeControl stubChangeControl(Change c, IdentifiedUser user) throws OrmException {
ChangeControl ctl = EasyMock.createNiceMock(ChangeControl.class);
expect(ctl.getChange()).andStubReturn(c);
expect(ctl.getCurrentUser()).andStubReturn(user);
ChangeNotes notes = new ChangeNotes(repoManager, c);
notes = notes.load();
expect(ctl.getNotes()).andStubReturn(notes);
EasyMock.replay(ctl);
return ctl;
}