Validate downstream branches exist.

Ensure that a change exists and is uploaded for each downstream
branch before allowing submit. If it does not, raise a user-facing
error so that they can correct it.

Change-Id: Ic488fd3144e31ed380879176f7a61ef330e9b0e0
diff --git a/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java b/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java
index 9d61595..5265920 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 The Android Open Source Project
+// Copyright (C) 2017 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.
@@ -26,6 +26,7 @@
 import com.google.gerrit.extensions.restapi.RestApiModule;
 import com.google.gerrit.extensions.webui.JavaScriptPlugin;
 import com.google.gerrit.extensions.webui.WebUiPlugin;
+import com.google.gerrit.server.git.validators.MergeValidationListener;
 import com.google.inject.AbstractModule;
 
 /** Module to bind listeners, plugins, and other modules. */
@@ -39,6 +40,7 @@
     DynamicSet.bind(binder(), DraftPublishedListener.class).to(DownstreamCreator.class);
     DynamicSet.bind(binder(), RevisionCreatedListener.class).to(DownstreamCreator.class);
     DynamicSet.bind(binder(), TopicEditedListener.class).to(DownstreamCreator.class);
+    DynamicSet.bind(binder(), MergeValidationListener.class).to(MergeValidator.class);
     install(
         new RestApiModule() {
           @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/automerger/MergeValidator.java b/src/main/java/com/googlesource/gerrit/plugins/automerger/MergeValidator.java
new file mode 100644
index 0000000..2722904
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/MergeValidator.java
@@ -0,0 +1,117 @@
+// Copyright (C) 2017 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.googlesource.gerrit.plugins.automerger;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.gerrit.extensions.api.GerritApi;
+import com.google.gerrit.extensions.client.ListChangesOption;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.extensions.common.CommitInfo;
+import com.google.gerrit.extensions.common.RevisionInfo;
+import com.google.gerrit.extensions.restapi.RestApiException;
+import com.google.gerrit.reviewdb.client.Branch.NameKey;
+import com.google.gerrit.reviewdb.client.PatchSet.Id;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.git.CodeReviewCommit;
+import com.google.gerrit.server.git.validators.MergeValidationException;
+import com.google.gerrit.server.git.validators.MergeValidationListener;
+import com.google.gerrit.server.project.ProjectState;
+import com.google.inject.Inject;
+import java.io.IOException;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * MergeValidator will validate that all downstream changes are uploaded for review before
+ * submission.
+ */
+public class MergeValidator implements MergeValidationListener {
+  private static final Logger log = LoggerFactory.getLogger(MergeValidator.class);
+
+  protected GerritApi gApi;
+  protected ConfigLoader config;
+
+  @Inject
+  public MergeValidator(GerritApi gApi, ConfigLoader config) {
+    this.gApi = gApi;
+    this.config = config;
+  }
+
+  @Override
+  public void onPreMerge(
+      Repository repo,
+      CodeReviewCommit commit,
+      ProjectState destProject,
+      NameKey destBranch,
+      Id patchSetId,
+      IdentifiedUser caller)
+      throws MergeValidationException {
+    int changeId = commit.change().getChangeId();
+    try {
+      ChangeInfo upstreamChange =
+          gApi.changes().id(changeId).get(EnumSet.of(ListChangesOption.CURRENT_REVISION));
+      Set<String> missingDownstreams = getMissingDownstreamMerges(upstreamChange);
+      if (!missingDownstreams.isEmpty()) {
+        throw new MergeValidationException(
+            "Missing downstream branches for "
+                + missingDownstreams
+                + ". Please recreate the automerges.");
+      }
+    } catch (RestApiException | IOException | ConfigInvalidException e) {
+      log.error("Automerger plugin failed onPreMerge for {}", changeId, e);
+      e.printStackTrace();
+      throw new MergeValidationException("Error when validating merge for: " + changeId);
+    }
+  }
+
+  @VisibleForTesting
+  protected Set<String> getMissingDownstreamMerges(ChangeInfo upstreamChange)
+      throws RestApiException, IOException, ConfigInvalidException {
+    Set<String> missingDownstreamBranches = new HashSet<>();
+
+    Set<String> downstreamBranches =
+        config.getDownstreamBranches(upstreamChange.branch, upstreamChange.project);
+    for (String downstreamBranch : downstreamBranches) {
+      boolean dsExists = false;
+      String query = "topic:" + upstreamChange.topic + " status:open branch:" + downstreamBranch;
+      List<ChangeInfo> changes =
+          gApi.changes()
+              .query(query)
+              .withOptions(ListChangesOption.ALL_REVISIONS, ListChangesOption.CURRENT_COMMIT)
+              .get();
+      for (ChangeInfo change : changes) {
+        RevisionInfo revision = change.revisions.get(change.currentRevision);
+        List<CommitInfo> parents = revision.commit.parents;
+        if (parents.size() > 1) {
+          String secondParent = parents.get(1).commit;
+          if (secondParent.equals(upstreamChange.currentRevision)) {
+            dsExists = true;
+            break;
+          }
+        }
+      }
+      if (!dsExists) {
+        missingDownstreamBranches.add(downstreamBranch);
+      }
+    }
+    return missingDownstreamBranches;
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java
new file mode 100644
index 0000000..126dc02
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java
@@ -0,0 +1,84 @@
+// Copyright (C) 2017 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.googlesource.gerrit.plugins.automerger;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.CharStreams;
+import com.google.gerrit.acceptance.GitUtil;
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.extensions.restapi.ResourceConflictException;
+import com.google.gerrit.reviewdb.client.Branch;
+import com.google.gerrit.reviewdb.client.RefNames;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
+import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.Config;
+import org.junit.Test;
+
+@TestPlugin(
+  name = "automerger",
+  sysModule = "com.googlesource.gerrit.plugins.automerger.AutomergerModule"
+)
+public class MergeValidatorIT extends LightweightPluginDaemonTest {
+  private void pushConfig(String resourceName, String project) throws Exception {
+    TestRepository<InMemoryRepository> allProjectRepo = cloneProject(allProjects, admin);
+    GitUtil.fetch(allProjectRepo, RefNames.REFS_CONFIG + ":config");
+    allProjectRepo.reset("config");
+    try (InputStream in = getClass().getResourceAsStream(resourceName)) {
+      String resourceString = CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8));
+
+      Config cfg = new Config();
+      cfg.fromText(resourceString);
+      // Update manifest project path to the result of createProject(resourceName), since it is
+      // scoped to the test method
+      cfg.setString("automerger", "master:ds_one", "setProjects", project);
+      PushOneCommit push =
+          pushFactory.create(
+              db, admin.getIdent(), allProjectRepo, "Subject", "automerger.config", cfg.toText());
+      push.to(RefNames.REFS_CONFIG).assertOkStatus();
+    }
+  }
+
+  @Test
+  public void testNoMissingDownstreamMerges() throws Exception {
+    // Create initial change
+    PushOneCommit.Result result = createChange("subject", "filename", "content", "testtopic");
+    // Project name is scoped by test, so we need to get it from our initial change
+    String projectName = result.getChange().change().getProject().get();
+    createBranch(new Branch.NameKey(projectName, "ds_one"));
+    pushConfig("automerger.config", projectName);
+    // After we upload our config, we upload a new patchset to create the downstreams
+    amendChange(result.getChangeId());
+    result.assertOkStatus();
+    merge(result);
+  }
+
+  @Test
+  public void testMissingDownstreamMerges() throws Exception {
+    // Create initial change
+    PushOneCommit.Result result = createChange("subject", "filename", "content", "testtopic");
+    pushConfig("automerger.config", result.getChange().project().get());
+    result.assertOkStatus();
+    // Assert we are missing downstreams
+    exception.expect(ResourceConflictException.class);
+    exception.expectMessage(
+        "Failed to submit 1 change due to the following problems:\n"
+            + "Change 1: Missing downstream branches for [ds_one]. Please recreate the automerges.");
+    merge(result);
+  }
+}