Add REST API endpoint for all downstream branches.
In addition to having an endpoint telling us what branches are a single
automerger downstream hop down, we also have an endpoint telling us all
the branches that are downstream of a given branch.
Change-Id: Ie839e1dd8e9764b9134478569f5c9024efbea285
diff --git a/src/main/java/com/googlesource/gerrit/plugins/automerger/AllConfigDownstreamAction.java b/src/main/java/com/googlesource/gerrit/plugins/automerger/AllConfigDownstreamAction.java
new file mode 100644
index 0000000..a28c050
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/AllConfigDownstreamAction.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2016 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.collect.Lists;
+import com.google.gerrit.extensions.restapi.ResourceConflictException;
+import com.google.gerrit.extensions.restapi.Response;
+import com.google.gerrit.extensions.restapi.RestApiException;
+import com.google.gerrit.extensions.restapi.RestReadView;
+import com.google.gerrit.server.project.BranchResource;
+import com.google.inject.Inject;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.eclipse.jgit.errors.ConfigInvalidException;
+
+/** The logic behind auto-filling the branch map, aka the input to AutomergeChangeAction. */
+class AllConfigDownstreamAction implements RestReadView<BranchResource> {
+
+ protected ConfigLoader config;
+
+ /**
+ * Initializer for this class that sets the config.
+ *
+ * @param config Config for this plugin.
+ */
+ @Inject
+ public AllConfigDownstreamAction(ConfigLoader config) {
+ this.config = config;
+ }
+
+ /**
+ * Return the list of all branch names that are downstream.
+ *
+ * @param branchResource BranchResource which we are attempting to find the downstreams of.
+ * @return A list of all branch names downstream of the given branch.
+ * @throws RestApiException
+ * @throws IOException
+ */
+ @Override
+ public Response<List<String>> apply(BranchResource branchResource)
+ throws RestApiException, IOException {
+
+ String branchName = branchResource.getBranchKey().getShortName();
+ String projectName = branchResource.getName();
+
+ try {
+ Set<String> downstreamBranches = config.getAllDownstreamBranches(branchName, projectName);
+ return Response.created(Lists.newArrayList(downstreamBranches));
+ } catch (ConfigInvalidException e) {
+ throw new ResourceConflictException(
+ "Automerger configuration file is invalid: " + e.getMessage());
+ }
+ }
+}
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 5265920..456b8e2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/AutomergerModule.java
@@ -15,6 +15,7 @@
package com.googlesource.gerrit.plugins.automerger;
import static com.google.gerrit.server.change.RevisionResource.REVISION_KIND;
+import static com.google.gerrit.server.project.BranchResource.BRANCH_KIND;
import com.google.gerrit.extensions.events.ChangeAbandonedListener;
import com.google.gerrit.extensions.events.ChangeRestoredListener;
@@ -47,6 +48,7 @@
protected void configure() {
post(REVISION_KIND, "automerge-change").to(AutomergeChangeAction.class);
post(REVISION_KIND, "config-downstream").to(ConfigDownstreamAction.class);
+ get(BRANCH_KIND, "all-config-downstream").to(AllConfigDownstreamAction.class);
}
});
DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin("automerger.js"));
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 ad9530f..7f0f6d5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/automerger/ConfigLoader.java
@@ -213,6 +213,17 @@
return downstreamBranches;
}
+ public Set<String> getAllDownstreamBranches(String branch, String project)
+ throws RestApiException, IOException, ConfigInvalidException {
+ Set<String> downstreamBranches = new HashSet<String>();
+ Set<String> immediateDownstreams = getDownstreamBranches(branch, project);
+ downstreamBranches.addAll(immediateDownstreams);
+ for (String immediateDownstream : immediateDownstreams) {
+ downstreamBranches.addAll(getAllDownstreamBranches(immediateDownstream, project));
+ }
+ return downstreamBranches;
+ }
+
public String getMissingDownstreamsMessage() throws ConfigInvalidException {
String message = getConfig().getString("global", null, "missingDownstreamsMessage");
if (message == null) {
diff --git a/src/main/resources/Documentation/rest-api-all-config-downstream.md b/src/main/resources/Documentation/rest-api-all-config-downstream.md
new file mode 100644
index 0000000..3eee7eb
--- /dev/null
+++ b/src/main/resources/Documentation/rest-api-all-config-downstream.md
@@ -0,0 +1,33 @@
+automerger config-downstream
+=============================
+
+NAME
+----
+all-config-downstream - Get set of all downstream branches
+
+SYNOPSIS
+--------
+> GET /projects/{project-name}/branches/{branch-id}/automerger~all-config-downstream
+
+DESCRIPTION
+-----------
+Returns a list of branch names that are downstream, including ones more than one
+hop away.
+
+REQUEST
+-----------
+```
+ GET /projects/{project-name}/branches/{branch-id}/automerger~all-config-downstream HTTP/1.0
+```
+
+RESPONSE
+-----------
+```
+ HTTP/1.1 200 OK
+ Content-Disposition: attachment
+ Content-Type: application/json;charset=UTF-8
+ )]}'
+ [
+ "master", "branch_two"
+ ]
+```
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 b5afb95..5b35c08 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
@@ -166,6 +166,16 @@
configLoader.getDownstreamBranches("master", "platform/some/project");
}
+ @Test
+ public void getAllDownstreamBranchesTest() throws Exception {
+ defaultSetup("automerger.config");
+ Set<String> expectedBranches = new HashSet<String>();
+ expectedBranches.add("ds_two");
+ expectedBranches.add("ds_three");
+ assertThat(configLoader.getAllDownstreamBranches("master", "platform/some/project"))
+ .isEqualTo(expectedBranches);
+ }
+
private void defaultSetup(String resourceName) throws Exception {
createProject("All-Projects");
manifestNameKey = createProject("platform/manifest");