From f76fee63ed9cb3a30d3c0c092d860b1cb93a481b Mon Sep 17 00:00:00 2001
From: Gerard Smyth <gerard.smyth@gmail.com>
Date: Thu, 08 May 2014 13:09:30 -0400
Subject: [PATCH] Updated the SyndicationServlet to provide an additional option to return details of the tags in the repository instead of the commits. This uses a new 'ot' request parameter to indicate the object type of the content to return, which can be ither TAG or COMMIT. If this is not provided, then COMMIT is assumed to maintain backwards compatability. If tags are returned, then the paging parameters, 'l' and 'pg' are still supported, but searching options are currently ignored.

---
 src/main/java/com/gitblit/servlet/RpcServlet.java |   72 +++++++++++++++++++++++++++++------
 1 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java
index e7b3ed2..b8cdfb0 100644
--- a/src/main/java/com/gitblit/servlet/RpcServlet.java
+++ b/src/main/java/com/gitblit/servlet/RpcServlet.java
@@ -53,13 +53,12 @@
  * Handles remote procedure calls.
  *
  * @author James Moger
- *
  */
 public class RpcServlet extends JsonServlet {
 
 	private static final long serialVersionUID = 1L;
 
-	public static final int PROTOCOL_VERSION = 6;
+	public static final int PROTOCOL_VERSION = 8;
 
 	private IStoredSettings settings;
 
@@ -80,12 +79,11 @@
 	 * @throws java.io.IOException
 	 */
 	@Override
-	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
-			throws ServletException, IOException {
+	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
+			IOException {
 		RpcRequest reqType = RpcRequest.fromName(request.getParameter("req"));
 		String objectName = request.getParameter("name");
-		logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType,
-				request.getRemoteAddr()));
+		logger.info(MessageFormat.format("Rpc {0} request from {1}", reqType, request.getRemoteAddr()));
 
 		UserModel user = (UserModel) request.getUserPrincipal();
 
@@ -101,7 +99,10 @@
 			result = PROTOCOL_VERSION;
 		} else if (RpcRequest.LIST_REPOSITORIES.equals(reqType)) {
 			// Determine the Gitblit clone url
-			String gitblitUrl = HttpUtils.getGitblitURL(request);
+			String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null);
+			if (StringUtils.isEmpty(gitblitUrl)) {
+				gitblitUrl = HttpUtils.getGitblitURL(request);
+			}
 			StringBuilder sb = new StringBuilder();
 			sb.append(gitblitUrl);
 			sb.append(Constants.R_PATH);
@@ -127,7 +128,8 @@
 				}
 				if (model.isCollectingGarbage) {
 					// skip garbage collecting repository
-					logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage", model.name));
+					logger.warn(MessageFormat.format("Temporarily excluding {0} from RPC, busy collecting garbage",
+							model.name));
 					continue;
 				}
 				// get local branches
@@ -192,6 +194,33 @@
 				gitblit.updateRepositoryModel(model.name, model, true);
 			} catch (GitBlitException e) {
 				response.setStatus(failureCode);
+			}
+		} else if (RpcRequest.FORK_REPOSITORY.equals(reqType)) {
+			// fork repository
+			RepositoryModel origin = gitblit.getRepositoryModel(objectName);
+			if (origin == null) {
+				// failed to find repository, error is logged by the repository
+				// manager
+				response.setStatus(failureCode);
+			} else {
+				if (user == null || !user.canFork(origin)) {
+					logger.error("User {} is not permitted to fork '{}'!", user == null ? "anonymous" : user.username,
+							objectName);
+					response.setStatus(failureCode);
+				} else {
+					try {
+						// fork the origin
+						RepositoryModel fork = gitblit.fork(origin, user);
+						if (fork == null) {
+							logger.error("Failed to fork repository '{}'!", objectName);
+							response.setStatus(failureCode);
+						} else {
+							logger.info("User {} has forked '{}'!", user.username, objectName);
+						}
+					} catch (GitBlitException e) {
+						response.setStatus(failureCode);
+					}
+				}
 			}
 		} else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
 			// edit repository
@@ -278,7 +307,8 @@
 		} else if (RpcRequest.SET_REPOSITORY_MEMBER_PERMISSIONS.equals(reqType)) {
 			// set the repository permissions for the specified users
 			RepositoryModel model = gitblit.getRepositoryModel(objectName);
-			Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
+			Collection<RegistrantAccessPermission> permissions = deserialize(request, response,
+					RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
 			result = gitblit.setUserAccessPermissions(model, permissions);
 		} else if (RpcRequest.LIST_REPOSITORY_TEAMS.equals(reqType)) {
 			// get repository teams
@@ -294,7 +324,8 @@
 		} else if (RpcRequest.SET_REPOSITORY_TEAM_PERMISSIONS.equals(reqType)) {
 			// set the repository permissions for the specified teams
 			RepositoryModel model = gitblit.getRepositoryModel(objectName);
-			Collection<RegistrantAccessPermission> permissions = deserialize(request, response, RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
+			Collection<RegistrantAccessPermission> permissions = deserialize(request, response,
+					RpcUtils.REGISTRANT_PERMISSIONS_TYPE);
 			result = gitblit.setTeamAccessPermissions(model, permissions);
 		} else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
 			// return the list of federation registrations
@@ -320,7 +351,10 @@
 		} else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
 			// return the list of federation sets
 			if (allowAdmin && gitblit.canFederate()) {
-				String gitblitUrl = HttpUtils.getGitblitURL(request);
+				String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null);
+				if (StringUtils.isEmpty(gitblitUrl)) {
+					gitblitUrl = HttpUtils.getGitblitURL(request);
+				}
 				result = gitblit.getFederationSets(gitblitUrl);
 			} else {
 				response.sendError(notAllowedCode);
@@ -357,8 +391,7 @@
 		} else if (RpcRequest.EDIT_SETTINGS.equals(reqType)) {
 			// update settings on the server
 			if (allowAdmin) {
-				Map<String, String> map = deserialize(request, response,
-						RpcUtils.SETTINGS_TYPE);
+				Map<String, String> map = deserialize(request, response, RpcUtils.SETTINGS_TYPE);
 				gitblit.updateSettings(map);
 			} else {
 				response.sendError(notAllowedCode);
@@ -377,6 +410,19 @@
 			} else {
 				response.sendError(notAllowedCode);
 			}
+		} else if (RpcRequest.REINDEX_TICKETS.equals(reqType)) {
+			if (allowManagement) {
+				if (StringUtils.isEmpty(objectName)) {
+					// reindex all tickets
+					gitblit.getTicketService().reindex();
+				} else {
+					// reindex tickets in a specific repository
+					RepositoryModel model = gitblit.getRepositoryModel(objectName);
+					gitblit.getTicketService().reindex(model);
+				}
+			} else {
+				response.sendError(notAllowedCode);
+			}
 		}
 
 		// send the result of the request

--
Gitblit v1.9.1