From c75304637f5535e634e57d4ed933c0fdb594d890 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 28 Oct 2011 08:08:00 -0400 Subject: [PATCH] Added a list branches rpc --- docs/02_rpc.mkd | 7 ++++++- src/com/gitblit/Constants.java | 2 +- src/com/gitblit/GitBlitException.java | 4 ++++ src/com/gitblit/RpcFilter.java | 2 +- src/com/gitblit/RpcServlet.java | 27 +++++++++++++++++++++++++++ src/com/gitblit/utils/RpcUtils.java | 21 +++++++++++++++++++++ tests/com/gitblit/tests/RpcTests.java | 16 ++++++++++++---- 7 files changed, 72 insertions(+), 7 deletions(-) diff --git a/docs/02_rpc.mkd b/docs/02_rpc.mkd index 13bf8eff..bf2cf24b 100644 --- a/docs/02_rpc.mkd +++ b/docs/02_rpc.mkd @@ -18,6 +18,7 @@ The Gitblit RPC mechanism, like the Gitblit JGit servlet, syndication/feed servl req=name=post bodyresponse body web.enableRpcServlet=true LIST_REPOSITORIES---Map<String, RepositoryModel> +LIST_BRANCHES---Map<String, List<String>> web.enableRpcManagement=true CREATE_REPOSITORYrepository nameadminRepositoryModel- EDIT_REPOSITORYrepository nameadminRepositoryModel- @@ -208,6 +209,9 @@ The original repository name is specified in the *name* url parameter. The new
 {
   "bootDate": "2011-10-22T12:13:00Z",
+  "version": "0.7.0-SNAPSHOT",
+  "releaseDate": "PENDING",
+  "isGO": true,
   "systemProperties": {
     "file.encoding": "Cp1252",
     "java.home": "C:\\Program Files\\Java\\jdk1.6.0_26\\jre",
@@ -226,6 +230,7 @@ The original repository name is specified in the *name* url parameter.  The new
   },
   "heapAllocated": 128057344,
   "heapFree": 120399168,
-  "heapSize": 1899560960
+  "heapSize": 1899560960,
+  "servletContainer": "jetty/7.4.3.v20110701"
 }
 
\ No newline at end of file diff --git a/src/com/gitblit/Constants.java b/src/com/gitblit/Constants.java index 450c11fb..25143051 100644 --- a/src/com/gitblit/Constants.java +++ b/src/com/gitblit/Constants.java @@ -201,7 +201,7 @@ public class Constants { * a client. */ public static enum RpcRequest { - LIST_REPOSITORIES, CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY, + LIST_REPOSITORIES, LIST_BRANCHES, CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY, LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER, LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS, LIST_SETTINGS, EDIT_SETTINGS, diff --git a/src/com/gitblit/GitBlitException.java b/src/com/gitblit/GitBlitException.java index 360f9f0a..7ab0f99d 100644 --- a/src/com/gitblit/GitBlitException.java +++ b/src/com/gitblit/GitBlitException.java @@ -31,6 +31,10 @@ public class GitBlitException extends IOException { super(message); } + public GitBlitException(Throwable cause) { + super(cause); + } + /** * Exception to indicate that the client should prompt for credentials * because the requested action requires authentication. diff --git a/src/com/gitblit/RpcFilter.java b/src/com/gitblit/RpcFilter.java index 551ca3c6..e768a514 100644 --- a/src/com/gitblit/RpcFilter.java +++ b/src/com/gitblit/RpcFilter.java @@ -64,7 +64,7 @@ public class RpcFilter extends AuthenticationFilter { return; } - boolean adminRequest = requestType.exceeds(RpcRequest.LIST_REPOSITORIES); + boolean adminRequest = requestType.exceeds(RpcRequest.LIST_BRANCHES); // conditionally reject all rpc requests if (!GitBlit.getBoolean(Keys.web.enableRpcServlet, true)) { diff --git a/src/com/gitblit/RpcServlet.java b/src/com/gitblit/RpcServlet.java index 53628a09..b068a39b 100644 --- a/src/com/gitblit/RpcServlet.java +++ b/src/com/gitblit/RpcServlet.java @@ -27,11 +27,15 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jgit.lib.Repository; + import com.gitblit.Constants.RpcRequest; +import com.gitblit.models.RefModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.ServerSettings; import com.gitblit.models.UserModel; import com.gitblit.utils.HttpUtils; +import com.gitblit.utils.JGitUtils; import com.gitblit.utils.RpcUtils; /** @@ -86,6 +90,29 @@ public class RpcServlet extends JsonServlet { repositories.put(url, model); } result = repositories; + } else if (RpcRequest.LIST_BRANCHES.equals(reqType)) { + // list all branches in all repositories accessible to user + Map> allBranches = new HashMap>(); + List models = GitBlit.self().getRepositoryModels(user); + for (RepositoryModel model : models) { + if (!model.hasCommits) { + // skip empty repository + continue; + } + // get branches + Repository repository = GitBlit.self().getRepository(model.name); + List refs = JGitUtils.getLocalBranches(repository, false, -1); + refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1)); + if (refs.size() > 0) { + List branches = new ArrayList(); + for (RefModel ref : refs) { + branches.add(ref.getName()); + } + allBranches.put(model.name, branches); + } + repository.close(); + } + result = allBranches; } else if (RpcRequest.LIST_USERS.equals(reqType)) { // list users List names = GitBlit.self().getAllUsernames(); diff --git a/src/com/gitblit/utils/RpcUtils.java b/src/com/gitblit/utils/RpcUtils.java index e5841518..faa910ba 100644 --- a/src/com/gitblit/utils/RpcUtils.java +++ b/src/com/gitblit/utils/RpcUtils.java @@ -62,6 +62,9 @@ public class RpcUtils { private static final Type SETS_TYPE = new TypeToken>() { }.getType(); + private static final Type BRANCHES_TYPE = new TypeToken>>() { + }.getType(); + /** * * @param remoteURL @@ -385,6 +388,24 @@ public class RpcUtils { return status; } + /** + * Retrieves a map of all branches in the Gitblit server keyed by + * repository. + * + * @param serverUrl + * @param account + * @param password + * @return + * @throws IOException + */ + public static Map> getAllBranches(String serverUrl, + String account, char[] password) throws IOException { + String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES); + Map> allReferences = JsonUtils.retrieveJson(url, + BRANCHES_TYPE, account, password); + return allReferences; + } + /** * Do the specified administrative action on the Gitblit server. * diff --git a/tests/com/gitblit/tests/RpcTests.java b/tests/com/gitblit/tests/RpcTests.java index 2860f32b..637d488e 100644 --- a/tests/com/gitblit/tests/RpcTests.java +++ b/tests/com/gitblit/tests/RpcTests.java @@ -16,6 +16,7 @@ package com.gitblit.tests; import java.io.IOException; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -220,25 +221,32 @@ public class RpcTests extends TestCase { public void testUpdateSettings() throws Exception { Map updated = new HashMap(); - + // grab current setting ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray()); boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true); showSizes = !showSizes; - + // update setting updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes)); boolean success = RpcUtils.updateSettings(updated, "http://localhost:8080/gb", account, password.toCharArray()); assertTrue("Failed to update server settings", success); - + // confirm setting change settings = RpcUtils.getSettings(url, account, password.toCharArray()); boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false); assertEquals(newValue, showSizes); - + // restore setting newValue = !newValue; updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue)); } + + public void testBranches() throws Exception { + Map> branches = RpcUtils.getAllBranches(url, account, + password.toCharArray()); + assertTrue(branches != null); + assertTrue(branches.size() > 0); + } } -- 2.39.5