Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-5163] Implemented support for ViewGit
- Loading branch information
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/resources/hudson/plugins/git/browser/ViewGitWeb/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
4 changes: 4 additions & 0 deletions
4
src/main/resources/hudson/plugins/git/browser/ViewGitWeb/help-projectName.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
3 changes: 3 additions & 0 deletions
3
src/main/resources/hudson/plugins/git/browser/ViewGitWeb/help-url.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
145
src/test/java/hudson/plugins/git/browser/ViewGitWebTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |