Split off publish method from CommentsUtil.

This is so CommentJSonMigratorTest doesn't need lots of GUICE bindings
for PatchListCache and PatchSetUtil.

Change-Id: I5039895f90e76a24e082081db4381b5e8a6ee135
This commit is contained in:
Han-Wen Nienhuys
2018-06-06 17:10:21 +02:00
parent 2361278bf8
commit 5d822b01c1
4 changed files with 93 additions and 45 deletions

View File

@@ -18,7 +18,6 @@ import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.reviewdb.client.PatchLineComment.Status.PUBLISHED;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.FluentIterable;
@@ -62,7 +61,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.NullProgressMonitor;
@@ -130,8 +128,6 @@ public class CommentsUtil {
private final GitRepositoryManager repoManager;
private final AllUsersName allUsers;
private final NotesMigration migration;
private final PatchListCache patchListCache;
private final PatchSetUtil psUtil;
private final String serverId;
@Inject
@@ -139,14 +135,10 @@ public class CommentsUtil {
GitRepositoryManager repoManager,
AllUsersName allUsers,
NotesMigration migration,
PatchListCache patchListCache,
PatchSetUtil psUtil,
@GerritServerId String serverId) {
this.repoManager = repoManager;
this.allUsers = allUsers;
this.migration = migration;
this.patchListCache = patchListCache;
this.psUtil = psUtil;
this.serverId = serverId;
}
@@ -524,39 +516,4 @@ public class CommentsUtil {
return COMMENT_ORDER.sortedCopy(
FluentIterable.from(comments).transform(plc -> plc.asComment(serverId)));
}
public void publish(
ChangeContext ctx, PatchSet.Id psId, Collection<Comment> drafts, @Nullable String tag)
throws OrmException {
ChangeNotes notes = ctx.getNotes();
checkArgument(notes != null);
if (drafts.isEmpty()) {
return;
}
Map<PatchSet.Id, PatchSet> patchSets =
psUtil.getAsMap(
ctx.getDb(), notes, drafts.stream().map(d -> psId(notes, d)).collect(toSet()));
for (Comment d : drafts) {
PatchSet ps = patchSets.get(psId(notes, d));
if (ps == null) {
throw new OrmException("patch set " + ps + " not found");
}
d.writtenOn = ctx.getWhen();
d.tag = tag;
// Draft may have been created by a different real user; copy the current real user. (Only
// applies to X-Gerrit-RunAs, since modifying drafts via on_behalf_of is not allowed.)
ctx.getUser().updateRealAccountId(d::setRealAuthor);
try {
setCommentRevId(d, patchListCache, notes.getChange(), ps);
} catch (PatchListNotAvailableException e) {
throw new OrmException(e);
}
}
putComments(ctx.getDb(), ctx.getUpdate(psId), PUBLISHED, drafts);
}
private static PatchSet.Id psId(ChangeNotes notes, Comment c) {
return new PatchSet.Id(notes.getChangeId(), c.key.patchSetId);
}
}

View File

@@ -0,0 +1,83 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.reviewdb.client.PatchLineComment.Status.PUBLISHED;
import static java.util.stream.Collectors.toSet;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.reviewdb.client.Comment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSet.Id;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.update.ChangeContext;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Collection;
import java.util.Map;
@Singleton
public class PublishCommentUtil {
private final PatchListCache patchListCache;
private final PatchSetUtil psUtil;
private final CommentsUtil commentsUtil;
@Inject
PublishCommentUtil(
CommentsUtil commentsUtil, PatchListCache patchListCache, PatchSetUtil psUtil) {
this.commentsUtil = commentsUtil;
this.psUtil = psUtil;
this.patchListCache = patchListCache;
}
public void publish(
ChangeContext ctx, PatchSet.Id psId, Collection<Comment> drafts, @Nullable String tag)
throws OrmException {
ChangeNotes notes = ctx.getNotes();
checkArgument(notes != null);
if (drafts.isEmpty()) {
return;
}
Map<Id, PatchSet> patchSets =
psUtil.getAsMap(
ctx.getDb(), notes, drafts.stream().map(d -> psId(notes, d)).collect(toSet()));
for (Comment d : drafts) {
PatchSet ps = patchSets.get(psId(notes, d));
if (ps == null) {
throw new OrmException("patch set " + ps + " not found");
}
d.writtenOn = ctx.getWhen();
d.tag = tag;
// Draft may have been created by a different real user; copy the current real user. (Only
// applies to X-Gerrit-RunAs, since modifying drafts via on_behalf_of is not allowed.)
ctx.getUser().updateRealAccountId(d::setRealAuthor);
try {
CommentsUtil.setCommentRevId(d, patchListCache, notes.getChange(), ps);
} catch (PatchListNotAvailableException e) {
throw new OrmException(e);
}
}
commentsUtil.putComments(ctx.getDb(), ctx.getUpdate(psId), PUBLISHED, drafts);
}
private static PatchSet.Id psId(ChangeNotes notes, Comment c) {
return new PatchSet.Id(notes.getChangeId(), c.key.patchSetId);
}
}

