Skip to content

Commit 5706ede

Browse files
author
Kraig Amador
committed
Adding support for Gitorious repositories
1 parent 7109f29 commit 5706ede

5 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
10+
import java.io.IOException;
11+
import java.net.URL;
12+
import java.net.MalformedURLException;
13+
14+
import net.sf.json.JSONObject;
15+
import org.kohsuke.stapler.DataBoundConstructor;
16+
import org.kohsuke.stapler.StaplerRequest;
17+
18+
/**
19+
* Git Browser for Gitorious
20+
*/
21+
public class GitoriousWeb extends GitRepositoryBrowser {
22+
23+
private static final long serialVersionUID = 1L;
24+
private final URL url;
25+
26+
@DataBoundConstructor
27+
public GitoriousWeb(String url) throws MalformedURLException {
28+
this.url = normalizeToEndWithSlash(new URL(url));
29+
}
30+
31+
public URL getUrl() {
32+
return url;
33+
}
34+
35+
@Override
36+
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
37+
return new URL(url, "commit/" + changeSet.getId().toString());
38+
}
39+
40+
/**
41+
* Creates a link to the commit diff.
42+
*
43+
* https://[Gitorious URL]/commit/a9182a07750c9a0dfd89a8461adf72ef5ef0885b
44+
*
45+
* @param path
46+
* @return diff link
47+
* @throws IOException
48+
*/
49+
@Override
50+
public URL getDiffLink(Path path) throws IOException {
51+
final GitChangeSet changeSet = path.getChangeSet();
52+
// gitorious does not show diffs on single files so we create a link to the commit
53+
return new URL(url, "commit/" + changeSet.getId().toString());
54+
}
55+
56+
/**
57+
* Creates a link to the file.
58+
* https://[Gitorious URL]/blobs/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/pom.xml
59+
*
60+
* @param path
61+
* @return file link
62+
* @throws IOException
63+
*/
64+
@Override
65+
public URL getFileLink(Path path) throws IOException {
66+
if (path.getEditType().equals(EditType.DELETE)) {
67+
return getDiffLink(path);
68+
} else {
69+
final String spec = "blobs/" + path.getChangeSet().getId() + "/" + path.getPath();
70+
return new URL(url, url.getPath() + spec);
71+
}
72+
}
73+
74+
@Extension
75+
public static class GitoriousWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
76+
public String getDisplayName() {
77+
return "gitoriousweb";
78+
}
79+
80+
@Override
81+
public GitoriousWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
82+
return req.bindParameters(GitoriousWeb.class, "Gitoriousweb.");
83+
}
84+
}
85+
86+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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="URL">
3+
<f:textbox/>
4+
</f:entry>
5+
</j:jelly>
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 href="http://gitorious.org/gitorious/mainline">http://gitorious.org/gitorious/mainline</a>).
3+
</div>

src/test/java/hudson/plugins/git/browser/BrowserChooserTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public void testRedmineWeb() throws IOException {
4747
testExistingBrowser(RedmineWeb.class);
4848
}
4949

50+
public void testGitoriousWeb() throws IOException {
51+
testExistingBrowser(GitoriousWeb.class);
52+
}
53+
5054
public void testGithubWeb() throws IOException {
5155
testExistingBrowser(GithubWeb.class);
5256
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
public class GitoriousWebTest extends TestCase {
21+
22+
/**
23+
*
24+
*/
25+
private static final String GITORIOUS_URL = "https://SERVER/PROJECT";
26+
private final GitoriousWeb gitoriousWeb;
27+
28+
{
29+
try {
30+
gitoriousWeb = new GitoriousWeb(GITORIOUS_URL);
31+
} catch (MalformedURLException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
/**
37+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
38+
* @throws MalformedURLException
39+
*/
40+
public void testGetUrl() throws MalformedURLException {
41+
assertEquals(String.valueOf(gitoriousWeb.getUrl()), GITORIOUS_URL + "/");
42+
}
43+
44+
/**
45+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
46+
* @throws MalformedURLException
47+
*/
48+
public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException {
49+
assertEquals(String.valueOf(new GitoriousWeb(GITORIOUS_URL + "/").getUrl()), GITORIOUS_URL + "/");
50+
}
51+
52+
/**
53+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}.
54+
* @throws SAXException
55+
* @throws IOException
56+
*/
57+
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
58+
final URL changeSetLink = gitoriousWeb.getChangeSetLink(createChangeSet("rawchangelog"));
59+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
60+
}
61+
62+
/**
63+
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}.
64+
* @throws SAXException
65+
* @throws IOException
66+
*/
67+
public void testGetDiffLinkPath() throws IOException, SAXException {
68+
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
69+
final Path modified1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
70+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(modified1).toString());
71+
// For added files returns a link to the commit.
72+
final Path added = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file");
73+
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(added).toString());
74+
}
75+
76+
/**
77+
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
78+
* @throws SAXException
79+
* @throws IOException
80+
*/
81+
public void testGetFileLinkPath() throws IOException, SAXException {
82+
final HashMap<String,Path> pathMap = createPathMap("rawchangelog");
83+
final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
84+
final URL fileLink = gitoriousWeb.getFileLink(path);
85+
assertEquals(GITORIOUS_URL + "/blobs/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink));
86+
}
87+
88+
/**
89+
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
90+
* @throws SAXException
91+
* @throws IOException
92+
*/
93+
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
94+
final HashMap<String,Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
95+
final Path path = pathMap.get("bar");
96+
final URL fileLink = gitoriousWeb.getFileLink(path);
97+
assertEquals(GITORIOUS_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f", String.valueOf(fileLink));
98+
}
99+
100+
private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
101+
final File rawchangelog = new File(GitoriousWebTest.class.getResource(rawchangelogpath).getFile());
102+
final GitChangeLogParser logParser = new GitChangeLogParser(false);
103+
final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs();
104+
return changeSetList.get(0);
105+
}
106+
107+
/**
108+
* @param changelog
109+
* @return
110+
* @throws IOException
111+
* @throws SAXException
112+
*/
113+
private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException {
114+
final HashMap<String, Path> pathMap = new HashMap<String, Path>();
115+
final Collection<Path> changeSet = createChangeSet(changelog).getPaths();
116+
for (final Path path : changeSet) {
117+
pathMap.put(path.getPath(), path);
118+
}
119+
return pathMap;
120+
}
121+
122+
123+
}

0 commit comments

Comments
 (0)