Skip to content

Commit

Permalink
[FIXED JENKINS-19029] GitList browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark committed Sep 15, 2014
1 parent 050de72 commit fa7baf4
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/main/java/hudson/plugins/git/browser/GitList.java
@@ -0,0 +1,108 @@
package hudson.plugins.git.browser;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.EnvironmentContributor;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;

/**
* Git Browser URLs
*/
public class GitList extends GitRepositoryBrowser {

private static final long serialVersionUID = 1L;

@DataBoundConstructor
public GitList(String repoUrl) {
super(repoUrl);
}

@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, url.getPath() + "commit/" + changeSet.getId().toString());
}

/**
* Creates a link to the file diff.
* http://[GitList URL]/commit/6c99ffee4cb6d605d55a1cc7cb47f25a443f7f54#N
*
* @param path affected file path
* @return diff link
* @throws IOException
*/
@Override
public URL getDiffLink(Path path) throws IOException {
if(path.getEditType() != EditType.EDIT || path.getSrc() == null || path.getDst() == null
|| path.getChangeSet().getParentCommit() == null) {
return null;
}
return getDiffLinkRegardlessOfEditType(path);
}

/**
* Return a diff link regardless of the edit type by appending the index of the pathname in the changeset.
*
* @param path
* @return url for differences
* @throws IOException
*/
private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
final GitChangeSet changeSet = path.getChangeSet();
final ArrayList<String> affectedPaths = new ArrayList<String>(changeSet.getAffectedPaths());
Collections.sort(affectedPaths);
final String pathAsString = path.getPath();
final int i = Collections.binarySearch(affectedPaths, pathAsString);
assert i >= 0;
return new URL(getChangeSetLink(changeSet), "#" + String.valueOf(i + 1)); //GitList diff indices begin at 1
}

/**
* Creates a link to the file.
* http://[GitList URL]/blob/6c99ffee4cb6d605d55a1cc7cb47f25a443f7f54/src/gitlist/Application.php
*
* @param path file
* @return file link
* @throws IOException
*/
@Override
public URL getFileLink(Path path) throws IOException {
if (path.getEditType().equals(EditType.DELETE)) {
return getDiffLinkRegardlessOfEditType(path);
} else {
final String spec = "blob/" + path.getChangeSet().getId() + "/" + path.getPath();
URL url = getUrl();
return new URL(url, url.getPath() + spec);
}
}

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

@Override
public GitList newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
return req.bindJSON(GitList.class, jsonObject);
}
}
}
@@ -0,0 +1,5 @@
<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="repoUrl" title="URL">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Specify the root URL serving this repository (such as <a href="http://gitlistserver:port/repo/">http://gitlistserver:port/repo/</a>).
</div>
122 changes: 122 additions & 0 deletions src/test/java/hudson/plugins/git/browser/GitListTest.java
@@ -0,0 +1,122 @@
/**
* Copyright 2010 Mirko Friedenhagen
*/

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 mirko
* @author fauxpark
*
*/
public class GitListTest extends TestCase {

/**
*
*/
private static final String GITLIST_URL = "http://gitlist.org/REPO";
private final GitList gitlist = new GitList(GITLIST_URL);

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

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

/**
* Test method for {@link hudson.plugins.git.browser.GitList#getChangeSetLink(hudson.plugins.git.GitChangeSet)}.
* @throws SAXException
* @throws IOException
*/
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
final URL changeSetLink = gitlist.getChangeSetLink(createChangeSet("rawchangelog"));
assertEquals(GITLIST_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GitList#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(GITLIST_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#1", gitlist.getDiffLink(path1).toString());
final Path path2 = pathMap.get("src/test/java/hudson/plugins/git/browser/GithubWebTest.java");
assertEquals(GITLIST_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#2", gitlist.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.", gitlist.getDiffLink(path3));
}

/**
* Test method for {@link hudson.plugins.git.browser.GitList#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 = gitlist.getFileLink(path);
assertEquals(GITLIST_URL + "/blob/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink));
}

/**
* Test method for {@link hudson.plugins.git.browser.GitList#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 = gitlist.getFileLink(path);
assertEquals(GITLIST_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#1", String.valueOf(fileLink));
}

private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
final File rawchangelog = new File(GitListTest.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 fa7baf4

Please sign in to comment.