PostReview: update and insert comments/approvals in a single step

Change-Id: Ieab4ac0628e397495bfd8a278c879f79817ba443
This commit is contained in:
Dave Borowitz
2014-02-05 12:15:41 -08:00
parent fc9d9628cd
commit d0624718cb

View File

@@ -309,16 +309,14 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
} }
List<PatchLineComment> del = Lists.newArrayList(); List<PatchLineComment> del = Lists.newArrayList();
List<PatchLineComment> ins = Lists.newArrayList(); List<PatchLineComment> ups = Lists.newArrayList();
List<PatchLineComment> upd = Lists.newArrayList();
for (Map.Entry<String, List<Comment>> ent : in.entrySet()) { for (Map.Entry<String, List<Comment>> ent : in.entrySet()) {
String path = ent.getKey(); String path = ent.getKey();
for (Comment c : ent.getValue()) { for (Comment c : ent.getValue()) {
String parent = Url.decode(c.inReplyTo); String parent = Url.decode(c.inReplyTo);
PatchLineComment e = drafts.remove(Url.decode(c.id)); PatchLineComment e = drafts.remove(Url.decode(c.id));
boolean create = e == null; if (e == null) {
if (create) {
e = new PatchLineComment( e = new PatchLineComment(
new PatchLineComment.Key( new PatchLineComment.Key(
new Patch.Key(rsrc.getPatchSet().getId(), path), new Patch.Key(rsrc.getPatchSet().getId(), path),
@@ -340,7 +338,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
c.range.endLine, c.range.endLine,
c.range.endCharacter)); c.range.endCharacter));
} }
(create ? ins : upd).add(e); ups.add(e);
} }
} }
@@ -355,16 +353,14 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
for (PatchLineComment e : drafts.values()) { for (PatchLineComment e : drafts.values()) {
e.setStatus(PatchLineComment.Status.PUBLISHED); e.setStatus(PatchLineComment.Status.PUBLISHED);
e.setWrittenOn(timestamp); e.setWrittenOn(timestamp);
upd.add(e); ups.add(e);
} }
break; break;
} }
db.get().patchComments().delete(del); db.get().patchComments().delete(del);
db.get().patchComments().insert(ins); db.get().patchComments().upsert(ups);
db.get().patchComments().update(upd); comments.addAll(ups);
comments.addAll(ins); return !del.isEmpty() || !ups.isEmpty();
comments.addAll(upd);
return !del.isEmpty() || !ins.isEmpty() || !upd.isEmpty();
} }
private Map<String, PatchLineComment> scanDraftComments( private Map<String, PatchLineComment> scanDraftComments(
@@ -385,8 +381,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
} }
List<PatchSetApproval> del = Lists.newArrayList(); List<PatchSetApproval> del = Lists.newArrayList();
List<PatchSetApproval> ins = Lists.newArrayList(); List<PatchSetApproval> ups = Lists.newArrayList();
List<PatchSetApproval> upd = Lists.newArrayList();
Map<String, PatchSetApproval> current = scanLabels(rsrc, del); Map<String, PatchSetApproval> current = scanLabels(rsrc, del);
LabelTypes labelTypes = rsrc.getControl().getLabelTypes(); LabelTypes labelTypes = rsrc.getControl().getLabelTypes();
@@ -412,7 +407,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
} else if (c != null && c.getValue() != ent.getValue()) { } else if (c != null && c.getValue() != ent.getValue()) {
c.setValue(ent.getValue()); c.setValue(ent.getValue());
c.setGranted(timestamp); c.setGranted(timestamp);
upd.add(c); ups.add(c);
addLabelDelta(normName, c.getValue()); addLabelDelta(normName, c.getValue());
categories.put(normName, c.getValue()); categories.put(normName, c.getValue());
update.putApproval(ent.getKey(), ent.getValue()); update.putApproval(ent.getKey(), ent.getValue());
@@ -425,24 +420,23 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
lt.getLabelId()), lt.getLabelId()),
ent.getValue(), TimeUtil.nowTs()); ent.getValue(), TimeUtil.nowTs());
c.setGranted(timestamp); c.setGranted(timestamp);
ins.add(c); ups.add(c);
addLabelDelta(normName, c.getValue()); addLabelDelta(normName, c.getValue());
categories.put(normName, c.getValue()); categories.put(normName, c.getValue());
update.putApproval(ent.getKey(), ent.getValue()); update.putApproval(ent.getKey(), ent.getValue());
} }
} }
forceCallerAsReviewer(rsrc, current, ins, upd, del); forceCallerAsReviewer(rsrc, current, ups, del);
db.get().patchSetApprovals().delete(del); db.get().patchSetApprovals().delete(del);
db.get().patchSetApprovals().insert(ins); db.get().patchSetApprovals().upsert(ups);
db.get().patchSetApprovals().update(upd); return !del.isEmpty() || !ups.isEmpty();
return !del.isEmpty() || !ins.isEmpty() || !upd.isEmpty();
} }
private void forceCallerAsReviewer(RevisionResource rsrc, private void forceCallerAsReviewer(RevisionResource rsrc,
Map<String, PatchSetApproval> current, List<PatchSetApproval> ins, Map<String, PatchSetApproval> current, List<PatchSetApproval> ups,
List<PatchSetApproval> upd, List<PatchSetApproval> del) { List<PatchSetApproval> del) {
if (current.isEmpty() && ins.isEmpty() && upd.isEmpty()) { if (current.isEmpty() && ups.isEmpty()) {
// TODO Find another way to link reviewers to changes. // TODO Find another way to link reviewers to changes.
if (del.isEmpty()) { if (del.isEmpty()) {
// If no existing label is being set to 0, hack in the caller // If no existing label is being set to 0, hack in the caller
@@ -454,7 +448,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
.getLabelId()), .getLabelId()),
(short) 0, TimeUtil.nowTs()); (short) 0, TimeUtil.nowTs());
c.setGranted(timestamp); c.setGranted(timestamp);
ins.add(c); ups.add(c);
} else { } else {
// Pick a random label that is about to be deleted and keep it. // Pick a random label that is about to be deleted and keep it.
Iterator<PatchSetApproval> i = del.iterator(); Iterator<PatchSetApproval> i = del.iterator();
@@ -462,7 +456,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
c.setValue((short) 0); c.setValue((short) 0);
c.setGranted(timestamp); c.setGranted(timestamp);
i.remove(); i.remove();
upd.add(c); ups.add(c);
} }
} }
} }