Implement ConfigLoader#getUpstreamChanges.

Change-Id: I7167215764b60bc06047a32214f0f7f062720880
diff --git a/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java b/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java
index 2ecb88b..1a35009 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java
@@ -188,6 +188,38 @@
   }
 
   /**
+   * Gets the upstream branches of the given branch and project.
+   *
+   * @param toBranch The downstream branch we would merge to.
+   * @param project The project we are merging.
+   * @return The branches upstream of the given branch for the given project.
+   * @throws RestApiException
+   * @throws IOException
+   * @throws ConfigInvalidException
+   */
+  public Set<String> getUpstreamBranches(String toBranch, String project)
+      throws ConfigInvalidException, RestApiException, IOException {
+    Set<String> upstreamBranches = new HashSet<String>();
+    // List all subsections of automerger, split by :
+    Set<String> subsections = getConfig().getSubsections(pluginName);
+    for (String subsection : subsections) {
+      // Subsections are of the form "fromBranch:toBranch"
+      String[] branchPair = subsection.split(Pattern.quote(BRANCH_DELIMITER));
+      if (branchPair.length != 2) {
+        throw new ConfigInvalidException("Automerger config branch pair malformed: " + subsection);
+      }
+      if (toBranch.equals(branchPair[1])) {
+        // If toBranch matches, check if project is in both their manifests
+        Set<String> projectsInScope = getProjectsInScope(branchPair[0], branchPair[1]);
+        if (projectsInScope.contains(project)) {
+          upstreamBranches.add(branchPair[0]);
+        }
+      }
+    }
+    return upstreamBranches;
+  }
+
+  /**
    * Gets the downstream branches of the given branch and project.
    *
    * @param fromBranch The branch we are merging from.
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
index d692e95..85ff90c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
@@ -146,6 +146,23 @@
   }
 
   @Test
+  public void upstreamBranchesTest() throws Exception {
+    defaultSetup("automerger.config");
+    Set<String> expectedBranches = new HashSet<String>();
+    expectedBranches.add("master");
+    assertThat(configLoader.getUpstreamBranches("ds_two", "platform/some/project"))
+        .isEqualTo(expectedBranches);
+  }
+
+  @Test
+  public void upstreamBranchesTest_nonexistentBranch() throws Exception {
+    defaultSetup("automerger.config");
+    Set<String> expectedBranches = new HashSet<String>();
+    assertThat(configLoader.getUpstreamBranches("master", "platform/some/project"))
+        .isEqualTo(expectedBranches);
+  }
+
+  @Test
   public void downstreamBranchesTest() throws Exception {
     defaultSetup("automerger.config");
     Set<String> expectedBranches = new HashSet<String>();