| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 19 | import com.google.common.base.CharMatcher; |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 25 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 26 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 27 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | |
| 29 | /** Gitiles access for testing. */ |
| 30 | public class TestGitilesAccess implements GitilesAccess.Factory { |
| 31 | private final DfsRepository repo; |
| 32 | |
| 33 | public TestGitilesAccess(DfsRepository repo) { |
| 34 | this.repo = checkNotNull(repo); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public GitilesAccess forRequest(final HttpServletRequest req) { |
| 39 | return new GitilesAccess() { |
| 40 | @Override |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 41 | public Map<String, RepositoryDescription> listRepositories( |
| 42 | String prefix, Set<String> branches) { |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 43 | String name = repo.getDescription().getRepositoryName(); |
| 44 | if (prefix != null) { |
| 45 | String pattern = CharMatcher.is('/').trimFrom(prefix) + '/'; |
| 46 | if (!name.startsWith(pattern)) { |
| 47 | return Collections.emptyMap(); |
| 48 | } |
| 49 | } |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 50 | if (branches != null && !branches.isEmpty()) { |
| 51 | throw new UnsupportedOperationException("branches set not yet supported"); |
| 52 | } |
| 53 | RepositoryDescription desc = new RepositoryDescription(); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 54 | desc.name = name; |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 55 | desc.cloneUrl = TestGitilesUrls.URLS.getBaseGitUrl(req) + "/" + desc.name; |
| 56 | return ImmutableMap.of(desc.name, desc); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Object getUserKey() { |
| 61 | return "a user"; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String getRepositoryName() { |
| 66 | return repo.getDescription().getRepositoryName(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public RepositoryDescription getRepositoryDescription() { |
| 71 | RepositoryDescription d = new RepositoryDescription(); |
| 72 | d.name = getRepositoryName(); |
| 73 | d.description = "a test data set"; |
| 74 | d.cloneUrl = TestGitilesUrls.URLS.getBaseGitUrl(req) + "/" + d.name; |
| 75 | return d; |
| 76 | } |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public Config getConfig() { |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 80 | Config config = new Config(); |
| Shawn Pearce | b55cf2b | 2017-06-29 21:56:37 -0700 | [diff] [blame] | 81 | config.setBoolean("markdown", null, "blocknote", true); |
| 82 | config.setBoolean("markdown", null, "multicolumn", true); |
| 83 | config.setBoolean("markdown", null, "namedanchor", true); |
| 84 | config.setBoolean("markdown", null, "smartquote", true); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 85 | config.setStringList( |
| 86 | "gitiles", null, "allowOriginRegex", ImmutableList.of("http://localhost")); |
| 87 | return config; |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 88 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 89 | }; |
| 90 | } |
| 91 | } |