Skip to content

Commit

Permalink
[FIXED JENKINS-5163] Implemented support for ViewGit
Browse files Browse the repository at this point in the history
  • Loading branch information
pnyheim committed Jun 16, 2011
1 parent 4d78ca0 commit eeb5dd3
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/java/hudson/plugins/git/browser/ViewGitWeb.java
@@ -0,0 +1,87 @@
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 hudson.scm.browsers.QueryBuilder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

public class ViewGitWeb extends GitRepositoryBrowser {

private static final long serialVersionUID = 1L;
private final URL url;
private final String projectName;

@DataBoundConstructor
public ViewGitWeb(String url, String projectName) throws MalformedURLException {
this.url = normalizeToEndWithSlash(new URL(url));
this.projectName = projectName;
}

@Override
public URL getDiffLink(Path path) throws IOException {
if (path.getEditType() == EditType.EDIT) {
String spec = buildCommitDiffSpec(path);
return new URL(url, url.getPath() + spec);
}
return null;
}

@Override
public URL getFileLink(Path path) throws IOException {
if (path.getEditType() == EditType.DELETE) {
String spec = buildCommitDiffSpec(path);
return new URL(url, url.getPath() + spec);
}
String spec = param().add("p=" + projectName).add("a=viewblob").add("h=" + path.getSrc()).add("f=" + path.getPath()).toString();
return new URL(url, url.getPath() + spec);
}

private String buildCommitDiffSpec(Path path)
throws UnsupportedEncodingException {
return param().add("p=" + projectName).add("a=commitdiff").add("h=" + path.getChangeSet().getId()).toString() + "#" + URLEncoder.encode(path.getPath(),"UTF-8").toString();
}

@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
return new URL(url, url.getPath() + param().add("p=" + projectName).add("a=commit").add("h=" + changeSet.getId()).toString());
}

private QueryBuilder param() {
return new QueryBuilder(url.getQuery());
}

public URL getUrl() {
return url;
}

public String getProjectName() {
return projectName;
}

@Extension
public static class ViewGitWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
public String getDisplayName() {
return "viewgit";
}

@Override
public ViewGitWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
return req.bindParameters(ViewGitWeb.class, "viewgit.");
}
}

}
@@ -0,0 +1,8 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="url" title="ViewGit root url">
<f:textbox/>
</f:entry>
<f:entry field="projectName" title="Project Name in ViewGit">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,4 @@
<div>
Specify the name of the project in ViewGit (e.g. scripts, scuttle etc. from <a href="http://code.fealdia.org/viewgit/">http://code.fealdia.org/viewgit/</a>)
</div>

@@ -0,0 +1,3 @@
<div>
Specify the root URL serving this repository (such as <a target="_blank" href="http://code.fealdia.org/viewgit">this</a>).
</div>
145 changes: 145 additions & 0 deletions src/test/java/hudson/plugins/git/browser/ViewGitWebTest.java
@@ -0,0 +1,145 @@
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;

/**
* @author Paul Nyheim (paul.nyheim@gmail.com)
*/
public class ViewGitWebTest extends TestCase {

private static final String VIEWGIT_URL = "http://SERVER/viewgit";
private static final String PROJECT_NAME = "PROJECT";
private final ViewGitWeb viewGitWeb;

{
try {
viewGitWeb = new ViewGitWeb(VIEWGIT_URL, PROJECT_NAME);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

/**
* Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}.
*
* @throws MalformedURLException
*/
public void testGetUrl() throws MalformedURLException {
assertEquals(String.valueOf(viewGitWeb.getUrl()), VIEWGIT_URL + "/");
}

/**
* Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}.
*
* @throws MalformedURLException
*/
public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException {
assertEquals(String.valueOf(new ViewGitWeb(VIEWGIT_URL + "/", PROJECT_NAME).getUrl()), VIEWGIT_URL + "/");
}

/**
* Test method for
* {@link hudson.plugins.git.browser.ViewGitWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}
* .
*
* @throws SAXException
* @throws IOException
*/
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
final URL changeSetLink = viewGitWeb.getChangeSetLink(createChangeSet("rawchangelog"));
assertEquals("http://SERVER/viewgit/?p=PROJECT&a=commit&h=396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
}

/**
* Test method for
* {@link hudson.plugins.git.browser.ViewGitWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}
* .
*
* @throws SAXException
* @throws IOException
*/
public void testGetDiffLinkPath() throws IOException, SAXException {
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
final Path path1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Fmain%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWeb.java", viewGitWeb.getDiffLink(path1).toString());
final Path path2 = pathMap.get("src/test/java/hudson/plugins/git/browser/GithubWebTest.java");
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Ftest%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWebTest.java", viewGitWeb.getDiffLink(path2).toString());
final Path path3 = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file");
assertNull("Do not return a diff link for added files.", viewGitWeb.getDiffLink(path3));
}

/**
* Test method for
* {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}
* .
*
* @throws SAXException
* @throws IOException
*/
public void testGetFileLinkPath() throws IOException, SAXException {
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
final URL fileLink = viewGitWeb.getFileLink(path);
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=viewblob&h=3f28ad75f5ecd5e0ea9659362e2eef18951bd451&f=src/main/java/hudson/plugins/git/browser/GithubWeb.java",
String.valueOf(fileLink));
}

public void testGetDiffLinkForDeletedFile() throws Exception{
final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
final Path path = pathMap.get("bar");
assertNull("Do not return a diff link for deleted files.", viewGitWeb.getDiffLink(path));

}

/**
* Test method for
* {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}
* .
*
* @throws SAXException
* @throws IOException
*/
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
final Path path = pathMap.get("bar");
final URL fileLink = viewGitWeb.getFileLink(path);
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar", String.valueOf(fileLink));
}

private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
final File rawchangelog = new File(ViewGitWebTest.class.getResource(rawchangelogpath).getFile());
final GitChangeLogParser logParser = new GitChangeLogParser(false);
final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs();
return changeSetList.get(0);
}

/**
* @param changelog
* @return
* @throws IOException
* @throws SAXException
*/
private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException {
final HashMap<String, Path> pathMap = new HashMap<String, Path>();
final Collection<Path> changeSet = createChangeSet(changelog).getPaths();
for (final Path path : changeSet) {
pathMap.put(path.getPath(), path);
}
return pathMap;
}

}

0 comments on commit eeb5dd3

Please sign in to comment.