Modify CommentsInNotesUtil to handle removing a notes object
Previously, we were using the inserter to insert null if the list of comments to write to the NoteMap was empty. However, in this case, we should be removing the note that existed before. Therefore, I made an additional method for the purpose of removing the note if we deleted all remaining comments from that note, removeNoteFromNoteMap(). Callers should call writeCommentsToNoteMap if they have a non-empty list of comments that they want to write to a note. If they have an empty list, they should call removeNoteFromNoteMap which requires a parameter for the RevId for the note where the comments used to be. Change-Id: Ia91d8698206ab4814039a47190319838ecb541cc
This commit is contained in:
@@ -425,14 +425,12 @@ public class CommentsInNotesUtil {
|
||||
* byte array.
|
||||
* All of the comments in this list must have the same side and
|
||||
* must share the same PatchSet.Id.
|
||||
* This list must not be empty because we cannot build a note
|
||||
* for no comments.
|
||||
* @return the note. Null if there are no comments in the list.
|
||||
*/
|
||||
public byte[] buildNote(List<PatchLineComment> comments)
|
||||
throws OrmException, IOException {
|
||||
if (comments.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
OutputStreamWriter streamWriter = new OutputStreamWriter(buf, UTF_8);
|
||||
PrintWriter writer = new PrintWriter(streamWriter);
|
||||
@@ -517,6 +515,8 @@ public class CommentsInNotesUtil {
|
||||
public void writeCommentsToNoteMap(NoteMap noteMap,
|
||||
List<PatchLineComment> allComments, ObjectInserter inserter)
|
||||
throws OrmException, IOException {
|
||||
checkArgument(!allComments.isEmpty(),
|
||||
"No comments to write; to delete, use removeNoteFromNoteMap().");
|
||||
ObjectId commitOID =
|
||||
ObjectId.fromString(allComments.get(0).getRevId().get());
|
||||
Collections.sort(allComments, ChangeNotes.PatchLineCommentComparator);
|
||||
@@ -524,4 +524,9 @@ public class CommentsInNotesUtil {
|
||||
ObjectId noteId = inserter.insert(Constants.OBJ_BLOB, note, 0, note.length);
|
||||
noteMap.set(commitOID, noteId);
|
||||
}
|
||||
|
||||
public void removeNote(NoteMap noteMap, RevId commitId)
|
||||
throws IOException {
|
||||
noteMap.remove(ObjectId.fromString(commitId.get()));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user