OnSubmitValidationListener: Don't expose Repository

If we give extensions a full Repository, it's possible to use it in an
incorrect way. For example, a RevWalk created with `new RevWalk(repo)`
will not be able to read the ObjectIds returned by `getCommands()`.
Instead, expose only a RevWalk and a getRef method that calls into the
ChainedReceiveCommands. This is more limiting, because it can't do
everything a Repository can do, but it is impossible to use in this
incorrect way.

The only known existing OnSubmitValidationListener is in the
git-numberer plugin[1], which will be able to do what it needs with this
restricted interface.

[1] https://chromium-review.googlesource.com/q/project:infra%252Fgerrit-plugins%252Fgit-numberer

Change-Id: I4a20ea34e49e30714e269b46835b55c43d7a121e
This commit is contained in:
Dave Borowitz
2017-03-22 18:29:21 -07:00
parent 4283674087
commit 563634e70a
5 changed files with 77 additions and 59 deletions

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.acceptance.rest.change;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static com.google.common.truth.Truth8.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static com.google.gerrit.extensions.client.ListChangesOption.CURRENT_REVISION;
import static com.google.gerrit.extensions.client.ListChangesOption.DETAILED_LABELS;
@@ -95,6 +96,7 @@ import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -756,11 +758,16 @@ public abstract class AbstractSubmit extends AbstractDaemonTest {
new OnSubmitValidationListener() {
@Override
public void preBranchUpdate(Arguments args) throws ValidationException {
assertThat(args.getCommands().keySet()).contains("refs/heads/master");
try (RevWalk rw = args.newRevWalk()) {
rw.parseBody(rw.parseCommit(args.getCommands().get("refs/heads/master").getNewId()));
String master = "refs/heads/master";
assertThat(args.getCommands()).containsKey(master);
ReceiveCommand cmd = args.getCommands().get(master);
ObjectId newMasterId = cmd.getNewId();
try (Repository repo = repoManager.openRepository(project)) {
assertThat(repo.exactRef(master).getObjectId()).isEqualTo(cmd.getOldId());
assertThat(args.getRef(master)).hasValue(newMasterId);
args.getRevWalk().parseBody(args.getRevWalk().parseCommit(newMasterId));
} catch (IOException e) {
assertThat(e).isNull();
throw new AssertionError("failed checking new ref value", e);
}
projectsCalled.add(args.getProject().get());
if (projectsCalled.size() == 2) {