Refactor AbandonChange code into gerrit-server
While most of the logic for AbandonChange was already in gerrit-server in ChangeUtil, this change moves it into it's own class. Using a ReviewResult will allow us to perform one of several related review actions when responding to an ssh/rpc request and then handle the result (or more specifically, the errors associated with it) in a single block of code. Change-Id: Icb70f2edbb4968a2cc43e78eba71f16f7f93d6e4
This commit is contained in:
@@ -17,6 +17,7 @@ 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.ApprovalTypes;
|
||||
import com.google.gerrit.common.data.ReviewResult;
|
||||
import com.google.gerrit.common.data.SubmitRecord;
|
||||
import com.google.gerrit.reviewdb.ApprovalCategory;
|
||||
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
|
||||
@@ -28,6 +29,7 @@ import com.google.gerrit.reviewdb.RevId;
|
||||
import com.google.gerrit.reviewdb.ReviewDb;
|
||||
import com.google.gerrit.server.ChangeUtil;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.changedetail.AbandonChange;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.git.MergeOp;
|
||||
import com.google.gerrit.server.git.MergeQueue;
|
||||
@@ -132,6 +134,9 @@ public class ReviewCommand extends BaseCommand {
|
||||
@Inject
|
||||
private ChangeControl.Factory changeControlFactory;
|
||||
|
||||
@Inject
|
||||
private AbandonChange.Factory abandonChangeFactory;
|
||||
|
||||
@Inject
|
||||
private AbandonedSender.Factory abandonedSenderFactory;
|
||||
|
||||
@@ -251,6 +256,8 @@ public class ReviewCommand extends BaseCommand {
|
||||
NoSuchChangeException, OrmException, EmailException, Failure {
|
||||
|
||||
final Change.Id changeId = patchSetId.getParentKey();
|
||||
|
||||
ReviewResult result = null;
|
||||
ChangeControl changeControl = changeControlFactory.validateFor(changeId);
|
||||
|
||||
if (changeComment == null) {
|
||||
@@ -270,12 +277,7 @@ public class ReviewCommand extends BaseCommand {
|
||||
publishCommentsFactory.create(patchSetId, changeComment, aps, forceMessage).call();
|
||||
|
||||
if (abandonChange) {
|
||||
if (changeControl.canAbandon()) {
|
||||
ChangeUtil.abandon(patchSetId, currentUser, changeComment, db,
|
||||
abandonedSenderFactory, hooks);
|
||||
} else {
|
||||
throw error("Not permitted to abandon change");
|
||||
}
|
||||
result = abandonChangeFactory.create(patchSetId, changeComment).call();
|
||||
}
|
||||
|
||||
if (restoreChange) {
|
||||
@@ -294,11 +296,11 @@ public class ReviewCommand extends BaseCommand {
|
||||
}
|
||||
|
||||
if (submitChange) {
|
||||
List<SubmitRecord> result = changeControl.canSubmit(db, patchSetId);
|
||||
if (result.isEmpty()) {
|
||||
List<SubmitRecord> submitResult = changeControl.canSubmit(db, patchSetId);
|
||||
if (submitResult.isEmpty()) {
|
||||
throw new Failure(1, "ChangeControl.canSubmit returned empty list");
|
||||
}
|
||||
switch (result.get(0).status) {
|
||||
switch (submitResult.get(0).status) {
|
||||
case OK:
|
||||
if (changeControl.getRefControl().canSubmit()) {
|
||||
toSubmit.add(patchSetId);
|
||||
@@ -309,7 +311,7 @@ public class ReviewCommand extends BaseCommand {
|
||||
|
||||
case NOT_READY: {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
for (SubmitRecord.Label lbl : result.get(0).labels) {
|
||||
for (SubmitRecord.Label lbl : submitResult.get(0).labels) {
|
||||
switch (lbl.status) {
|
||||
case OK:
|
||||
break;
|
||||
@@ -341,14 +343,14 @@ public class ReviewCommand extends BaseCommand {
|
||||
throw error("change " + changeId + " is closed");
|
||||
|
||||
case RULE_ERROR:
|
||||
if (result.get(0).errorMessage != null) {
|
||||
throw error("change " + changeId + ": " + result.get(0).errorMessage);
|
||||
if (submitResult.get(0).errorMessage != null) {
|
||||
throw error("change " + changeId + ": " + submitResult.get(0).errorMessage);
|
||||
} else {
|
||||
throw error("change " + changeId + ": internal rule error");
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Failure(1, "Unsupported status " + result.get(0).status);
|
||||
throw new Failure(1, "Unsupported status " + submitResult.get(0).status);
|
||||
}
|
||||
}
|
||||
if (publishPatchSet) {
|
||||
@@ -371,6 +373,17 @@ public class ReviewCommand extends BaseCommand {
|
||||
throw error("Not permitted to delete draft patchset");
|
||||
}
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
for (ReviewResult.Error resultError : result.getErrors()) {
|
||||
switch (resultError.getType()) {
|
||||
case ABANDON_NOT_PERMITTED:
|
||||
writeError("error: not permitted to abandon change");
|
||||
default:
|
||||
writeError("error: failure in review");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<PatchSet.Id> parsePatchSetId(final String patchIdentity)
|
||||
|
Reference in New Issue
Block a user