review: Refactor to reuse same code as PublishComments web UI
This way the two interfaces to the same action have the same behavior, and stay in sync with each other if any changes are made in the future. Change-Id: Iad6bedbc445fbd102a53e593787b32ce9bc3942e
This commit is contained in:
committed by
Shawn O. Pearce
parent
322d8b2eca
commit
bc61b54f29
@@ -14,13 +14,11 @@
|
|||||||
|
|
||||||
package com.google.gerrit.sshd.commands;
|
package com.google.gerrit.sshd.commands;
|
||||||
|
|
||||||
import com.google.gerrit.common.ChangeHookRunner;
|
|
||||||
import com.google.gerrit.common.data.ApprovalType;
|
import com.google.gerrit.common.data.ApprovalType;
|
||||||
import com.google.gerrit.common.data.ApprovalTypes;
|
import com.google.gerrit.common.data.ApprovalTypes;
|
||||||
import com.google.gerrit.reviewdb.ApprovalCategory;
|
import com.google.gerrit.reviewdb.ApprovalCategory;
|
||||||
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
|
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
|
||||||
import com.google.gerrit.reviewdb.Change;
|
import com.google.gerrit.reviewdb.Change;
|
||||||
import com.google.gerrit.reviewdb.ChangeMessage;
|
|
||||||
import com.google.gerrit.reviewdb.PatchSet;
|
import com.google.gerrit.reviewdb.PatchSet;
|
||||||
import com.google.gerrit.reviewdb.PatchSetApproval;
|
import com.google.gerrit.reviewdb.PatchSetApproval;
|
||||||
import com.google.gerrit.reviewdb.RevId;
|
import com.google.gerrit.reviewdb.RevId;
|
||||||
@@ -28,10 +26,7 @@ import com.google.gerrit.reviewdb.ReviewDb;
|
|||||||
import com.google.gerrit.server.ChangeUtil;
|
import com.google.gerrit.server.ChangeUtil;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.git.MergeQueue;
|
import com.google.gerrit.server.git.MergeQueue;
|
||||||
import com.google.gerrit.server.mail.CommentSender;
|
import com.google.gerrit.server.patch.PublishComments;
|
||||||
import com.google.gerrit.server.mail.EmailException;
|
|
||||||
import com.google.gerrit.server.patch.PatchSetInfoFactory;
|
|
||||||
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
|
|
||||||
import com.google.gerrit.server.project.CanSubmitResult;
|
import com.google.gerrit.server.project.CanSubmitResult;
|
||||||
import com.google.gerrit.server.project.ChangeControl;
|
import com.google.gerrit.server.project.ChangeControl;
|
||||||
import com.google.gerrit.server.project.NoSuchChangeException;
|
import com.google.gerrit.server.project.NoSuchChangeException;
|
||||||
@@ -52,10 +47,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class ReviewCommand extends BaseCommand {
|
public class ReviewCommand extends BaseCommand {
|
||||||
@@ -102,12 +95,6 @@ public class ReviewCommand extends BaseCommand {
|
|||||||
@Inject
|
@Inject
|
||||||
private MergeQueue merger;
|
private MergeQueue merger;
|
||||||
|
|
||||||
@Inject
|
|
||||||
private CommentSender.Factory commentSenderFactory;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private PatchSetInfoFactory patchSetInfoFactory;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ApprovalTypes approvalTypes;
|
private ApprovalTypes approvalTypes;
|
||||||
|
|
||||||
@@ -117,11 +104,11 @@ public class ReviewCommand extends BaseCommand {
|
|||||||
@Inject
|
@Inject
|
||||||
private FunctionState.Factory functionStateFactory;
|
private FunctionState.Factory functionStateFactory;
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ChangeHookRunner hooks;
|
|
||||||
|
|
||||||
private List<ApproveOption> optionList;
|
private List<ApproveOption> optionList;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private PublishComments.Factory publishCommentsFactory;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void start(final Environment env) {
|
public final void start(final Environment env) {
|
||||||
startThread(new CommandRunnable() {
|
startThread(new CommandRunnable() {
|
||||||
@@ -153,70 +140,26 @@ public class ReviewCommand extends BaseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void approveOne(final PatchSet.Id patchSetId)
|
private void approveOne(final PatchSet.Id patchSetId)
|
||||||
throws NoSuchChangeException, UnloggedFailure, OrmException,
|
throws NoSuchChangeException, UnloggedFailure, OrmException {
|
||||||
PatchSetInfoNotAvailableException, EmailException {
|
|
||||||
final Change.Id changeId = patchSetId.getParentKey();
|
final Change.Id changeId = patchSetId.getParentKey();
|
||||||
final ChangeControl changeControl =
|
final ChangeControl changeControl =
|
||||||
changeControlFactory.validateFor(changeId);
|
changeControlFactory.validateFor(changeId);
|
||||||
final Change change = changeControl.getChange();
|
|
||||||
|
|
||||||
final StringBuffer msgBuf = new StringBuffer();
|
if (changeComment == null) {
|
||||||
msgBuf.append("Patch Set ");
|
changeComment = "";
|
||||||
msgBuf.append(patchSetId.get());
|
}
|
||||||
msgBuf.append(": ");
|
|
||||||
|
|
||||||
final Map<ApprovalCategory.Id, ApprovalCategoryValue.Id> approvalsMap =
|
Set<ApprovalCategoryValue.Id> aps = new HashSet<ApprovalCategoryValue.Id>();
|
||||||
new HashMap<ApprovalCategory.Id, ApprovalCategoryValue.Id>();
|
for (ApproveOption ao : optionList) {
|
||||||
|
Short v = ao.value();
|
||||||
if (change.getStatus().isOpen()) {
|
if (v != null) {
|
||||||
for (ApproveOption co : optionList) {
|
assertScoreIsAllowed(patchSetId, changeControl, ao, v);
|
||||||
final ApprovalCategory.Id category = co.getCategoryId();
|
aps.add(new ApprovalCategoryValue.Id(ao.getCategoryId(), v));
|
||||||
PatchSetApproval.Key psaKey =
|
|
||||||
new PatchSetApproval.Key(patchSetId, currentUser.getAccountId(),
|
|
||||||
category);
|
|
||||||
PatchSetApproval psa = db.patchSetApprovals().get(psaKey);
|
|
||||||
|
|
||||||
Short score = co.value();
|
|
||||||
|
|
||||||
if (score != null) {
|
|
||||||
addApproval(psaKey, score, change, co);
|
|
||||||
} else {
|
|
||||||
if (psa == null) {
|
|
||||||
score = 0;
|
|
||||||
addApproval(psaKey, score, change, co);
|
|
||||||
} else {
|
|
||||||
score = psa.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final ApprovalCategoryValue.Id val =
|
|
||||||
new ApprovalCategoryValue.Id(category, score);
|
|
||||||
|
|
||||||
String message = db.approvalCategoryValues().get(val).getName();
|
|
||||||
msgBuf.append(" " + message + ";");
|
|
||||||
approvalsMap.put(category, val);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msgBuf.deleteCharAt(msgBuf.length() - 1);
|
publishCommentsFactory.create(patchSetId, changeComment, aps).call();
|
||||||
msgBuf.append("\n\n");
|
|
||||||
|
|
||||||
if (changeComment != null) {
|
|
||||||
msgBuf.append(changeComment);
|
|
||||||
}
|
|
||||||
|
|
||||||
String uuid = ChangeUtil.messageUUID(db);
|
|
||||||
ChangeMessage cm =
|
|
||||||
new ChangeMessage(new ChangeMessage.Key(changeId, uuid), currentUser
|
|
||||||
.getAccountId());
|
|
||||||
cm.setMessage(msgBuf.toString());
|
|
||||||
db.changeMessages().insert(Collections.singleton(cm));
|
|
||||||
|
|
||||||
ChangeUtil.touch(change, db);
|
|
||||||
sendMail(change, change.currentPatchSetId(), cm);
|
|
||||||
|
|
||||||
hooks.doCommentAddedHook(change, currentUser.getAccount(), db.patchSets()
|
|
||||||
.get(patchSetId), changeComment, approvalsMap);
|
|
||||||
|
|
||||||
if (submitChange) {
|
if (submitChange) {
|
||||||
CanSubmitResult result =
|
CanSubmitResult result =
|
||||||
@@ -294,34 +237,20 @@ public class ReviewCommand extends BaseCommand {
|
|||||||
return projectControl.getProject().getNameKey().equals(change.getProject());
|
return projectControl.getProject().getNameKey().equals(change.getProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMail(final Change c, final PatchSet.Id psid,
|
private void assertScoreIsAllowed(final PatchSet.Id patchSetId,
|
||||||
final ChangeMessage message) throws PatchSetInfoNotAvailableException,
|
final ChangeControl changeControl, ApproveOption ao, Short v)
|
||||||
EmailException, OrmException {
|
throws UnloggedFailure {
|
||||||
PatchSet ps = db.patchSets().get(psid);
|
final PatchSetApproval psa =
|
||||||
final CommentSender cm;
|
new PatchSetApproval(new PatchSetApproval.Key(patchSetId, currentUser
|
||||||
cm = commentSenderFactory.create(c);
|
.getAccountId(), ao.getCategoryId()), v);
|
||||||
cm.setFrom(currentUser.getAccountId());
|
|
||||||
cm.setPatchSet(ps, patchSetInfoFactory.get(psid));
|
|
||||||
cm.setChangeMessage(message);
|
|
||||||
cm.setReviewDb(db);
|
|
||||||
cm.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addApproval(final PatchSetApproval.Key psaKey,
|
|
||||||
final Short score, final Change c, final ApproveOption co)
|
|
||||||
throws OrmException, UnloggedFailure {
|
|
||||||
final PatchSetApproval psa = new PatchSetApproval(psaKey, score);
|
|
||||||
final List<PatchSetApproval> approvals = Collections.emptyList();
|
|
||||||
final FunctionState fs =
|
final FunctionState fs =
|
||||||
functionStateFactory.create(c, psaKey.getParentKey(), approvals);
|
functionStateFactory.create(changeControl.getChange(), patchSetId,
|
||||||
psa.setValue(score);
|
Collections.<PatchSetApproval> emptyList());
|
||||||
|
psa.setValue(v);
|
||||||
fs.normalize(approvalTypes.getApprovalType(psa.getCategoryId()), psa);
|
fs.normalize(approvalTypes.getApprovalType(psa.getCategoryId()), psa);
|
||||||
if (score != psa.getValue()) {
|
if (v != psa.getValue()) {
|
||||||
throw error(co.name() + "=" + co.value() + " not permitted");
|
throw error(ao.name() + "=" + ao.value() + " not permitted");
|
||||||
}
|
}
|
||||||
|
|
||||||
psa.setGranted();
|
|
||||||
db.patchSetApprovals().upsert(Collections.singleton(psa));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initOptionList() {
|
private void initOptionList() {
|
||||||
|
|||||||
Reference in New Issue
Block a user