From 5706edec674c9fc1a57124a30dfdb48e15bb7185 Mon Sep 17 00:00:00 2001 From: Kraig Amador Date: Thu, 14 Jul 2011 11:19:14 -0700 Subject: [PATCH] Adding support for Gitorious repositories --- .../plugins/git/browser/GitoriousWeb.java | 86 ++++++++++++ .../git/browser/GitoriousWeb/config.jelly | 5 + .../git/browser/GitoriousWeb/help-url.html | 3 + .../git/browser/BrowserChooserTest.java | 4 + .../plugins/git/browser/GitoriousWebTest.java | 123 ++++++++++++++++++ 5 files changed, 221 insertions(+) create mode 100644 src/main/java/hudson/plugins/git/browser/GitoriousWeb.java create mode 100644 src/main/resources/hudson/plugins/git/browser/GitoriousWeb/config.jelly create mode 100644 src/main/resources/hudson/plugins/git/browser/GitoriousWeb/help-url.html create mode 100644 src/test/java/hudson/plugins/git/browser/GitoriousWebTest.java diff --git a/src/main/java/hudson/plugins/git/browser/GitoriousWeb.java b/src/main/java/hudson/plugins/git/browser/GitoriousWeb.java new file mode 100644 index 0000000000..3252a6dab3 --- /dev/null +++ b/src/main/java/hudson/plugins/git/browser/GitoriousWeb.java @@ -0,0 +1,86 @@ +package hudson.plugins.git.browser; + +import hudson.Extension; +import hudson.model.Descriptor; +import hudson.plugins.git.GitChangeSet; +import hudson.plugins.git.GitChangeSet.Path; +import hudson.scm.EditType; +import hudson.scm.RepositoryBrowser; + +import java.io.IOException; +import java.net.URL; +import java.net.MalformedURLException; + +import net.sf.json.JSONObject; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.StaplerRequest; + +/** + * Git Browser for Gitorious + */ +public class GitoriousWeb extends GitRepositoryBrowser { + + private static final long serialVersionUID = 1L; + private final URL url; + + @DataBoundConstructor + public GitoriousWeb(String url) throws MalformedURLException { + this.url = normalizeToEndWithSlash(new URL(url)); + } + + public URL getUrl() { + return url; + } + + @Override + public URL getChangeSetLink(GitChangeSet changeSet) throws IOException { + return new URL(url, "commit/" + changeSet.getId().toString()); + } + + /** + * Creates a link to the commit diff. + * + * https://[Gitorious URL]/commit/a9182a07750c9a0dfd89a8461adf72ef5ef0885b + * + * @param path + * @return diff link + * @throws IOException + */ + @Override + public URL getDiffLink(Path path) throws IOException { + final GitChangeSet changeSet = path.getChangeSet(); + // gitorious does not show diffs on single files so we create a link to the commit + return new URL(url, "commit/" + changeSet.getId().toString()); + } + + /** + * Creates a link to the file. + * https://[Gitorious URL]/blobs/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/pom.xml + * + * @param path + * @return file link + * @throws IOException + */ + @Override + public URL getFileLink(Path path) throws IOException { + if (path.getEditType().equals(EditType.DELETE)) { + return getDiffLink(path); + } else { + final String spec = "blobs/" + path.getChangeSet().getId() + "/" + path.getPath(); + return new URL(url, url.getPath() + spec); + } + } + + @Extension + public static class GitoriousWebDescriptor extends Descriptor> { + public String getDisplayName() { + return "gitoriousweb"; + } + + @Override + public GitoriousWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException { + return req.bindParameters(GitoriousWeb.class, "Gitoriousweb."); + } + } + +} diff --git a/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/config.jelly b/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/config.jelly new file mode 100644 index 0000000000..a87014bc7f --- /dev/null +++ b/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/config.jelly @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/help-url.html b/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/help-url.html new file mode 100644 index 0000000000..7cf22b5cb0 --- /dev/null +++ b/src/main/resources/hudson/plugins/git/browser/GitoriousWeb/help-url.html @@ -0,0 +1,3 @@ +
+ Specify the root URL serving this repository (such as http://gitorious.org/gitorious/mainline). +
\ No newline at end of file diff --git a/src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java b/src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java index 281740eeea..e9eed3eae7 100644 --- a/src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java +++ b/src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java @@ -47,6 +47,10 @@ public void testRedmineWeb() throws IOException { testExistingBrowser(RedmineWeb.class); } + public void testGitoriousWeb() throws IOException { + testExistingBrowser(GitoriousWeb.class); + } + public void testGithubWeb() throws IOException { testExistingBrowser(GithubWeb.class); } diff --git a/src/test/java/hudson/plugins/git/browser/GitoriousWebTest.java b/src/test/java/hudson/plugins/git/browser/GitoriousWebTest.java new file mode 100644 index 0000000000..908bca95c9 --- /dev/null +++ b/src/test/java/hudson/plugins/git/browser/GitoriousWebTest.java @@ -0,0 +1,123 @@ +package hudson.plugins.git.browser; + +import hudson.plugins.git.GitChangeLogParser; +import hudson.plugins.git.GitChangeSet; +import hudson.plugins.git.GitChangeSet.Path; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + +import junit.framework.TestCase; + +import org.xml.sax.SAXException; + + +public class GitoriousWebTest extends TestCase { + + /** + * + */ + private static final String GITORIOUS_URL = "https://SERVER/PROJECT"; + private final GitoriousWeb gitoriousWeb; + + { + try { + gitoriousWeb = new GitoriousWeb(GITORIOUS_URL); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + /** + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}. + * @throws MalformedURLException + */ + public void testGetUrl() throws MalformedURLException { + assertEquals(String.valueOf(gitoriousWeb.getUrl()), GITORIOUS_URL + "/"); + } + + /** + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}. + * @throws MalformedURLException + */ + public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException { + assertEquals(String.valueOf(new GitoriousWeb(GITORIOUS_URL + "/").getUrl()), GITORIOUS_URL + "/"); + } + + /** + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}. + * @throws SAXException + * @throws IOException + */ + public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException { + final URL changeSetLink = gitoriousWeb.getChangeSetLink(createChangeSet("rawchangelog")); + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString()); + } + + /** + * Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}. + * @throws SAXException + * @throws IOException + */ + public void testGetDiffLinkPath() throws IOException, SAXException { + final HashMap pathMap = createPathMap("rawchangelog"); + final Path modified1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(modified1).toString()); + // For added files returns a link to the commit. + final Path added = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file"); + assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(added).toString()); + } + + /** + * Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}. + * @throws SAXException + * @throws IOException + */ + public void testGetFileLinkPath() throws IOException, SAXException { + final HashMap pathMap = createPathMap("rawchangelog"); + final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java"); + final URL fileLink = gitoriousWeb.getFileLink(path); + assertEquals(GITORIOUS_URL + "/blobs/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink)); + } + + /** + * Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}. + * @throws SAXException + * @throws IOException + */ + public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException { + final HashMap pathMap = createPathMap("rawchangelog-with-deleted-file"); + final Path path = pathMap.get("bar"); + final URL fileLink = gitoriousWeb.getFileLink(path); + assertEquals(GITORIOUS_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f", String.valueOf(fileLink)); + } + + private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException { + final File rawchangelog = new File(GitoriousWebTest.class.getResource(rawchangelogpath).getFile()); + final GitChangeLogParser logParser = new GitChangeLogParser(false); + final List changeSetList = logParser.parse(null, rawchangelog).getLogs(); + return changeSetList.get(0); + } + + /** + * @param changelog + * @return + * @throws IOException + * @throws SAXException + */ + private HashMap createPathMap(final String changelog) throws IOException, SAXException { + final HashMap pathMap = new HashMap(); + final Collection changeSet = createChangeSet(changelog).getPaths(); + for (final Path path : changeSet) { + pathMap.put(path.getPath(), path); + } + return pathMap; + } + + +}