Skip to content

Commit eeb5dd3

Browse files
committed
[FIXED JENKINS-5163] Implemented support for ViewGit
1 parent 4d78ca0 commit eeb5dd3

5 files changed

Lines changed: 247 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package hudson.plugins.git.browser;
2+
3+
import hudson.Extension;
4+
import hudson.model.Descriptor;
5+
import hudson.plugins.git.GitChangeSet;
6+
import hudson.plugins.git.GitChangeSet.Path;
7+
import hudson.scm.EditType;
8+
import hudson.scm.RepositoryBrowser;
9+
import hudson.scm.browsers.QueryBuilder;
10+
11+
import java.io.IOException;
12+
import java.io.UnsupportedEncodingException;
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
import java.net.URLEncoder;
16+
17+
import net.sf.json.JSONObject;
18+
19+
import org.kohsuke.stapler.DataBoundConstructor;
20+
import org.kohsuke.stapler.StaplerRequest;
21+
22+
public class ViewGitWeb extends GitRepositoryBrowser {
23+
24+
private static final long serialVersionUID = 1L;
25+
private final URL url;
26+
private final String projectName;
27+
28+
@DataBoundConstructor
29+
public ViewGitWeb(String url, String projectName) throws MalformedURLException {
30+
this.url = normalizeToEndWithSlash(new URL(url));
31+
this.projectName = projectName;
32+
}
33+
34+
@Override
35+
public URL getDiffLink(Path path) throws IOException {
36+
if (path.getEditType() == EditType.EDIT) {
37+
String spec = buildCommitDiffSpec(path);
38+
return new URL(url, url.getPath() + spec);
39+
}
40+
return null;
41+
}
42+
43+
@Override
44+
public URL getFileLink(Path path) throws IOException {
45+
if (path.getEditType() == EditType.DELETE) {
46+
String spec = buildCommitDiffSpec(path);
47+
return new URL(url, url.getPath() + spec);
48+
}
49+
String spec = param().add("p=" + projectName).add("a=viewblob").add("h=" + path.getSrc()).add("f=" + path.getPath()).toString();
50+
return new URL(url, url.getPath() + spec);
51+
}
52+
53+
private String buildCommitDiffSpec(Path path)
54+
throws UnsupportedEncodingException {
55+
return param().add("p=" + projectName).add("a=commitdiff").add("h=" + path.getChangeSet().getId()).toString() + "#" + URLEncoder.encode(path.getPath(),"UTF-8").toString();
56+
}
57+
58+
@Override
59+
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
60+
return new URL(url, url.getPath() + param().add("p=" + projectName).add("a=commit").add("h=" + changeSet.getId()).toString());
61+
}
62+
63+
private QueryBuilder param() {
64+
return new QueryBuilder(url.getQuery());
65+
}
66+
67+
public URL getUrl() {
68+
return url;
69+
}
70+
71+
public String getProjectName() {
72+
return projectName;
73+
}
74+
75+
@Extension
76+
public static class ViewGitWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
77+
public String getDisplayName() {
78+
return "viewgit";
79+
}
80+
81+
@Override
82+
public ViewGitWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
83+
return req.bindParameters(ViewGitWeb.class, "viewgit.");
84+
}
85+
}
86+
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<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">
2+
<f:entry field="url" title="ViewGit root url">
3+
<f:textbox/>
4+
</f:entry>
5+
<f:entry field="projectName" title="Project Name in ViewGit">
6+
<f:textbox/>
7+
</f:entry>
8+
</j:jelly>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
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>)
3+
</div>
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
Specify the root URL serving this repository (such as <a target="_blank" href="http://code.fealdia.org/viewgit">this</a>).
3+
</div>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package hudson.plugins.git.browser;
2+
3+
import hudson.plugins.git.GitChangeLogParser;
4+
import hudson.plugins.git.GitChangeSet;
5+
import hudson.plugins.git.GitChangeSet.Path;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
import java.util.Collection;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
15+
import junit.framework.TestCase;
16+
17+
import org.xml.sax.SAXException;
18+
19+
/**
20+
* @author Paul Nyheim (paul.nyheim@gmail.com)
21+
*/
22+
public class ViewGitWebTest extends TestCase {
23+
24+
private static final String VIEWGIT_URL = "http://SERVER/viewgit";
25+
private static final String PROJECT_NAME = "PROJECT";
26+
private final ViewGitWeb viewGitWeb;
27+
28+
{
29+
try {
30+
viewGitWeb = new ViewGitWeb(VIEWGIT_URL, PROJECT_NAME);
31+
} catch (MalformedURLException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
/**
37+
* Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}.
38+
*
39+
* @throws MalformedURLException
40+
*/
41+
public void testGetUrl() throws MalformedURLException {
42+
assertEquals(String.valueOf(viewGitWeb.getUrl()), VIEWGIT_URL + "/");
43+
}
44+
45+
/**
46+
* Test method for {@link hudson.plugins.git.browser.ViewGitWeb#getUrl()}.
47+
*
48+
* @throws MalformedURLException
49+
*/
50+
public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException {
51+
assertEquals(String.valueOf(new ViewGitWeb(VIEWGIT_URL + "/", PROJECT_NAME).getUrl()), VIEWGIT_URL + "/");
52+
}
53+
54+
/**
55+
* Test method for
56+
* {@link hudson.plugins.git.browser.ViewGitWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}
57+
* .
58+
*
59+
* @throws SAXException
60+
* @throws IOException
61+
*/
62+
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
63+
final URL changeSetLink = viewGitWeb.getChangeSetLink(createChangeSet("rawchangelog"));
64+
assertEquals("http://SERVER/viewgit/?p=PROJECT&a=commit&h=396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
65+
}
66+
67+
/**
68+
* Test method for
69+
* {@link hudson.plugins.git.browser.ViewGitWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}
70+
* .
71+
*
72+
* @throws SAXException
73+
* @throws IOException
74+
*/
75+
public void testGetDiffLinkPath() throws IOException, SAXException {
76+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
77+
final Path path1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
78+
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Fmain%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWeb.java", viewGitWeb.getDiffLink(path1).toString());
79+
final Path path2 = pathMap.get("src/test/java/hudson/plugins/git/browser/GithubWebTest.java");
80+
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=396fc230a3db05c427737aa5c2eb7856ba72b05d#src%2Ftest%2Fjava%2Fhudson%2Fplugins%2Fgit%2Fbrowser%2FGithubWebTest.java", viewGitWeb.getDiffLink(path2).toString());
81+
final Path path3 = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file");
82+
assertNull("Do not return a diff link for added files.", viewGitWeb.getDiffLink(path3));
83+
}
84+
85+
/**
86+
* Test method for
87+
* {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}
88+
* .
89+
*
90+
* @throws SAXException
91+
* @throws IOException
92+
*/
93+
public void testGetFileLinkPath() throws IOException, SAXException {
94+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
95+
final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
96+
final URL fileLink = viewGitWeb.getFileLink(path);
97+
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=viewblob&h=3f28ad75f5ecd5e0ea9659362e2eef18951bd451&f=src/main/java/hudson/plugins/git/browser/GithubWeb.java",
98+
String.valueOf(fileLink));
99+
}
100+
101+
public void testGetDiffLinkForDeletedFile() throws Exception{
102+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
103+
final Path path = pathMap.get("bar");
104+
assertNull("Do not return a diff link for deleted files.", viewGitWeb.getDiffLink(path));
105+
106+
}
107+
108+
/**
109+
* Test method for
110+
* {@link hudson.plugins.git.browser.ViewGitWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}
111+
* .
112+
*
113+
* @throws SAXException
114+
* @throws IOException
115+
*/
116+
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
117+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
118+
final Path path = pathMap.get("bar");
119+
final URL fileLink = viewGitWeb.getFileLink(path);
120+
assertEquals(VIEWGIT_URL + "/?p=PROJECT&a=commitdiff&h=fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar", String.valueOf(fileLink));
121+
}
122+
123+
private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
124+
final File rawchangelog = new File(ViewGitWebTest.class.getResource(rawchangelogpath).getFile());
125+
final GitChangeLogParser logParser = new GitChangeLogParser(false);
126+
final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs();
127+
return changeSetList.get(0);
128+
}
129+
130+
/**
131+
* @param changelog
132+
* @return
133+
* @throws IOException
134+
* @throws SAXException
135+
*/
136+
private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException {
137+
final HashMap<String, Path> pathMap = new HashMap<String, Path>();
138+
final Collection<Path> changeSet = createChangeSet(changelog).getPaths();
139+
for (final Path path : changeSet) {
140+
pathMap.put(path.getPath(), path);
141+
}
142+
return pathMap;
143+
}
144+
145+
}

0 commit comments

Comments
 (0)