View File

@@ -41,6 +41,7 @@ import com.google.gerrit.server.ApprovalsUtil;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.CommentsUtil;
import com.google.gerrit.server.PatchSetUtil;
import com.google.gerrit.server.PublishCommentUtil;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.change.ChangeKindCache;
import com.google.gerrit.server.change.EmailReviewComments;
@@ -107,6 +108,7 @@ public class ReplaceOp implements BatchUpdateOp {
private final ChangeKindCache changeKindCache;
private final ChangeMessagesUtil cmUtil;
private final CommentsUtil commentsUtil;
private final PublishCommentUtil publishCommentUtil;
private final EmailReviewComments.Factory emailCommentsFactory;
private final ExecutorService sendEmailExecutor;
private final RevisionCreated revisionCreated;
@@ -150,6 +152,7 @@ public class ReplaceOp implements BatchUpdateOp {
ChangeKindCache changeKindCache,
ChangeMessagesUtil cmUtil,
CommentsUtil commentsUtil,
PublishCommentUtil publishCommentUtil,
EmailReviewComments.Factory emailCommentsFactory,
RevisionCreated revisionCreated,
CommentAdded commentAdded,
@@ -176,6 +179,7 @@ public class ReplaceOp implements BatchUpdateOp {
this.changeKindCache = changeKindCache;
this.cmUtil = cmUtil;
this.commentsUtil = commentsUtil;
this.publishCommentUtil = publishCommentUtil;
this.emailCommentsFactory = emailCommentsFactory;
this.revisionCreated = revisionCreated;
this.commentAdded = commentAdded;
@@ -446,7 +450,7 @@ public class ReplaceOp implements BatchUpdateOp {
throws OrmException {
List<Comment> comments =
commentsUtil.draftByChangeAuthor(ctx.getDb(), ctx.getNotes(), ctx.getUser().getAccountId());
commentsUtil.publish(
publishCommentUtil.publish(
ctx, patchSetId, comments, ChangeMessagesUtil.uploadedPatchSetTag(workInProgress));
return comments;
}

View File

@@ -85,6 +85,7 @@ import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.OutputFormat;
import com.google.gerrit.server.PatchSetUtil;
import com.google.gerrit.server.PublishCommentUtil;
import com.google.gerrit.server.ReviewerSet;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.EmailReviewComments;
@@ -161,6 +162,7 @@ public class PostReview
private final ApprovalsUtil approvalsUtil;
private final ChangeMessagesUtil cmUtil;
private final CommentsUtil commentsUtil;
private final PublishCommentUtil publishCommentUtil;
private final PatchSetUtil psUtil;
private final PatchListCache patchListCache;
private final AccountsCollection accounts;
@@ -183,6 +185,7 @@ public class PostReview
ApprovalsUtil approvalsUtil,
ChangeMessagesUtil cmUtil,
CommentsUtil commentsUtil,
PublishCommentUtil publishCommentUtil,
PatchSetUtil psUtil,
PatchListCache patchListCache,
AccountsCollection accounts,
@@ -199,6 +202,7 @@ public class PostReview
this.changeResourceFactory = changeResourceFactory;
this.changeDataFactory = changeDataFactory;
this.commentsUtil = commentsUtil;
this.publishCommentUtil = publishCommentUtil;
this.psUtil = psUtil;
this.patchListCache = patchListCache;
this.approvalsUtil = approvalsUtil;
@@ -948,7 +952,7 @@ public class PostReview
switch (in.drafts) {
case PUBLISH:
case PUBLISH_ALL_REVISIONS:
commentsUtil.publish(ctx, psId, drafts.values(), in.tag);
publishCommentUtil.publish(ctx, psId, drafts.values(), in.tag);
comments.addAll(drafts.values());
break;
case KEEP: