From 03d5ee0bcc4b9d7b6814d7212d19d1126638810d Mon Sep 17 00:00:00 2001 From: manisha Date: Fri, 21 Mar 2014 14:27:02 +0530 Subject: Implement FORK RPC request type --- src/main/java/com/gitblit/utils/RpcUtils.java | 31 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/gitblit/utils') diff --git a/src/main/java/com/gitblit/utils/RpcUtils.java b/src/main/java/com/gitblit/utils/RpcUtils.java index 5e577fb6..904461c8 100644 --- a/src/main/java/com/gitblit/utils/RpcUtils.java +++ b/src/main/java/com/gitblit/utils/RpcUtils.java @@ -25,16 +25,7 @@ import java.util.Map; import com.gitblit.Constants; import com.gitblit.Constants.RpcRequest; import com.gitblit.GitBlitException.UnknownRequestException; -import com.gitblit.models.FederationModel; -import com.gitblit.models.FederationProposal; -import com.gitblit.models.FederationSet; -import com.gitblit.models.FeedModel; -import com.gitblit.models.RegistrantAccessPermission; -import com.gitblit.models.RepositoryModel; -import com.gitblit.models.ServerSettings; -import com.gitblit.models.ServerStatus; -import com.gitblit.models.TeamModel; -import com.gitblit.models.UserModel; +import com.gitblit.models.*; import com.google.gson.reflect.TypeToken; /** @@ -203,7 +194,25 @@ public class RpcUtils { } - /** + /** + * Create a fork of an already existing repo + * + * @param repository + * @param user + + * @return true if the action succeeded + * @throws IOException + */ + public static boolean forkRpository(RepositoryModel repository, UserModel user, String serverUrl, + String account, char[] password) throws IOException { + UserRepositoryCompositeModel userRepositoryCompositeModel = new UserRepositoryCompositeModel(); + userRepositoryCompositeModel.setRepositoryModel(repository); + userRepositoryCompositeModel.setUserModel(user); + return doAction(RpcRequest.FORK_REPOSITORY, null, userRepositoryCompositeModel, serverUrl, account, password); + } + + + /** * Send a revised version of the repository model to the Gitblit server. * * @param repository -- cgit v1.2.3 From 813a1fab72a462bdd9387ac3f137055853a69710 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 8 May 2014 11:11:04 -0400 Subject: Revise, complete, and fully test the FORK rpc --- releases.moxie | 6 +- src/main/java/com/gitblit/Constants.java | 7 ++- .../models/UserRepositoryCompositeModel.java | 47 --------------- src/main/java/com/gitblit/servlet/RpcServlet.java | 50 ++++++++++------ src/main/java/com/gitblit/utils/RpcUtils.java | 21 ++++--- src/site/rpc.mkd | 8 ++- src/test/config/test-users.conf | 1 + src/test/java/com/gitblit/tests/RpcTests.java | 69 ++++++++++++++++++++-- src/test/java/com/gitblit/tests/Test.java | 11 ---- 9 files changed, 122 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/com/gitblit/models/UserRepositoryCompositeModel.java delete mode 100644 src/test/java/com/gitblit/tests/Test.java (limited to 'src/main/java/com/gitblit/utils') diff --git a/releases.moxie b/releases.moxie index aac46f0c..228fc7e6 100644 --- a/releases.moxie +++ b/releases.moxie @@ -11,9 +11,11 @@ r24: { security: ~ fixes: ~ changes: ~ - additions: ~ + additions: + - Add FORK_REPOSITORY RPC request type (issue-371, pr-161, ticket-65) dependencyChanges: ~ - contributors: ~ + contributors: + - Manisha Gayathri } # diff --git a/src/main/java/com/gitblit/Constants.java b/src/main/java/com/gitblit/Constants.java index cbbc0cac..c8ce83c1 100644 --- a/src/main/java/com/gitblit/Constants.java +++ b/src/main/java/com/gitblit/Constants.java @@ -350,10 +350,11 @@ public class Constants { * a client. */ public static enum RpcRequest { - // Order is important here. anything above LIST_SETTINGS requires + // Order is important here. anything after LIST_SETTINGS requires // administrator privileges and web.allowRpcManagement. - CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS, - CREATE_REPOSITORY, FORK_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY, + CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, + FORK_REPOSITORY, LIST_SETTINGS, + CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY, LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER, LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM, LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS, diff --git a/src/main/java/com/gitblit/models/UserRepositoryCompositeModel.java b/src/main/java/com/gitblit/models/UserRepositoryCompositeModel.java deleted file mode 100644 index 122bb3ba..00000000 --- a/src/main/java/com/gitblit/models/UserRepositoryCompositeModel.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.gitblit.models; - -/* - * Copyright 2011 gitblit.com. - * - * 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. - */ - - -import com.gitblit.utils.StringUtils; - -import java.io.Serializable; - -public class UserRepositoryCompositeModel implements Serializable { - - private static final long serialVersionUID = 1L; - - public UserModel userModel; - public RepositoryModel repositoryModel; - - public UserModel getUserModel() { - return userModel; - } - - public void setUserModel(UserModel userModel) { - this.userModel = userModel; - } - - public RepositoryModel getRepositoryModel() { - return repositoryModel; - } - - public void setRepositoryModel(RepositoryModel repositoryModel) { - this.repositoryModel = repositoryModel; - } - -} \ No newline at end of file diff --git a/src/main/java/com/gitblit/servlet/RpcServlet.java b/src/main/java/com/gitblit/servlet/RpcServlet.java index 80c50714..b3016ad6 100644 --- a/src/main/java/com/gitblit/servlet/RpcServlet.java +++ b/src/main/java/com/gitblit/servlet/RpcServlet.java @@ -27,7 +27,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import com.gitblit.models.*; import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants; @@ -36,6 +35,12 @@ import com.gitblit.GitBlitException; import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.manager.IGitblit; +import com.gitblit.models.RefModel; +import com.gitblit.models.RegistrantAccessPermission; +import com.gitblit.models.RepositoryModel; +import com.gitblit.models.ServerSettings; +import com.gitblit.models.TeamModel; +import com.gitblit.models.UserModel; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.HttpUtils; import com.gitblit.utils.JGitUtils; @@ -53,7 +58,7 @@ public class RpcServlet extends JsonServlet { private static final long serialVersionUID = 1L; - public static final int PROTOCOL_VERSION = 7; + public static final int PROTOCOL_VERSION = 8; private IStoredSettings settings; @@ -191,22 +196,31 @@ public class RpcServlet extends JsonServlet { response.setStatus(failureCode); } } else if (RpcRequest.FORK_REPOSITORY.equals(reqType)) { - // fork repository - UserRepositoryCompositeModel userRepositoryCompositeModel = deserialize(request, response, - UserRepositoryCompositeModel.class); - RepositoryModel repoModel = userRepositoryCompositeModel.getRepositoryModel(); - UserModel userModel = userRepositoryCompositeModel.getUserModel(); - try { - if (repoModel != null && userModel != null) { - gitblit.fork(repoModel, userModel); - } else { - System.out.println("Non existing user model or repo model"); - response.setStatus(failureCode); - } - - } catch (GitBlitException e) { - response.setStatus(failureCode); - } + // 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 RepositoryModel model = deserialize(request, response, RepositoryModel.class); diff --git a/src/main/java/com/gitblit/utils/RpcUtils.java b/src/main/java/com/gitblit/utils/RpcUtils.java index 904461c8..82202154 100644 --- a/src/main/java/com/gitblit/utils/RpcUtils.java +++ b/src/main/java/com/gitblit/utils/RpcUtils.java @@ -25,7 +25,16 @@ import java.util.Map; import com.gitblit.Constants; import com.gitblit.Constants.RpcRequest; import com.gitblit.GitBlitException.UnknownRequestException; -import com.gitblit.models.*; +import com.gitblit.models.FederationModel; +import com.gitblit.models.FederationProposal; +import com.gitblit.models.FederationSet; +import com.gitblit.models.FeedModel; +import com.gitblit.models.RegistrantAccessPermission; +import com.gitblit.models.RepositoryModel; +import com.gitblit.models.ServerSettings; +import com.gitblit.models.ServerStatus; +import com.gitblit.models.TeamModel; +import com.gitblit.models.UserModel; import com.google.gson.reflect.TypeToken; /** @@ -195,20 +204,16 @@ public class RpcUtils { } /** - * Create a fork of an already existing repo + * Create a fork of a repository. * * @param repository - * @param user * @return true if the action succeeded * @throws IOException */ - public static boolean forkRpository(RepositoryModel repository, UserModel user, String serverUrl, + public static boolean forkRepository(RepositoryModel repository, String serverUrl, String account, char[] password) throws IOException { - UserRepositoryCompositeModel userRepositoryCompositeModel = new UserRepositoryCompositeModel(); - userRepositoryCompositeModel.setRepositoryModel(repository); - userRepositoryCompositeModel.setUserModel(user); - return doAction(RpcRequest.FORK_REPOSITORY, null, userRepositoryCompositeModel, serverUrl, account, password); + return doAction(RpcRequest.FORK_REPOSITORY, repository.name, null, serverUrl, account, password); } diff --git a/src/site/rpc.mkd b/src/site/rpc.mkd index b86fd9ad..2e502e2c 100644 --- a/src/site/rpc.mkd +++ b/src/site/rpc.mkd @@ -57,9 +57,10 @@ The Gitblit API includes methods for retrieving and interpreting RSS feeds. The Gitblit v0.8.02 Gitblit v0.9.0 - v1.0.03 Gitblit v1.1.04 -Gitblit v1.2.0+5 -Gitblit v1.3.1+6 -Gitblit v1.4.0+7 +Gitblit v1.2.05 +Gitblit v1.3.16 +Gitblit v1.4.07 +Gitblit v1.6.08 @@ -81,6 +82,7 @@ Use *SET_REPOSITORY_TEAM_PERMISSIONS* instead. LIST_BRANCHES--1-Map<String, List<String>> LIST_SETTINGS--1-ServerSettings (basic keys) GET_USERuser name-6-UserModel +FORK_REPOSITORYrepository name-8-- web.enableRpcManagement=true CREATE_REPOSITORYrepository nameadmin1RepositoryModel- EDIT_REPOSITORYrepository nameadmin1RepositoryModel- diff --git a/src/test/config/test-users.conf b/src/test/config/test-users.conf index 4e00903b..1d01f846 100644 --- a/src/test/config/test-users.conf +++ b/src/test/config/test-users.conf @@ -6,6 +6,7 @@ role = "#notfederated" [user "sampleuser"] password = sampleuser + cookie = 6e07ed42149fc166206319faffdfba2e2ec82e43 accountType = LOCAL role = "#none" [team "admins"] diff --git a/src/test/java/com/gitblit/tests/RpcTests.java b/src/test/java/com/gitblit/tests/RpcTests.java index 943153de..51b4671b 100644 --- a/src/test/java/com/gitblit/tests/RpcTests.java +++ b/src/test/java/com/gitblit/tests/RpcTests.java @@ -191,12 +191,6 @@ public class RpcTests extends GitblitUnitTest { assertEquals(AccessRestrictionType.VIEW, retrievedRepository.accessRestriction); assertEquals(AuthorizationControl.AUTHENTICATED, retrievedRepository.authorizationControl); - //fork repo - UserModel userModel = new UserModel("garbageUser"); - assertTrue("Failed to create Fork Repository!", - RpcUtils.forkRpository(model, userModel, url, account, password.toCharArray())); - - // rename and change access restriciton String originalName = model.name; model.name = "garbagerepo2.git"; @@ -403,4 +397,67 @@ public class RpcTests extends GitblitUnitTest { assertNotNull(branches); assertTrue(branches.size() > 0); } + + @Test + public void testFork() throws Exception { + // test forking by an administrator + // admins are all-powerful and can fork the unforakable :) + testFork(account, password, true, true); + testFork(account, password, false, true); + + // test forking by a permitted normal user + UserModel forkUser = new UserModel("forkuser"); + forkUser.password = forkUser.username; + forkUser.canFork = true; + RpcUtils.deleteUser(forkUser, url, account, password.toCharArray()); + RpcUtils.createUser(forkUser, url, account, password.toCharArray()); + testFork(forkUser.username, forkUser.password, true, true); + testFork(forkUser.username, forkUser.password, false, false); + RpcUtils.deleteUser(forkUser, url, account, password.toCharArray()); + + // test forking by a non-permitted normal user + UserModel noForkUser = new UserModel("noforkuser"); + noForkUser.password = noForkUser.username; + noForkUser.canFork = false; + RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray()); + RpcUtils.createUser(noForkUser, url, account, password.toCharArray()); + testFork(forkUser.username, forkUser.password, true, false); + testFork(forkUser.username, forkUser.password, false, false); + RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray()); + } + + private void testFork(String forkAcct, String forkAcctPassword, boolean allowForks, boolean expectSuccess) throws Exception { + // test does not exist + RepositoryModel dne = new RepositoryModel(); + dne.name = "doesNotExist.git"; + assertFalse(String.format("Successfully forked %s!", dne.name), + RpcUtils.forkRepository(dne, url, forkAcct, forkAcctPassword.toCharArray())); + + // delete any previous fork + RepositoryModel fork = findRepository(String.format("~%s/helloworld.git", forkAcct)); + if (fork != null) { + RpcUtils.deleteRepository(fork, url, account, password.toCharArray()); + } + + // update the origin to allow forks or not + RepositoryModel origin = findRepository("helloworld.git"); + origin.allowForks = allowForks; + RpcUtils.updateRepository(origin.name, origin, url, account, password.toCharArray()); + + // fork the repository + if (expectSuccess) { + assertTrue(String.format("Failed to fork %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + } else { + assertFalse(String.format("Successfully forked %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + } + + // attempt another fork + assertFalse(String.format("Successfully forked %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + + // delete the fork repository + RpcUtils.deleteRepository(fork, url, account, password.toCharArray()); + } } diff --git a/src/test/java/com/gitblit/tests/Test.java b/src/test/java/com/gitblit/tests/Test.java deleted file mode 100644 index 275c6b4c..00000000 --- a/src/test/java/com/gitblit/tests/Test.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.gitblit.tests; - -/** - * Created with IntelliJ IDEA. - * User: manisha - * Date: 3/21/14 - * Time: 12:03 PM - * To change this template use File | Settings | File Templates. - */ -public @interface Test { -} -- cgit v1.2.3 From f76fee63ed9cb3a30d3c0c092d860b1cb93a481b Mon Sep 17 00:00:00 2001 From: Gerard Smyth Date: Tue, 1 Apr 2014 12:29:01 +0100 Subject: 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/Constants.java | 21 ++++ .../com/gitblit/servlet/SyndicationServlet.java | 135 ++++++++++++++------- src/main/java/com/gitblit/utils/JGitUtils.java | 48 +++++++- 3 files changed, 159 insertions(+), 45 deletions(-) (limited to 'src/main/java/com/gitblit/utils') diff --git a/src/main/java/com/gitblit/Constants.java b/src/main/java/com/gitblit/Constants.java index c8ce83c1..2007c0a8 100644 --- a/src/main/java/com/gitblit/Constants.java +++ b/src/main/java/com/gitblit/Constants.java @@ -402,6 +402,27 @@ public class Constants { } } + /** + * Enumeration of the feed content object types. + */ + public static enum FeedContentObjectType { + COMMIT, TAG; + + public static FeedContentObjectType forName(String name) { + for (FeedContentObjectType type : values()) { + if (type.name().equalsIgnoreCase(name)) { + return type; + } + } + return COMMIT; + } + + @Override + public String toString() { + return name().toLowerCase(); + } + } + /** * The types of objects that can be indexed and queried. */ diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index 24def995..9650ee3a 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -163,6 +163,15 @@ public class SyndicationServlet extends DaggerServlet { searchType = type; } } + + Constants.FeedContentObjectType objectType = Constants.FeedContentObjectType.COMMIT; + if (!StringUtils.isEmpty(request.getParameter("ot"))) { + Constants.FeedContentObjectType type = Constants.FeedContentObjectType.forName(request.getParameter("ot")); + if (type != null) { + objectType = type; + } + } + int length = settings.getInteger(Keys.web.syndicationEntries, 25); if (StringUtils.isEmpty(objectId)) { objectId = org.eclipse.jgit.lib.Constants.HEAD; @@ -214,14 +223,7 @@ public class SyndicationServlet extends DaggerServlet { boolean mountParameters = settings.getBoolean(Keys.web.mountParameters, true); - String urlPattern; - if (mountParameters) { - // mounted parameters - urlPattern = "{0}/commit/{1}/{2}"; - } else { - // parameterized parameters - urlPattern = "{0}/commit/?r={1}&h={2}"; - } + String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null); if (StringUtils.isEmpty(gitblitUrl)) { gitblitUrl = HttpUtils.getGitblitURL(request); @@ -247,47 +249,92 @@ public class SyndicationServlet extends DaggerServlet { feedDescription = model.description; } - List commits; - if (StringUtils.isEmpty(searchString)) { - // standard log/history lookup - commits = JGitUtils.getRevLog(repository, objectId, offset, length); + if (objectType == Constants.FeedContentObjectType.TAG) { + + String urlPattern; + if (mountParameters) { + // mounted parameters + urlPattern = "{0}/tag/{1}/{2}"; + } else { + // parameterized parameters + urlPattern = "{0}/tag/?r={1}&h={2}"; + } + + List tags = JGitUtils.getTags(repository, false, length, offset); + + for (RefModel tag : tags) { + FeedEntryModel entry = new FeedEntryModel(); + entry.title = tag.getName(); + entry.author = tag.getAuthorIdent().getName(); + entry.link = MessageFormat.format(urlPattern, gitblitUrl, + StringUtils.encodeURL(model.name.replace('/', fsc)), tag.getObjectId().getName()); + entry.published = tag.getDate(); + entry.contentType = "text/html"; + entry.content = tag.getFullMessage(); + entry.repository = model.name; + entry.branch = objectId; + + entry.tags = new ArrayList(); + + // add tag id and referenced commit id + entry.tags.add("tag:" + tag.getObjectId().getName()); + entry.tags.add("commit:" + tag.getReferencedObjectId().getName()); + + entries.add(entry); + } } else { - // repository search - commits = JGitUtils.searchRevlogs(repository, objectId, searchString, searchType, - offset, length); - } - Map> allRefs = JGitUtils.getAllRefs(repository, model.showRemoteBranches); - BugtraqProcessor processor = new BugtraqProcessor(settings); - - // convert RevCommit to SyndicatedEntryModel - for (RevCommit commit : commits) { - FeedEntryModel entry = new FeedEntryModel(); - entry.title = commit.getShortMessage(); - entry.author = commit.getAuthorIdent().getName(); - entry.link = MessageFormat.format(urlPattern, gitblitUrl, - StringUtils.encodeURL(model.name.replace('/', fsc)), commit.getName()); - entry.published = commit.getCommitterIdent().getWhen(); - entry.contentType = "text/html"; - String message = processor.processCommitMessage(repository, model, commit.getFullMessage()); - entry.content = message; - entry.repository = model.name; - entry.branch = objectId; - entry.tags = new ArrayList(); - - // add commit id and parent commit ids - entry.tags.add("commit:" + commit.getName()); - for (RevCommit parent : commit.getParents()) { - entry.tags.add("parent:" + parent.getName()); + + String urlPattern; + if (mountParameters) { + // mounted parameters + urlPattern = "{0}/commit/{1}/{2}"; + } else { + // parameterized parameters + urlPattern = "{0}/commit/?r={1}&h={2}"; } - // add refs to tabs list - List refs = allRefs.get(commit.getId()); - if (refs != null && refs.size() > 0) { - for (RefModel ref : refs) { - entry.tags.add("ref:" + ref.getName()); + List commits; + if (StringUtils.isEmpty(searchString)) { + // standard log/history lookup + commits = JGitUtils.getRevLog(repository, objectId, offset, length); + } else { + // repository search + commits = JGitUtils.searchRevlogs(repository, objectId, searchString, searchType, + offset, length); + } + Map> allRefs = JGitUtils.getAllRefs(repository, model.showRemoteBranches); + BugtraqProcessor processor = new BugtraqProcessor(settings); + + // convert RevCommit to SyndicatedEntryModel + for (RevCommit commit : commits) { + FeedEntryModel entry = new FeedEntryModel(); + entry.title = commit.getShortMessage(); + entry.author = commit.getAuthorIdent().getName(); + entry.link = MessageFormat.format(urlPattern, gitblitUrl, + StringUtils.encodeURL(model.name.replace('/', fsc)), commit.getName()); + entry.published = commit.getCommitterIdent().getWhen(); + entry.contentType = "text/html"; + String message = processor.processCommitMessage(repository, model, commit.getFullMessage()); + entry.content = message; + entry.repository = model.name; + entry.branch = objectId; + entry.tags = new ArrayList(); + + // add commit id and parent commit ids + entry.tags.add("commit:" + commit.getName()); + for (RevCommit parent : commit.getParents()) { + entry.tags.add("parent:" + parent.getName()); + } + + // add refs to tabs list + List refs = allRefs.get(commit.getId()); + if (refs != null && refs.size() > 0) { + for (RefModel ref : refs) { + entry.tags.add("ref:" + ref.getName()); + } } + entries.add(entry); } - entries.add(entry); } } diff --git a/src/main/java/com/gitblit/utils/JGitUtils.java b/src/main/java/com/gitblit/utils/JGitUtils.java index 190872a7..da51ea98 100644 --- a/src/main/java/com/gitblit/utils/JGitUtils.java +++ b/src/main/java/com/gitblit/utils/JGitUtils.java @@ -1667,6 +1667,24 @@ public class JGitUtils { return getRefs(repository, Constants.R_TAGS, fullName, maxCount); } + /** + * Returns the list of tags in the repository. If repository does not exist + * or is empty, an empty list is returned. + * + * @param repository + * @param fullName + * if true, /refs/tags/yadayadayada is returned. If false, + * yadayadayada is returned. + * @param maxCount + * if < 0, all tags are returned + * @param offset + * if maxCount provided sets the starting point of the records to return + * @return list of tags + */ + public static List getTags(Repository repository, boolean fullName, int maxCount, int offset) { + return getRefs(repository, Constants.R_TAGS, fullName, maxCount, offset); + } + /** * Returns the list of local branches in the repository. If repository does * not exist or is empty, an empty list is returned. @@ -1748,6 +1766,27 @@ public class JGitUtils { */ private static List getRefs(Repository repository, String refs, boolean fullName, int maxCount) { + return getRefs(repository, refs, fullName, maxCount, 0); + } + + /** + * Returns a list of references in the repository matching "refs". If the + * repository is null or empty, an empty list is returned. + * + * @param repository + * @param refs + * if unspecified, all refs are returned + * @param fullName + * if true, /refs/something/yadayadayada is returned. If false, + * yadayadayada is returned. + * @param maxCount + * if < 0, all references are returned + * @param offset + * if maxCount provided sets the starting point of the records to return + * @return list of references + */ + private static List getRefs(Repository repository, String refs, boolean fullName, + int maxCount, int offset) { List list = new ArrayList(); if (maxCount == 0) { return list; @@ -1771,7 +1810,14 @@ public class JGitUtils { Collections.sort(list); Collections.reverse(list); if (maxCount > 0 && list.size() > maxCount) { - list = new ArrayList(list.subList(0, maxCount)); + if (offset < 0) { + offset = 0; + } + int endIndex = offset + maxCount; + if (endIndex > list.size()) { + endIndex = list.size(); + } + list = new ArrayList(list.subList(offset, endIndex)); } } catch (IOException e) { error(e, repository, "{0} failed to retrieve {1}", refs); -- cgit v1.2.3 From 9ff0c16b05cb0eb7c3cc63eda763b0f75d84853c Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 8 May 2014 13:09:08 -0400 Subject: Change enum name and unit test RSS tag queries --- releases.moxie | 2 + src/main/java/com/gitblit/Constants.java | 6 +-- .../com/gitblit/servlet/SyndicationServlet.java | 6 +-- .../java/com/gitblit/utils/SyndicationUtils.java | 57 ++++++++++++++++++++++ src/site/rpc.mkd | 1 + .../com/gitblit/tests/SyndicationUtilsTest.java | 36 +++++++++++++- 6 files changed, 101 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/gitblit/utils') diff --git a/releases.moxie b/releases.moxie index 228fc7e6..96a0ae91 100644 --- a/releases.moxie +++ b/releases.moxie @@ -13,9 +13,11 @@ r24: { changes: ~ additions: - Add FORK_REPOSITORY RPC request type (issue-371, pr-161, ticket-65) + - Add object type (ot) parameter for RSS queries to retrieve tag details (pr-165, ticket-66) dependencyChanges: ~ contributors: - Manisha Gayathri + - Gerard Smyth } # diff --git a/src/main/java/com/gitblit/Constants.java b/src/main/java/com/gitblit/Constants.java index 2007c0a8..95eb944a 100644 --- a/src/main/java/com/gitblit/Constants.java +++ b/src/main/java/com/gitblit/Constants.java @@ -405,11 +405,11 @@ public class Constants { /** * Enumeration of the feed content object types. */ - public static enum FeedContentObjectType { + public static enum FeedObjectType { COMMIT, TAG; - public static FeedContentObjectType forName(String name) { - for (FeedContentObjectType type : values()) { + public static FeedObjectType forName(String name) { + for (FeedObjectType type : values()) { if (type.name().equalsIgnoreCase(name)) { return type; } diff --git a/src/main/java/com/gitblit/servlet/SyndicationServlet.java b/src/main/java/com/gitblit/servlet/SyndicationServlet.java index 9650ee3a..631df781 100644 --- a/src/main/java/com/gitblit/servlet/SyndicationServlet.java +++ b/src/main/java/com/gitblit/servlet/SyndicationServlet.java @@ -164,9 +164,9 @@ public class SyndicationServlet extends DaggerServlet { } } - Constants.FeedContentObjectType objectType = Constants.FeedContentObjectType.COMMIT; + Constants.FeedObjectType objectType = Constants.FeedObjectType.COMMIT; if (!StringUtils.isEmpty(request.getParameter("ot"))) { - Constants.FeedContentObjectType type = Constants.FeedContentObjectType.forName(request.getParameter("ot")); + Constants.FeedObjectType type = Constants.FeedObjectType.forName(request.getParameter("ot")); if (type != null) { objectType = type; } @@ -249,7 +249,7 @@ public class SyndicationServlet extends DaggerServlet { feedDescription = model.description; } - if (objectType == Constants.FeedContentObjectType.TAG) { + if (objectType == Constants.FeedObjectType.TAG) { String urlPattern; if (mountParameters) { diff --git a/src/main/java/com/gitblit/utils/SyndicationUtils.java b/src/main/java/com/gitblit/utils/SyndicationUtils.java index 2ee1cf69..93e9321a 100644 --- a/src/main/java/com/gitblit/utils/SyndicationUtils.java +++ b/src/main/java/com/gitblit/utils/SyndicationUtils.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.List; import com.gitblit.Constants; +import com.gitblit.Constants.FeedObjectType; import com.gitblit.GitBlitException; import com.gitblit.models.FeedEntryModel; import com.sun.syndication.feed.synd.SyndCategory; @@ -137,6 +138,59 @@ public class SyndicationUtils { */ public static List readFeed(String url, String repository, String branch, int numberOfEntries, int page, String username, char[] password) throws IOException { + return readFeed(url, repository, branch, FeedObjectType.COMMIT, numberOfEntries, + page, username, password); + } + + /** + * Reads tags from the specified repository. + * + * @param url + * the url of the Gitblit server + * @param repository + * the repository name + * @param branch + * the branch name (optional) + * @param numberOfEntries + * the number of entries to retrieve. if <= 0 the server default + * is used. + * @param page + * 0-indexed. used to paginate the results. + * @param username + * @param password + * @return a list of SyndicationModel entries + * @throws {@link IOException} + */ + public static List readTags(String url, String repository, + int numberOfEntries, int page, String username, char[] password) throws IOException { + return readFeed(url, repository, null, FeedObjectType.TAG, numberOfEntries, + page, username, password); + } + + /** + * Reads a Gitblit RSS feed. + * + * @param url + * the url of the Gitblit server + * @param repository + * the repository name + * @param branch + * the branch name (optional) + * @param objectType + * the object type to return (optional, COMMIT assummed) + * @param numberOfEntries + * the number of entries to retrieve. if <= 0 the server default + * is used. + * @param page + * 0-indexed. used to paginate the results. + * @param username + * @param password + * @return a list of SyndicationModel entries + * @throws {@link IOException} + */ + private static List readFeed(String url, String repository, String branch, + FeedObjectType objectType, int numberOfEntries, int page, String username, + char[] password) throws IOException { // build feed url List parameters = new ArrayList(); if (numberOfEntries > 0) { @@ -148,6 +202,9 @@ public class SyndicationUtils { if (!StringUtils.isEmpty(branch)) { parameters.add("h=" + branch); } + if (objectType != null) { + parameters.add("ot=" + objectType.name()); + } return readFeed(url, parameters, repository, branch, username, password); } diff --git a/src/site/rpc.mkd b/src/site/rpc.mkd index 2e502e2c..302084fb 100644 --- a/src/site/rpc.mkd +++ b/src/site/rpc.mkd @@ -32,6 +32,7 @@ The Gitblit API includes methods for retrieving and interpreting RSS feeds. The url parameterdefaultdescription standard query repositoryrequiredrepository name is part of the url (see examples below) +ot=optional
default: COMMITobject type to return in results. COMMIT or TAG h=optional
default: HEADstarting branch, ref, or commit id l=optional
default: web.syndicationEntriesmaximum return count pg=optional
default: 0page number for paging
(offset into history = pagenumber*maximum return count) diff --git a/src/test/java/com/gitblit/tests/SyndicationUtilsTest.java b/src/test/java/com/gitblit/tests/SyndicationUtilsTest.java index d206bbdb..b4bb044f 100644 --- a/src/test/java/com/gitblit/tests/SyndicationUtilsTest.java +++ b/src/test/java/com/gitblit/tests/SyndicationUtilsTest.java @@ -21,7 +21,10 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import com.gitblit.Constants.SearchType; @@ -30,6 +33,20 @@ import com.gitblit.utils.SyndicationUtils; public class SyndicationUtilsTest extends GitblitUnitTest { + private static final AtomicBoolean started = new AtomicBoolean(false); + + @BeforeClass + public static void startGitblit() throws Exception { + started.set(GitBlitSuite.startGitblit()); + } + + @AfterClass + public static void stopGitblit() throws Exception { + if (started.get()) { + GitBlitSuite.stopGitblit(); + } + } + @Test public void testSyndication() throws Exception { List entries = new ArrayList(); @@ -60,7 +77,7 @@ public class SyndicationUtilsTest extends GitblitUnitTest { } @Test - public void testFeedRead() throws Exception { + public void testFeedReadCommits() throws Exception { Set links = new HashSet(); for (int i = 0; i < 2; i++) { List feed = SyndicationUtils.readFeed(GitBlitSuite.url, "ticgit.git", @@ -76,6 +93,23 @@ public class SyndicationUtilsTest extends GitblitUnitTest { assertEquals("Feed pagination failed", 10, links.size()); } + @Test + public void testFeedReadTags() throws Exception { + Set links = new HashSet(); + for (int i = 0; i < 2; i++) { + List feed = SyndicationUtils.readTags(GitBlitSuite.url, "test/gitective.git", + 5, i, GitBlitSuite.account, GitBlitSuite.password.toCharArray()); + assertTrue(feed != null); + assertTrue(feed.size() > 0); + assertEquals(5, feed.size()); + for (FeedEntryModel entry : feed) { + links.add(entry.link); + } + } + // confirm we have 10 unique tags + assertEquals("Feed pagination failed", 10, links.size()); + } + @Test public void testSearchFeedRead() throws Exception { List feed = SyndicationUtils -- cgit v1.2.3 From fd8cea4761b5382f23d06ed52608d1f556c4dbe5 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 22 May 2014 17:31:30 -0400 Subject: Render GFM links using Markdown, not direct HTML --- src/main/java/com/gitblit/utils/MarkdownUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/gitblit/utils') diff --git a/src/main/java/com/gitblit/utils/MarkdownUtils.java b/src/main/java/com/gitblit/utils/MarkdownUtils.java index da0db79d..2ebfdb26 100644 --- a/src/main/java/com/gitblit/utils/MarkdownUtils.java +++ b/src/main/java/com/gitblit/utils/MarkdownUtils.java @@ -136,7 +136,7 @@ public class MarkdownUtils { String canonicalUrl = settings.getString(Keys.web.canonicalUrl, "https://localhost:8443"); // emphasize and link mentions - String mentionReplacement = String.format(" **@$1**", canonicalUrl); + String mentionReplacement = String.format(" **[@$1](%1s/user/$1)**", canonicalUrl); text = text.replaceAll("\\s@([A-Za-z0-9-_]+)", mentionReplacement); // link ticket refs @@ -146,7 +146,7 @@ public class MarkdownUtils { // link commit shas int shaLen = settings.getInteger(Keys.web.shortCommitIdLength, 6); String commitPattern = MessageFormat.format("\\s([A-Fa-f0-9]'{'{0}'}')([A-Fa-f0-9]'{'{1}'}')", shaLen, 40 - shaLen); - String commitReplacement = String.format(" $1", canonicalUrl, repositoryName); + String commitReplacement = String.format(" [`$1`](%1$s/commit?r=%2$s&h=$1$2)", canonicalUrl, repositoryName); text = text.replaceAll(commitPattern, commitReplacement); String html = transformMarkdown(text); -- cgit v1.2.3