Merge "Refactor BranchCommitValidator API"
This commit is contained in:
@@ -17,6 +17,8 @@ package com.google.gerrit.server.git.receive;
|
|||||||
import static com.google.gerrit.git.ObjectIds.abbreviateName;
|
import static com.google.gerrit.git.ObjectIds.abbreviateName;
|
||||||
import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
|
import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
|
||||||
|
|
||||||
|
import com.google.auto.value.AutoValue;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.gerrit.common.Nullable;
|
import com.google.gerrit.common.Nullable;
|
||||||
import com.google.gerrit.reviewdb.client.BranchNameKey;
|
import com.google.gerrit.reviewdb.client.BranchNameKey;
|
||||||
@@ -27,14 +29,12 @@ import com.google.gerrit.server.events.CommitReceivedEvent;
|
|||||||
import com.google.gerrit.server.git.validators.CommitValidationException;
|
import com.google.gerrit.server.git.validators.CommitValidationException;
|
||||||
import com.google.gerrit.server.git.validators.CommitValidationMessage;
|
import com.google.gerrit.server.git.validators.CommitValidationMessage;
|
||||||
import com.google.gerrit.server.git.validators.CommitValidators;
|
import com.google.gerrit.server.git.validators.CommitValidators;
|
||||||
import com.google.gerrit.server.git.validators.ValidationMessage;
|
|
||||||
import com.google.gerrit.server.permissions.PermissionBackend;
|
import com.google.gerrit.server.permissions.PermissionBackend;
|
||||||
import com.google.gerrit.server.project.ProjectState;
|
import com.google.gerrit.server.project.ProjectState;
|
||||||
import com.google.gerrit.server.ssh.SshInfo;
|
import com.google.gerrit.server.ssh.SshInfo;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.assistedinject.Assisted;
|
import com.google.inject.assistedinject.Assisted;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
import org.eclipse.jgit.lib.ObjectReader;
|
import org.eclipse.jgit.lib.ObjectReader;
|
||||||
import org.eclipse.jgit.notes.NoteMap;
|
import org.eclipse.jgit.notes.NoteMap;
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
@@ -56,6 +56,23 @@ public class BranchCommitValidator {
|
|||||||
ProjectState projectState, BranchNameKey branch, IdentifiedUser user);
|
ProjectState projectState, BranchNameKey branch, IdentifiedUser user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A boolean validation status and a list of additional messages. */
|
||||||
|
@AutoValue
|
||||||
|
abstract static class Result {
|
||||||
|
static Result create(boolean isValid, ImmutableList<CommitValidationMessage> messages) {
|
||||||
|
return new AutoValue_BranchCommitValidator_Result(isValid, messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether the commit is valid. */
|
||||||
|
abstract boolean isValid();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of messages related to the validation. Messages may be present regardless of the
|
||||||
|
* {@link #isValid()} status.
|
||||||
|
*/
|
||||||
|
abstract ImmutableList<CommitValidationMessage> messages();
|
||||||
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
BranchCommitValidator(
|
BranchCommitValidator(
|
||||||
CommitValidators.Factory commitValidatorsFactory,
|
CommitValidators.Factory commitValidatorsFactory,
|
||||||
@@ -80,16 +97,17 @@ public class BranchCommitValidator {
|
|||||||
* @param commit the commit being validated.
|
* @param commit the commit being validated.
|
||||||
* @param isMerged whether this is a merge commit created by magicBranch --merge option
|
* @param isMerged whether this is a merge commit created by magicBranch --merge option
|
||||||
* @param change the change for which this is a new patchset.
|
* @param change the change for which this is a new patchset.
|
||||||
|
* @return The validation {@link Result}.
|
||||||
*/
|
*/
|
||||||
public boolean validCommit(
|
Result validateCommit(
|
||||||
ObjectReader objectReader,
|
ObjectReader objectReader,
|
||||||
ReceiveCommand cmd,
|
ReceiveCommand cmd,
|
||||||
RevCommit commit,
|
RevCommit commit,
|
||||||
boolean isMerged,
|
boolean isMerged,
|
||||||
List<ValidationMessage> messages,
|
|
||||||
NoteMap rejectCommits,
|
NoteMap rejectCommits,
|
||||||
@Nullable Change change)
|
@Nullable Change change)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
ImmutableList.Builder<CommitValidationMessage> messages = new ImmutableList.Builder<>();
|
||||||
try (CommitReceivedEvent receiveEvent =
|
try (CommitReceivedEvent receiveEvent =
|
||||||
new CommitReceivedEvent(cmd, project, branch.branch(), objectReader, commit, user)) {
|
new CommitReceivedEvent(cmd, project, branch.branch(), objectReader, commit, user)) {
|
||||||
CommitValidators validators;
|
CommitValidators validators;
|
||||||
@@ -123,9 +141,9 @@ public class BranchCommitValidator {
|
|||||||
messageForCommit(commit, m.getMessage(), objectReader), m.getType()));
|
messageForCommit(commit, m.getMessage(), objectReader), m.getType()));
|
||||||
}
|
}
|
||||||
cmd.setResult(REJECTED_OTHER_REASON, messageForCommit(commit, e.getMessage(), objectReader));
|
cmd.setResult(REJECTED_OTHER_REASON, messageForCommit(commit, e.getMessage(), objectReader));
|
||||||
return false;
|
return Result.create(false, messages.build());
|
||||||
}
|
}
|
||||||
return true;
|
return Result.create(true, messages.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String messageForCommit(RevCommit c, String msg, ObjectReader objectReader)
|
private String messageForCommit(RevCommit c, String msg, ObjectReader objectReader)
|
||||||
|
@@ -1949,14 +1949,16 @@ class ReceiveCommits {
|
|||||||
BranchCommitValidator validator =
|
BranchCommitValidator validator =
|
||||||
commitValidatorFactory.create(projectState, changeEnt.getDest(), user);
|
commitValidatorFactory.create(projectState, changeEnt.getDest(), user);
|
||||||
try {
|
try {
|
||||||
if (validator.validCommit(
|
BranchCommitValidator.Result validationResult =
|
||||||
receivePack.getRevWalk().getObjectReader(),
|
validator.validateCommit(
|
||||||
cmd,
|
receivePack.getRevWalk().getObjectReader(),
|
||||||
newCommit,
|
cmd,
|
||||||
false,
|
newCommit,
|
||||||
messages,
|
false,
|
||||||
rejectCommits,
|
rejectCommits,
|
||||||
changeEnt)) {
|
changeEnt);
|
||||||
|
messages.addAll(validationResult.messages());
|
||||||
|
if (validationResult.isValid()) {
|
||||||
logger.atFine().log("Replacing change %s", changeEnt.getId());
|
logger.atFine().log("Replacing change %s", changeEnt.getId());
|
||||||
requestReplace(cmd, true, changeEnt, newCommit);
|
requestReplace(cmd, true, changeEnt, newCommit);
|
||||||
}
|
}
|
||||||
@@ -2114,14 +2116,16 @@ class ReceiveCommits {
|
|||||||
logger.atFine().log("Creating new change for %s even though it is already tracked", name);
|
logger.atFine().log("Creating new change for %s even though it is already tracked", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validator.validCommit(
|
BranchCommitValidator.Result validationResult =
|
||||||
receivePack.getRevWalk().getObjectReader(),
|
validator.validateCommit(
|
||||||
magicBranch.cmd,
|
receivePack.getRevWalk().getObjectReader(),
|
||||||
c,
|
magicBranch.cmd,
|
||||||
magicBranch.merged,
|
c,
|
||||||
messages,
|
magicBranch.merged,
|
||||||
rejectCommits,
|
rejectCommits,
|
||||||
null)) {
|
null);
|
||||||
|
messages.addAll(validationResult.messages());
|
||||||
|
if (!validationResult.isValid()) {
|
||||||
// Not a change the user can propose? Abort as early as possible.
|
// Not a change the user can propose? Abort as early as possible.
|
||||||
logger.atFine().log("Aborting early due to invalid commit");
|
logger.atFine().log("Aborting early due to invalid commit");
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -3113,8 +3117,10 @@ class ReceiveCommits {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validator.validCommit(
|
BranchCommitValidator.Result validationResult =
|
||||||
walk.getObjectReader(), cmd, c, false, messages, rejectCommits, null)) {
|
validator.validateCommit(walk.getObjectReader(), cmd, c, false, rejectCommits, null);
|
||||||
|
messages.addAll(validationResult.messages());
|
||||||
|
if (!validationResult.isValid()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user